r31500@martha (orig r1229): purge | 2009-09-11 05:51:11 -0400
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Role / FieldArgs.pm
CommitLineData
463e51d0 1package Reaction::UI::ViewPort::Role::FieldArgs;
2
3use Reaction::Role;
4use namespace::clean -except => [ qw(meta) ];
5
6has field_order => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
7has excluded_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
8has included_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
9has computed_field_order => (is => 'ro', isa => 'ArrayRef', lazy_build => 1);
10
11has _raw_field_labels => (
12 is => 'rw',
13 isa => 'HashRef',
14 init_arg => 'field_labels',
15 default => sub { {} },
16);
17
18has field_labels => (
19 is => 'ro',
20 isa => 'HashRef',
21 lazy_build => 1,
22 init_arg => undef,
23);
24
25sub _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
35sub _build_field_order { [] }
36
37sub _build_excluded_fields { [] }
38
39sub _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
43sub _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
63around _build_members => sub {
64 my $orig = shift;
65 my $self = shift;
66 $self->member_args->{computed_field_order} ||= $self->computed_field_order;
463e51d0 67 my $members = $self->$orig(@_);
68
463e51d0 69 return $members;
70};
71
721;
73
74__END__;
75
76=head1 NAME
77
78Reaction::UI::ViewPort::Role::FieldOptions
79
80=head1 DESCRIPTION
81
82=head1 ATTRIBUTES
83
84=head2 field_order
85
86=head2 excluded_fields
87
88List of field names to exclude.
89
90=head2 included_fields
91
92List of field names to include. If both C<included_fields> and
93C<excluded_fields> are specified the result is those fields which
94are in C<included_fields> and not in C<excluded_fields>.
95
96=head2 included_fields
97
98List of field names to include. If both C<included_fields> and
99C<excluded_fields> are specified the result is those fields which
100are in C<included_fields> and not in C<excluded_fields>.
101
102=head2 field_labels
103
104=head2 computed_field_order
105
106=head1 INTERNAL METHODS
107
108These methods, although stable, are subject to change without notice. These are meant
109to be used only by developers. End users should refrain from using these methods to
110avoid potential breakages.
111
112=head1 SEE ALSO
113
114L<Reaction::UI::ViewPort::Collection>
115
116=head1 AUTHORS
117
118See L<Reaction::Class> for authors.
119
120=head1 LICENSE
121
122See L<Reaction::Class> for the license.
123
124=cut