Skip to content

Commit

Permalink
merge devin's changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewalker committed Feb 7, 2012
2 parents 6aec4ee + 3f33518 commit cb7efbc
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 33 deletions.
5 changes: 4 additions & 1 deletion dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ copyright_holder = Devin Austin
[NextRelease]
format = %-5v %{yyyy-MM-dd}d
[CheckChangesHasContent]
[CompileTests]
[Test::Compile]

[Repository]
git_remote = git://github.com/dhoss/Form-Sensible-Reflector-DBIC.git
Expand All @@ -17,6 +17,9 @@ github_http = 0
hash_tags = #form-sensible-reflector #perl
url_shortener = TinyURL

[ChangelogFromGit]
file_name = Changes

[Prereqs]
Moose = 0.93
Form::Sensible = 0.20012
Expand Down
76 changes: 72 additions & 4 deletions lib/Form/Sensible/Reflector/DBIC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,40 @@ For example:
);
__PACKAGE__->set_primary_key('id'); # Defaults to hidden in the form
=head1 WAYS TO REFLECT
=over 4
=item From a ResultSet:
my $reflector = Form::Sensible::Reflector::DBIC->new();
my $form = $reflector->reflect_from( $schema->resultset("Test"),
{
form =>
{
name => 'test'
},
with_trigger => 1
}
);
=item From a Row object:
my $reflector = Form::Sensible::Reflector::DBIC->new();
my $row = $schema->resultset('Herp')->create($derp);
my $form_from_row = $reflector->reflect_from( $row,
{
form =>
{
name => 'test'
},
with_trigger => 1
}
);
=back
=head1 INTERNAL METHODS
=head2 $self->field_type_map
Expand Down Expand Up @@ -221,7 +255,6 @@ Get a given field's definition.
=cut

sub get_field_definition {

my ( $self, $form, $handle, $name ) = @_;
## TODO:
## 1. Follow relationships
Expand All @@ -232,10 +265,45 @@ sub get_field_definition {
my $columninfo = $result_source->column_info($name);
my $is_ai = $columninfo->{is_auto_increment};

## this does the basics of the field definitions including field mapping. Then we
## do some general stuff that applies to ALL field types...
my $definition;

if ( $columninfo->{fs_select_name_column} ) {
$columninfo->{fs_select_value_column} ||= $columninfo->{fs_select_name_column};
}

if ( $columninfo->{fs_select_value_column} ) {
$columninfo->{fs_select_name_column} ||= $columninfo->{fs_select_value_column};
}

if ( $columninfo->{fs_select_name_column} && $columninfo->{fs_select_value_column} ) {
$definition->{field_class} = 'Select';

for ($result_source->relationships) {
my $relationship_info = $result_source->relationship_info($_);
my $cond = $relationship_info->{cond};

if ( grep { $cond->{$_} eq "self.$name" } keys %$cond ) {
my $fs_select_name_column = $columninfo->{fs_select_name_column};
my $fs_select_value_column = $columninfo->{fs_select_value_column};

$relationship_info->{source} =~ /:?(\w+)$/;

my $definition = $self->get_base_definition( $name, $columninfo );
$definition->{options} = [
map {
{
name => $_->$fs_select_name_column,
value => $_->$fs_select_value_column,
}
} $result_source->schema->resultset($1)->all
];

last;
}
}
}
else {
$definition = $self->get_base_definition( $name, $columninfo );
}

$definition->{'validation'} ||= {};

Expand Down
52 changes: 52 additions & 0 deletions lib/Form/Sensible/Reflector/DBIC/Role/FieldClassOptions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,58 @@ package Form::Sensible::Reflector::DBIC::Role::FieldClassOptions;
use Moose::Role;
use namespace::autoclean;

=head1 NAME
Form::Sensible::Reflector::DBIC::Role::FieldClassOptions - Sane defaults for field classes.
=cut


=head1 $self->field_class_options
Hashref of default field class options.
Current incarnation:
'Number' => {
'validation' => {
'integer_only' => 'integer_only',
'upper_bound' => 'upper_bound',
'lower_bound' => 'lower_bound',
'step' => 'step',
},
},
'Toggle' => {
'render_hints' => {
'on_value' => 'on_value',
'on_label' => 'on_label',
'off_value' => 'off_value',
'off_label' => 'off_label',
},
},
'Text' => {
'validation' => {
'size' => 'maximum_length',
'minimum_length' => 'minimum_length',
'maximum_length' => 'maximum_length',
'should_truncate' => 'should_truncate',
},
},
'LongText' => {
'validation' => {
'size' => 'maximum_length',
'minimum_length' => 'minimum_length',
'maximum_length' => 'maximum_length',
'should_truncate' => 'should_truncate',
},
},
'FileSelector' => { 'validation' => { 'size' => 'maximum_size', }, },
'Select' =>
{ 'validation' => { 'options_delegate' => 'options_delegate', }, },
=cut

has 'field_class_options' => (
is => 'rw',
isa => 'HashRef',
Expand Down
64 changes: 64 additions & 0 deletions lib/Form/Sensible/Reflector/DBIC/Role/FieldTypeMap.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,61 @@ package Form::Sensible::Reflector::DBIC::Role::FieldTypeMap;
use Moose::Role;
use namespace::autoclean;

=head1 NAME
Form::Sensible::Reflector::DBIC::Role::FieldTypeMap - Map generic SQL data types to L<Form::Sensible> field types.
=cut

=head1 $self->field_type_map
Define a hashref of field->types.
Current incarnation is as follows:
varchar => { defaults => { field_class => 'Text', }, },
text => { defaults => { field_class => 'LongText', }, },
blob => { defaults => { field_class => 'FileSelector' }, },
datetime => { defaults => { field_class => 'Text', }, },
enum => { defaults => { field_class => 'Select', }, },
int => {
defaults => {
field_class => 'Number',
integer_only => 1,
},
},
integer => {
defaults => {
field_class => 'Number',
integer_only => 1,
},
},
bigint => {
defaults => {
field_class => 'Number',
integer_only => 1,
},
},
bool => {
defaults => {
field_class => 'Toggle',
on_value => 1,
on_label => 'yes',
off_value => 0,
off_label => 'no'
},
},
decimal => {
defaults => {
field_class => 'Number',
integer_only => 0,
},
},
};
=cut

has field_type_map => (
is => 'rw',
isa => 'HashRef',
Expand Down Expand Up @@ -52,4 +107,13 @@ sub _build_field_type_map {
};
}


=head1 AUTHOR
Devin Austin L<mailto:devin.austin@ionzero.com>
Andre Walker L<http://andrewalker.net>
=cut

1;
Loading

0 comments on commit cb7efbc

Please sign in to comment.