diff options
author | wreis <wreis@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7> | 2009-07-31 12:04:55 +0000 |
---|---|---|
committer | wreis <wreis@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7> | 2009-07-31 12:04:55 +0000 |
commit | 463e51d0379bb139a9d5de856756b21655e6a259 (patch) | |
tree | ed5d67efb1a34a4ed7f3dc5c6e81882d3aad5121 | |
parent | 372fcc324af7c081194fe09ae72d4607d6a370ac (diff) | |
download | reaction-463e51d0379bb139a9d5de856756b21655e6a259.tar.gz reaction-463e51d0379bb139a9d5de856756b21655e6a259.zip |
factoring out for field args from Collection::Grid
-rw-r--r-- | lib/Reaction/UI/ViewPort/Collection/Grid.pm | 76 | ||||
-rw-r--r-- | lib/Reaction/UI/ViewPort/Role/FieldArgs.pm | 133 |
2 files changed, 134 insertions, 75 deletions
diff --git a/lib/Reaction/UI/ViewPort/Collection/Grid.pm b/lib/Reaction/UI/ViewPort/Collection/Grid.pm index eb10d3d..d24b96e 100644 --- a/lib/Reaction/UI/ViewPort/Collection/Grid.pm +++ b/lib/Reaction/UI/ViewPort/Collection/Grid.pm @@ -9,24 +9,7 @@ use namespace::clean -except => [ qw(meta) ]; use MooseX::Types::Moose qw/ArrayRef HashRef Int/; extends 'Reaction::UI::ViewPort::Collection'; -has field_order => ( is => 'ro', isa => ArrayRef, lazy_build => 1); -has excluded_fields => ( is => 'ro', isa => ArrayRef, lazy_build => 1); -has included_fields => ( is => 'ro', isa => ArrayRef, lazy_build => 1); -has computed_field_order => (is => 'ro', isa => ArrayRef, lazy_build => 1); - -has _raw_field_labels => ( - is => 'rw', - isa => HashRef, - init_arg => 'field_labels', - default => sub { {} }, -); - -has field_labels => ( - is => 'ro', - isa => HashRef, - lazy_build => 1, - init_arg => undef, -); +with 'Reaction::UI::ViewPort::Role::FieldArgs'; has member_action_count => ( is => 'rw', @@ -43,65 +26,8 @@ has member_action_count => ( }, ); -#################################### sub _build_member_class { Member }; -sub _build_field_labels { - my $self = shift; - my %labels = %{$self->_raw_field_labels}; - for my $field ( @{$self->computed_field_order}) { - next if defined $labels{$field}; - $labels{$field} = join(' ', map{ ucfirst } split('_', $field)); - } - return \%labels; -} - -sub _build_field_order { []; } - -sub _build_excluded_fields { []; } - -sub _build_included_fields { [] } - -#this is a total clusterfuck and it sucks we should just eliminate it and have -# the grid members not render ArrayRef or Collection fields -sub _build_computed_field_order { - my ($self) = @_; - my %excluded = map { $_ => undef } @{ $self->excluded_fields }; - my %included = map { $_ => undef } @{ $self->included_fields }; - #treat _$field_name as private and exclude fields with no reader - my @names = grep { $_ !~ /^_/ && (!%included || exists( $included{$_}) ) - && !exists($excluded{$_})} map { $_->name } - grep { - !($_->has_type_constraint && - ($_->type_constraint->is_a_type_of('ArrayRef') || - eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} || - eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') } - ) - ) } - grep { defined $_->get_read_method } - $self->current_collection->member_type->parameter_attributes; - - return $self->sort_by_spec($self->field_order, \@names); -} - -around _build_members => sub { - my $orig = shift; - my $self = shift; - $self->member_args->{computed_field_order} ||= $self->computed_field_order; - $self->member_args->{computed_action_order} ||= []; - my $members = $self->$orig(@_); - - # cache everything yo - for my $member (@$members){ - $member->clear_computed_action_order; - my $order = $member->computed_action_order; - @{ $self->member_args->{computed_action_order} } = @$order; - last; - } - - return $members; -}; - __PACKAGE__->meta->make_immutable; diff --git a/lib/Reaction/UI/ViewPort/Role/FieldArgs.pm b/lib/Reaction/UI/ViewPort/Role/FieldArgs.pm new file mode 100644 index 0000000..bff31c2 --- /dev/null +++ b/lib/Reaction/UI/ViewPort/Role/FieldArgs.pm @@ -0,0 +1,133 @@ +package Reaction::UI::ViewPort::Role::FieldArgs; + +use Reaction::Role; +use namespace::clean -except => [ qw(meta) ]; + +has field_order => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1); +has excluded_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1); +has included_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1); +has computed_field_order => (is => 'ro', isa => 'ArrayRef', lazy_build => 1); + +has _raw_field_labels => ( + is => 'rw', + isa => 'HashRef', + init_arg => 'field_labels', + default => sub { {} }, +); + +has field_labels => ( + is => 'ro', + isa => 'HashRef', + lazy_build => 1, + init_arg => undef, +); + +sub _build_field_labels { + my $self = shift; + my %labels = %{$self->_raw_field_labels}; + for my $field ( @{$self->computed_field_order}) { + next if defined $labels{$field}; + $labels{$field} = join(' ', map{ ucfirst } split('_', $field)); + } + return \%labels; +} + +sub _build_field_order { [] } + +sub _build_excluded_fields { [] } + +sub _build_included_fields { [] } + +#this is a total clusterfuck and it sucks we should just eliminate it and have +# the grid members not render ArrayRef or Collection fields +sub _build_computed_field_order { + my ($self) = @_; + my %excluded = map { $_ => undef } @{ $self->excluded_fields }; + my %included = map { $_ => undef } @{ $self->included_fields }; + #treat _$field_name as private and exclude fields with no reader + my @names = grep { $_ !~ /^_/ && (!%included || exists( $included{$_}) ) + && !exists($excluded{$_})} map { $_->name } + grep { + !($_->has_type_constraint && + ($_->type_constraint->is_a_type_of('ArrayRef') || + eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} || + eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') } + ) + ) } + grep { defined $_->get_read_method } + $self->current_collection->member_type->parameter_attributes; + + return $self->sort_by_spec($self->field_order, \@names); +} + +around _build_members => sub { + my $orig = shift; + my $self = shift; + $self->member_args->{computed_field_order} ||= $self->computed_field_order; + $self->member_args->{computed_action_order} ||= []; + my $members = $self->$orig(@_); + + # cache everything yo + for my $member (@$members){ + $member->clear_computed_action_order; + my $order = $member->computed_action_order; + @{ $self->member_args->{computed_action_order} } = @$order; + last; + } + + return $members; +}; + +1; + +__END__; + +=head1 NAME + +Reaction::UI::ViewPort::Role::FieldOptions + +=head1 DESCRIPTION + +=head1 ATTRIBUTES + +=head2 field_order + +=head2 excluded_fields + +List of field names to exclude. + +=head2 included_fields + +List of field names to include. If both C<included_fields> and +C<excluded_fields> are specified the result is those fields which +are in C<included_fields> and not in C<excluded_fields>. + +=head2 included_fields + +List of field names to include. If both C<included_fields> and +C<excluded_fields> are specified the result is those fields which +are in C<included_fields> and not in C<excluded_fields>. + +=head2 field_labels + +=head2 computed_field_order + +=head1 INTERNAL METHODS + +These methods, although stable, are subject to change without notice. These are meant +to be used only by developers. End users should refrain from using these methods to +avoid potential breakages. + +=head1 SEE ALSO + +L<Reaction::UI::ViewPort::Collection> + +=head1 AUTHORS + +See L<Reaction::Class> for authors. + +=head1 LICENSE + +See L<Reaction::Class> for the license. + +=cut |