| 1 | package Valiant::HTML::Tag; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | |||
| 5 | has 'model_name' => (is=>'ro', coerce=>sub { $_[0]=~s/\[\]$// || $_[0]=~s/\[\]\]$/]/; $_[0] }, required=>1); | ||
| 6 | has 'method_name' => (is=>'ro', required=>1); | ||
| 7 | has 'view' => (is=>'ro', required=>1); | ||
| 8 | has 'options' => (is=>'ro', required=>1, default=>sub { +{} }); | ||
| 9 | |||
| 10 | has 'model' => ( | ||
| 11 | is=>'ro', | ||
| 12 | required => 1, | ||
| 13 | lazy => 1, | ||
| 14 | builder => '_build_model', | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub _build_model { | ||
| 18 | my $self = shift; | ||
| 19 | $self->_retrieve_object(delete $self->options->{model}); | ||
| 20 | } | ||
| 21 | |||
| 22 | has 'skip_default_ids' => ( | ||
| 23 | is=>'ro', | ||
| 24 | required => 1, | ||
| 25 | lazy => 1, | ||
| 26 | builder => '_skip_default_ids', | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub _skip_default_ids { return delete($_[0]->options->{skip_default_ids}) ? 1:0 } | ||
| 30 | |||
| 31 | |||
| 32 | has 'allow_method_names_outside_object' => ( | ||
| 33 | is=>'ro', | ||
| 34 | required => 1, | ||
| 35 | lazy => 1, | ||
| 36 | builder => '_allow_method_names_outside_object', | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub _allow_method_names_outside_object { return delete($_[0]->options->{allow_method_names_outside_object}) ? 1:0 } | ||
| 40 | |||
| 41 | |||
| 42 | sub _retrieve_object { | ||
| 43 | my ($self, $object) = @_; | ||
| 44 | if ($object) { | ||
| 45 | return $object; | ||
| 46 | } elsif ($self->view->can("read_attribute_for_view")) { | ||
| 47 | $object = $self->view->read_attribute_for_view($self->model_name); | ||
| 48 | return $object if defined($object); | ||
| 49 | die "Can't find model '@{[ $self->model_name ]}' in options or in view."; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | =head1 NAME | ||
| 56 | |||
| 57 | Valiant::HTML::Tag - Base class for tag objects | ||
| 58 | |||
| 59 | =head1 SYNOPSIS | ||
| 60 | |||
| 61 | TBD | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | TBD | ||
| 66 | |||
| 67 | =head1 CLASS METHODS | ||
| 68 | |||
| 69 | This class exposes the folllowing class methods | ||
| 70 | |||
| 71 | =head1 ATTRIBUTES | ||
| 72 | |||
| 73 | This class has the following initialization attributes | ||
| 74 | |||
| 75 | =head2 skip_default_ids | ||
| 76 | |||
| 77 | Defaults to false. Generally we create an html C<id> attribute for the field based | ||
| 78 | on a convention which includes the model name, index and method name. Setting this | ||
| 79 | to true prevents that so you should set C<id> manually unless you don't want them. | ||
| 80 | Please note that even if this is false, you can always override the C<id> on a per | ||
| 81 | field basis by setting it manually. | ||
| 82 | |||
| 83 | =head2 allow_method_names_outside_object | ||
| 84 | |||
| 85 | Default is false. Generally we expect C<method_name> to be an actual method on the | ||
| 86 | C<model> and if its not we expect an exception. This helps to prevent typos from | ||
| 87 | leading to unexpected results. However sometimes you may wish create a form field that | ||
| 88 | has a name that isn't on the model but still respects the current namespace and index. | ||
| 89 | These names would appear in the POST request body and could be used for things other than | ||
| 90 | updating or creating a model. | ||
| 91 | <D-c><D-c><D-c> | ||
| 92 | |||
| 93 | =head1 INSTANCE METHODS | ||
| 94 | |||
| 95 | This class exposes the folllowing instance methods | ||
| 96 | |||
| 97 | |||
| 98 | =head1 SEE ALSO | ||
| 99 | |||
| 100 | L<Valiant>, L<Valiant::HTML::FormBuilder> | ||
| 101 | |||
| 102 | =head1 AUTHOR | ||
| 103 | |||
| 104 | See L<Valiant> | ||
| 105 | |||
| 106 | =head1 COPYRIGHT & LICENSE | ||
| 107 | |||
| 108 | See L<Valiant> | ||
| 109 | |||
| 110 | =cut |