1 package Reaction::UI::ViewPort::GridView;
5 use aliased 'Reaction::InterfaceModel::Collection' => 'IM_Collection';
6 use aliased 'Reaction::UI::ViewPort::GridView::Entity';
8 class GridView is 'Reaction::UI::ViewPort', which {
10 has exclude_fields => ( isa => 'ArrayRef', is => 'ro' );
11 has field_order => ( isa => 'ArrayRef', is => 'ro', lazy_build => 1);
12 has field_labels => ( isa => 'HashRef', is => 'ro', lazy_build => 1);
15 has entities => ( isa => 'ArrayRef', is => 'rw', lazy_build => 1);
17 has collection => (isa => IM_Collection, is => 'ro', required => 1);
18 has current_collection => (isa => IM_Collection, is => 'rw', lazy_build => 1);
20 has entity_class => ( isa => 'Str', is => 'rw', lazy_build => 1);
21 has entity_args => ( is => 'rw' );
23 implements BUILD => as {
24 my ($self, $args) = @_;
25 my $entity_args = delete $args->{Entity};
26 $self->entity_args( $entity_args ) if ref $entity_args;
29 after clear_current_collection => sub{
30 shift->clear_entities; #clear the entitiesis the current collection changes, duh
33 implements _build_entity_class => as { Entity };
35 implements _build_field_order => as {
37 my %excluded = map { $_ => undef }
38 @{ $self->has_exclude_fields ? $self->exclude_fields : [] };
39 #XXX this abuse of '_im_class' needs to be fixed ASAP
40 my $object_class = $self->collection->_im_class;
41 my @fields = $object_class->meta->parameter_attributes;
42 #obviously only get fields with readers.
43 @fields = grep { $_->get_read_method } @fields;
44 #eliminate excluded fields & treat names that start with an underscore as private
45 @fields = grep {$_->name !~ /^_/ && !exists $excluded{$_->name} } @fields;
47 #eliminate fields marked as collections, or fields that are arrayrefs
49 !($_->has_type_constraint &&
50 ($_->type_constraint->is_a_type_of('ArrayRef') ||
51 eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} ||
52 eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') }
56 #order the columns all nice and pretty, and only get fields with readers, duh
57 my $ordered = $self->sort_by_spec
58 ( $self->column_order, [ map { (($_->name) || ()) } @fields] );
63 implements _build_current_collection => as {
67 implements _build_field_labels => as {
70 for my $field ( @{$self->field_order}){
71 $labels{$field} = join(' ', map{ ucfirst } split('_', $field));
76 implements _build_entities => as {
79 my $args = $self->has_entity_args ? $self->entity_args : {};
82 my $loc = $self->location;
83 my $order = $self->field_order;
84 my $class = $self->entity_class;
85 for my $obj ( $self->current_collection->members ) {
86 my $row = $class->new(
89 location => join('-', $loc, 'row', $i++),
90 field_order => $order,
91 builder_cache => $builders,
92 ref $args ? %$args : ()
94 push(@entities, $row);