allow redirect_to a url parameter
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Role / FieldArgs.pm
1 package Reaction::UI::ViewPort::Role::FieldArgs;
2
3 use Reaction::Role;
4 use namespace::clean -except => [ qw(meta) ];
5
6 has field_order => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
7 has excluded_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
8 has included_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
9 has computed_field_order => (is => 'ro', isa => 'ArrayRef', lazy_build => 1);
10
11 has _raw_field_labels => (
12   is => 'rw',
13   isa => 'HashRef',
14   init_arg => 'field_labels',
15   default => sub { {} },
16 );
17
18 has field_labels => (
19   is => 'ro',
20   isa => 'HashRef',
21   lazy_build => 1,
22   init_arg => undef,
23 );
24
25 sub _build_field_labels {
26   my $self = shift;
27   my %labels = %{$self->_raw_field_labels};
28   for my $field ( @{$self->computed_field_order}) {
29     next if defined $labels{$field};
30     $labels{$field} = join(' ', map{ ucfirst } split('_', $field));
31   }
32   return \%labels;
33 }
34
35 sub _build_field_order { [] }
36
37 sub _build_excluded_fields { [] }
38
39 sub _build_included_fields { [] }
40
41 #this is a total clusterfuck and it sucks we should just eliminate it and have
42 # the grid members not render ArrayRef or Collection fields
43 sub _build_computed_field_order {
44   my ($self) = @_;
45   my %excluded = map { $_ => undef } @{ $self->excluded_fields };
46   my %included = map { $_ => undef } @{ $self->included_fields };
47   #treat _$field_name as private and exclude fields with no reader
48   my @names = grep { $_ !~ /^_/ &&  (!%included || exists( $included{$_}) )
49     && !exists($excluded{$_})} map { $_->name }
50     grep {
51       !($_->has_type_constraint &&
52         ($_->type_constraint->is_a_type_of('ArrayRef') ||
53          eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} ||
54          eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') }
55         )
56        )  }
57       grep { defined $_->get_read_method }
58         $self->current_collection->member_type->parameter_attributes;
59
60   return $self->sort_by_spec($self->field_order, \@names);
61 }
62
63 around _build_members => sub {
64   my $orig = shift;
65   my $self = shift;
66   $self->member_args->{computed_field_order} ||= $self->computed_field_order;
67   my $members = $self->$orig(@_);
68
69   return $members;
70 };
71
72 1;
73
74 __END__;
75
76 =head1 NAME
77
78 Reaction::UI::ViewPort::Role::FieldOptions
79
80 =head1 DESCRIPTION
81
82 =head1 ATTRIBUTES
83
84 =head2 field_order
85
86 =head2 excluded_fields
87
88 List of field names to exclude.
89
90 =head2 included_fields
91
92 List of field names to include. If both C<included_fields> and
93 C<excluded_fields> are specified the result is those fields which
94 are in C<included_fields> and not in C<excluded_fields>.
95
96 =head2 included_fields
97
98 List of field names to include. If both C<included_fields> and
99 C<excluded_fields> are specified the result is those fields which
100 are in C<included_fields> and not in C<excluded_fields>.
101
102 =head2 field_labels
103
104 =head2 computed_field_order
105
106 =head1 INTERNAL METHODS
107
108 These methods, although stable, are subject to change without notice. These are meant
109 to be used only by developers. End users should refrain from using these methods to
110 avoid potential breakages.
111
112 =head1 SEE ALSO
113
114 L<Reaction::UI::ViewPort::Collection>
115
116 =head1 AUTHORS
117
118 See L<Reaction::Class> for authors.
119
120 =head1 LICENSE
121
122 See L<Reaction::Class> for the license.
123
124 =cut