refactor CustomFields so with_custom_fields is available on backend
This commit is contained in:
		
							
								
								
									
										59
									
								
								lib/App/Netdisco/Util/CustomFields.pm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								lib/App/Netdisco/Util/CustomFields.pm
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| package App::Netdisco::Util::CustomFields; | ||||
|  | ||||
| use Dancer ':syntax'; | ||||
| use Dancer::Plugin::DBIC; | ||||
|  | ||||
| use App::Netdisco::DB::ResultSet::Device; | ||||
| use App::Netdisco::DB::ResultSet::DevicePort; | ||||
|  | ||||
| my @inline_device_actions = (); | ||||
| my @inline_device_port_actions = (); | ||||
|  | ||||
| foreach my $config (@{ setting('custom_fields')->{'device'} || [] }) { | ||||
|   next unless $config->{'name'}; | ||||
|   push @inline_device_actions, $config->{'name'}; | ||||
| } | ||||
|  | ||||
| foreach my $config (@{ setting('custom_fields')->{'device_port'} || [] }) { | ||||
|   next unless $config->{'name'}; | ||||
|   push @inline_device_port_actions, $config->{'name'}; | ||||
| } | ||||
|  | ||||
| { | ||||
|   package App::Netdisco::DB::ResultSet::Device; | ||||
|  | ||||
|   sub with_custom_fields { | ||||
|     my ($rs, $cond, $attrs) = @_; | ||||
|  | ||||
|     return $rs | ||||
|       ->search_rs($cond, $attrs) | ||||
|       ->search({}, | ||||
|         { '+columns' => { | ||||
|             map {( ('cf_'. $_) => \[ 'me.custom_fields ->> ?' => $_ ] )} | ||||
|                 @inline_device_actions | ||||
|         }}); | ||||
|   } | ||||
| } | ||||
|  | ||||
| { | ||||
|   package App::Netdisco::DB::ResultSet::DevicePort; | ||||
|  | ||||
|   sub with_custom_fields { | ||||
|     my ($rs, $cond, $attrs) = @_; | ||||
|  | ||||
|     return $rs | ||||
|       ->search_rs($cond, $attrs) | ||||
|       ->search({}, | ||||
|         { '+columns' => { | ||||
|             map {( ('cf_'. $_) => \[ 'me.custom_fields ->> ?' => $_ ] )} | ||||
|                 @inline_device_port_actions | ||||
|         }}); | ||||
|   } | ||||
| } | ||||
|  | ||||
| set('_inline_actions' => [ | ||||
|   map {'cf_' . $_} (@inline_device_actions, @inline_device_port_actions) | ||||
| ]); | ||||
|  | ||||
| true; | ||||
|  | ||||
| @@ -6,6 +6,7 @@ use Dancer qw/:syntax :script/; | ||||
|  | ||||
| use Scalar::Util qw/blessed reftype/; | ||||
| use NetAddr::IP::Lite ':lower'; | ||||
|  | ||||
| use App::Netdisco::Util::DNS 'hostname_from_ip'; | ||||
|  | ||||
| use base 'Exporter'; | ||||
|   | ||||
| @@ -1,86 +1,30 @@ | ||||
| package App::Netdisco::Web::CustomFields; | ||||
|  | ||||
| use Dancer ':syntax'; | ||||
| use Dancer::Plugin::DBIC; | ||||
|  | ||||
| use App::Netdisco::DB::ResultSet::Device; | ||||
| use App::Netdisco::DB::ResultSet::DevicePort; | ||||
|  | ||||
| use App::Netdisco::Web::Plugin; | ||||
|  | ||||
| my @inline_device_actions = (); | ||||
| my @inline_device_port_actions = (); | ||||
| use App::Netdisco::Util::CustomFields; | ||||
|  | ||||
| foreach my $config (@{ setting('custom_fields')->{'device'} || [] }) { | ||||
|  | ||||
|   if (! $config->{'name'}) { | ||||
|       error 'custom_field missing name'; | ||||
|       next; | ||||
|   } | ||||
|   next unless $config->{'name'}; | ||||
|  | ||||
|   register_device_details({ | ||||
|     %{ $config }, | ||||
|     field => ('cf_' . $config->{'name'}), | ||||
|     label => ($config->{'label'} || ucfirst $config->{'name'}), | ||||
|   }) unless $config->{'hidden'}; | ||||
|  | ||||
|   push @inline_device_actions, $config->{'name'}; | ||||
|  | ||||
| } | ||||
|  | ||||
| foreach my $config (@{ setting('custom_fields')->{'device_port'} || [] }) { | ||||
|  | ||||
|   if (! $config->{'name'}) { | ||||
|       error 'custom_field missing name'; | ||||
|       next; | ||||
|   } | ||||
|   next unless $config->{'name'}; | ||||
|  | ||||
|   register_device_port_column({ | ||||
|     position => 'right', # or "mid" or "right" | ||||
|     default  => undef,   # or undef | ||||
|     default  => undef,   # or "checked" | ||||
|     %{ $config }, | ||||
|     field => ('cf_' . $config->{'name'}), | ||||
|     label => ($config->{'label'} || ucfirst $config->{'name'}), | ||||
|   }) unless $config->{'hidden'}; | ||||
|  | ||||
|   push @inline_device_port_actions, $config->{'name'}; | ||||
|  | ||||
| } | ||||
|  | ||||
| { | ||||
|   package App::Netdisco::DB::ResultSet::Device; | ||||
|  | ||||
|   sub with_custom_fields { | ||||
|     my ($rs, $cond, $attrs) = @_; | ||||
|  | ||||
|     return $rs | ||||
|       ->search_rs($cond, $attrs) | ||||
|       ->search({}, | ||||
|         { '+columns' => { | ||||
|             map {( ('cf_'. $_) => \[ 'me.custom_fields ->> ?' => $_ ] )} | ||||
|                 @inline_device_actions | ||||
|         }}); | ||||
|   } | ||||
| } | ||||
|  | ||||
| { | ||||
|   package App::Netdisco::DB::ResultSet::DevicePort; | ||||
|  | ||||
|   sub with_custom_fields { | ||||
|     my ($rs, $cond, $attrs) = @_; | ||||
|  | ||||
|     return $rs | ||||
|       ->search_rs($cond, $attrs) | ||||
|       ->search({}, | ||||
|         { '+columns' => { | ||||
|             map {( ('cf_'. $_) => \[ 'me.custom_fields ->> ?' => $_ ] )} | ||||
|                 @inline_device_port_actions | ||||
|         }}); | ||||
|   } | ||||
| } | ||||
|  | ||||
| set('_inline_actions' => [ | ||||
|   map {'cf_' . $_} (@inline_device_actions, @inline_device_port_actions) | ||||
| ]); | ||||
|  | ||||
| true; | ||||
|   | ||||
| @@ -2,6 +2,8 @@ package App::Netdisco::Worker::Runner; | ||||
|  | ||||
| use Dancer qw/:moose :syntax/; | ||||
| use Dancer::Plugin::DBIC 'schema'; | ||||
|  | ||||
| use App::Netdisco::Util::CustomFields; | ||||
| use App::Netdisco::Util::Device 'get_device'; | ||||
| use App::Netdisco::Util::Permission qw/acl_matches acl_matches_only/; | ||||
| use aliased 'App::Netdisco::Worker::Status'; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user