rclass stuff ripped out of everything but widget classes
[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';
7
8 use namespace::clean -except => [ qw(meta) ];
9 extends 'Reaction::UI::ViewPort::Collection';
10
11
12
13 has field_order     => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
14 has excluded_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
15 has field_labels    => ( is => 'ro', isa => 'HashRef',  lazy_build => 1);
16
17 has computed_field_order => (is => 'ro', isa => 'ArrayRef', lazy_build => 1);
18
19 ####################################
20 sub _build_member_class { Member };
21 sub _build_field_labels {
22   my $self = shift;
23   my %labels;
24   for my $field ( @{$self->computed_field_order}){
25     $labels{$field} = join(' ', map{ ucfirst } split('_', $field));
26   }
27   return \%labels;
28 };
29 sub _build_field_order { []; };
30 sub _build_excluded_fields { []; };
31 sub _build_computed_field_order {
32   my ($self) = @_;
33   my %excluded = map { $_ => undef } @{ $self->excluded_fields };
34   #treat _$field_name as private and exclude fields with no reader
35   my @names = grep { $_ !~ /^_/ && !exists($excluded{$_})} map { $_->name }
36     grep {
37       !($_->has_type_constraint &&
38         ($_->type_constraint->is_a_type_of('ArrayRef') ||
39          eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} ||
40          eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') }
41         )
42        )  }
43       grep { defined $_->get_read_method }
44         $self->current_collection->member_type->parameter_attributes;
45
46   return $self->sort_by_spec($self->field_order, \@names);
47 };
48
49 before _build_members => sub {
50   my ($self) = @_;
51   $self->member_args->{computed_field_order} ||= $self->computed_field_order;
52 };
53
54 __PACKAGE__->meta->make_immutable;
55
56
57 1;
58
59 __END__;
60
61 =head1 NAME
62
63 Reaction::UI::ViewPort::Collection
64
65 =head1 DESCRIPTION
66
67 This subclass of L<Reaction::UI::ViewPort::Collection> allows you to display a
68 homogenous collection of Reaction::InterfaceModel::Objects as a grid.
69
70 =head1 ATTRIBUTES
71
72 =head2 field_order
73
74 =head2 excluded_fields
75
76 =head2 field_labels
77
78 =head2 computed_field_order
79
80 =head1
81
82 =head1 INTERNAL METHODS
83
84 These methods, although stable, are subject to change without notice. These are meant
85 to be used only by developers. End users should refrain from using these methods to
86 avoid potential breakages.
87
88 =head1 SEE ALSO
89
90 L<Reaction::UI::ViewPort::Collection>
91
92 =head1 AUTHORS
93
94 See L<Reaction::Class> for authors.
95
96 =head1 LICENSE
97
98 See L<Reaction::Class> for the license.
99
100 =cut