bff31c2664c383a7947b6674129fdae9f2c7e8c5
[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   $self->member_args->{computed_action_order} ||= [];
68   my $members = $self->$orig(@_);
69
70   # cache everything yo
71   for my $member (@$members){
72     $member->clear_computed_action_order;
73     my $order = $member->computed_action_order;
74     @{ $self->member_args->{computed_action_order} } = @$order;
75     last;
76   }
77
78   return $members;
79 };
80
81 1;
82
83 __END__;
84
85 =head1 NAME
86
87 Reaction::UI::ViewPort::Role::FieldOptions
88
89 =head1 DESCRIPTION
90
91 =head1 ATTRIBUTES
92
93 =head2 field_order
94
95 =head2 excluded_fields
96
97 List of field names to exclude.
98
99 =head2 included_fields
100
101 List of field names to include. If both C<included_fields> and
102 C<excluded_fields> are specified the result is those fields which
103 are in C<included_fields> and not in C<excluded_fields>.
104
105 =head2 included_fields
106
107 List of field names to include. If both C<included_fields> and
108 C<excluded_fields> are specified the result is those fields which
109 are in C<included_fields> and not in C<excluded_fields>.
110
111 =head2 field_labels
112
113 =head2 computed_field_order
114
115 =head1 INTERNAL METHODS
116
117 These methods, although stable, are subject to change without notice. These are meant
118 to be used only by developers. End users should refrain from using these methods to
119 avoid potential breakages.
120
121 =head1 SEE ALSO
122
123 L<Reaction::UI::ViewPort::Collection>
124
125 =head1 AUTHORS
126
127 See L<Reaction::Class> for authors.
128
129 =head1 LICENSE
130
131 See L<Reaction::Class> for the license.
132
133 =cut