added included_fields patch from abs
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Collection / Grid.pm
1 package Reaction::UI::ViewPort::Collection::Grid;
2
3 use Reaction::Class;
4
5 use aliased 'Reaction::InterfaceModel::Collection' => 'IM_Collection';
6 use aliased 'Reaction::UI::ViewPort::Collection::Grid::Member::WithActions';
7
8 use namespace::clean -except => [ qw(meta) ];
9 extends 'Reaction::UI::ViewPort::Collection';
10
11 has field_order => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
12 has excluded_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
13 has included_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
14 has computed_field_order => (is => 'ro', isa => 'ArrayRef', lazy_build => 1);
15
16 has _raw_field_labels => (
17   is => 'rw',
18   isa => 'HashRef',
19   init_arg => 'field_labels',
20   default => sub { {} },
21 );
22
23 has field_labels => (
24   is => 'ro',
25   isa => 'HashRef',
26   lazy_build => 1,
27   init_arg => undef,
28 );
29
30 has member_action_count => (
31   is => 'rw',
32   isa => 'Int',
33   required => 1,
34   lazy => 1,
35   default => sub {
36     my $self = shift;
37     for (@{ $self->members }) {
38       my $protos = $_->action_prototypes;
39       return scalar(keys(%$protos));
40     }
41     return 1;
42   },
43 );
44
45 ####################################
46 sub _build_member_class { WithActions };
47
48 sub _build_field_labels {
49   my $self = shift;
50   my %labels = %{$self->_raw_field_labels};
51   for my $field ( @{$self->computed_field_order}) {
52     next if $labels{$field};
53     $labels{$field} = join(' ', map{ ucfirst } split('_', $field));
54   }
55   return \%labels;
56 }
57
58 sub _build_field_order { []; }
59
60 sub _build_excluded_fields { []; }
61
62 sub _build_included_fields { [] }
63
64 #this is a total clusterfuck and it sucks we should just eliminate it and have
65 # the grid members not render ArrayRef or Collection fields
66 sub _build_computed_field_order {
67   my ($self) = @_;
68   my %excluded = map { $_ => undef } @{ $self->excluded_fields };
69   my %included = map { $_ => undef } @{ $self->included_fields };
70   #treat _$field_name as private and exclude fields with no reader
71   my @names = grep { $_ !~ /^_/ &&  (!%included || exists( $included{$_}) )
72     && !exists($excluded{$_})} map { $_->name }
73     grep {
74       !($_->has_type_constraint &&
75         ($_->type_constraint->is_a_type_of('ArrayRef') ||
76          eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} ||
77          eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') }
78         )
79        )  }
80       grep { defined $_->get_read_method }
81         $self->current_collection->member_type->parameter_attributes;
82
83   return $self->sort_by_spec($self->field_order, \@names);
84 }
85
86 around _build_members => sub {
87   my $orig = shift;
88   my $self = shift;
89   $self->member_args->{computed_field_order} ||= $self->computed_field_order;
90   $self->member_args->{computed_action_order} ||= [];
91   my $members = $self->$orig(@_);
92
93   # cache everything yo
94   for my $member (@$members){
95     $member->clear_computed_action_order;
96     my $order = $member->computed_action_order;
97     @{ $self->member_args->{computed_action_order} } = @$order;
98     last;
99   }
100
101   return $members;
102 };
103
104 __PACKAGE__->meta->make_immutable;
105
106
107 1;
108
109 __END__;
110
111 =head1 NAME
112
113 Reaction::UI::ViewPort::Collection
114
115 =head1 DESCRIPTION
116
117 This subclass of L<Reaction::UI::ViewPort::Collection> allows you to display a
118 homogenous collection of Reaction::InterfaceModel::Objects as a grid.
119
120 =head1 ATTRIBUTES
121
122 =head2 field_order
123
124 =head2 excluded_fields
125
126 List of field names to exclude.
127
128 =head2 included_fields
129
130 List of field names to include. If both C<included_fields> and
131 C<excluded_fields> are specified the result is those fields which
132 are in C<included_fields> and not in C<excluded_fields>.
133
134 =head2 included_fields
135
136 List of field names to include. If both C<included_fields> and
137 C<excluded_fields> are specified the result is those fields which
138 are in C<included_fields> and not in C<excluded_fields>.
139
140
141 =head2 field_labels
142
143 =head2 _raw_field_labels
144
145 =head2 computed_field_order
146
147 =head2 member_action_count
148
149 =head1 INTERNAL METHODS
150
151 These methods, although stable, are subject to change without notice. These are meant
152 to be used only by developers. End users should refrain from using these methods to
153 avoid potential breakages.
154
155 =head1 SEE ALSO
156
157 L<Reaction::UI::ViewPort::Collection>
158
159 =head1 AUTHORS
160
161 See L<Reaction::Class> for authors.
162
163 =head1 LICENSE
164
165 See L<Reaction::Class> for the license.
166
167 =cut