aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/ViewPort/ObjectView.pm
blob: e33ba5df8e0813cc42262ea1d7a34ae6ae5c0ec7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package Reaction::UI::ViewPort::ObjectView;

use Reaction::Class;

use aliased 'Reaction::UI::ViewPort::DisplayField::Text';
use aliased 'Reaction::UI::ViewPort::DisplayField::Number';
use aliased 'Reaction::UI::ViewPort::DisplayField::Boolean';
use aliased 'Reaction::UI::ViewPort::DisplayField::String';
use aliased 'Reaction::UI::ViewPort::DisplayField::DateTime';
use aliased 'Reaction::UI::ViewPort::DisplayField::RelatedObject';
use aliased 'Reaction::UI::ViewPort::DisplayField::List';
use aliased 'Reaction::UI::ViewPort::DisplayField::Collection';

class ObjectView is 'Reaction::UI::ViewPort', which {
  has object => (
    isa => 'Reaction::InterfaceModel::Object', is => 'ro', required => 1
  );

  has field_names => (isa => 'ArrayRef', is => 'rw', lazy_build => 1);

  has _field_map => (
    isa => 'HashRef', is => 'rw', init_arg => 'fields',
    predicate => '_has_field_map', set_or_lazy_build('field_map'),
  );

  has exclude_fields =>
      ( is => 'rw', isa => 'ArrayRef', required => 1, default => sub{ [] } );

  sub fields { shift->_field_map }

  implements BUILD => as {
    my ($self, $args) = @_;
    unless ($self->_has_field_map) {
      my @field_map;
      my $object = $self->object;
      my %excluded = map{$_ => 1} @{$self->exclude_fields};
      for my $attr (grep { !$excluded{$_->name} } $object->parameter_attributes) {
        push(@field_map, $self->build_fields_for($attr => $args));
      }

      my %field_map = @field_map;
      my @field_names = @{ $self->sort_by_spec(
          $args->{column_order}, [keys %field_map] )};

      $self->_field_map(\%field_map);
      $self->field_names(\@field_names);
    }
  };

  implements build_fields_for => as {
    my ($self, $attr, $args) = @_;
    my $attr_name = $attr->name;
    my $builder = "build_fields_for_name_${attr_name}";
    my @fields;
    if ($self->can($builder)) {
      @fields = $self->$builder($attr, $args); # re-use coderef from can()
    } elsif ($attr->has_type_constraint) {
      my $constraint = $attr->type_constraint;
      my $base_name = $constraint->name;
      my $tried_isa = 0;
      CONSTRAINT: while (defined($constraint)) {
        my $name = $constraint->name;
        if (eval { $name->can('meta') } && !$tried_isa++) {
          foreach my $class ($name->meta->class_precedence_list) {
            my $mangled_name = $class;
            $mangled_name =~ s/:+/_/g;
            my $builder = "build_fields_for_type_${mangled_name}";
            if ($self->can($builder)) {
              @fields = $self->$builder($attr, $args);
              last CONSTRAINT;
            }
          }
        }
        if (defined($name)) {
          unless (defined($base_name)) {
            $base_name = "(anon subtype of ${name})";
          }
          my $mangled_name = $name;
          $mangled_name =~ s/:+/_/g;
          my $builder = "build_fields_for_type_${mangled_name}";
          if ($self->can($builder)) {
            @fields = $self->$builder($attr, $args);
            last CONSTRAINT;
          }
        }
        $constraint = $constraint->parent;
      }
      if (!defined($constraint)) {
        confess "Can't build field ${attr_name} of type ${base_name} without $builder method or build_fields_for_type_<type> method for type or any supertype";
      }
    } else {
      confess "Can't build field ${attr} without $builder method or type constraint";
    }
    return @fields;
  };

  implements build_field_map => as {
    confess "Lazy field map building not supported by default";
  };

  implements build_simple_field => as {
    my ($self, $class, $attr, $args) = @_;
    my $attr_name = $attr->name;
    my %extra;
    if (my $config = $args->{Field}{$attr_name}) {
      %extra = %$config;
    }
    my $field = $class->new(
                  object => $self->object,
                  attribute => $attr,
                  name => $attr->name,
                  location => join('-', $self->location, 'field', $attr->name),
                  ctx => $self->ctx,
                  %extra
                );
    return ($attr_name => $field);
  };

  implements build_fields_for_type_Num => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(Number, $attr, $args);
  };

  implements build_fields_for_type_Int => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(Number, $attr, $args);
  };

  implements build_fields_for_type_Bool => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(Boolean, $attr, $args);
  };

  implements build_fields_for_type_Password => as { return };

  implements build_fields_for_type_Str => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(String, $attr, $args);
  };

  implements build_fields_for_type_SimpleStr => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(String, $attr, $args);
  };

  implements build_fields_for_type_DateTime => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(DateTime, $attr, $args);
  };

  implements build_fields_for_type_Enum => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(String, $attr, $args);
  };


  implements build_fields_for_type_ArrayRef => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(List, $attr, $args)
  };

  #todo dirty hack need generic collection object
  #if a collection wasnt a resultset that'd be good.
  implements build_fields_for_type_Reaction_InterfaceModel_DBIC_Collection => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(Collection, $attr, $args)
  };

  implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(RelatedObject, $attr, $args);
  };


  no Moose;

  no strict 'refs';
  delete ${__PACKAGE__ . '::'}{inner};

};

1;