- if (grep { $_ eq "name" } $child->columns) { # Common case
- $child->columns( Stringify => qw/ name / );
- } # Otherwise, work it out for yourself.
-}
-
-sub search :Exported {
- return shift->SUPER::search(@_) if caller ne "Maypole::Model::Base";
- # A real CDBI search.
- my ($self, $r) = @_;
- my %fields = map {$_ => 1 } $self->columns;
- my $oper = "like"; # For now
- use Carp; Carp::confess("Urgh") unless ref $r;
- my %params = %{$r->{params}};
- my %values = map { $_ => {$oper, $params{$_} } }
- grep { $params{$_} and $fields{$_} } keys %params;
-
- $r->objects([ %values ? $self->search_where(%values) : $self->retrieve_all ]);
- $r->template("list");
- $r->{template_args}{search} = 1;
+ if ( my $col = $child->stringify_column ) {
+ $child->columns( Stringify => $col );
+ }
+}
+
+=head2 is_class
+
+Tell if action is a class method (See Maypole::Plugin::Menu)
+
+=cut
+
+sub is_class {
+ my ( $self, $method, $attrs ) = @_;
+ die "Usage: method must be passed as first arg" unless $method;
+ $attrs = join(' ',$self->method_attrs($method)) unless ($attrs);
+ return 1 if $attrs =~ /\bClass\b/i;
+ return 1 if $method =~ /^list$/; # default class actions
+ return 0;
+}
+
+=head2 is_object
+
+Tell if action is a object method (See Maypole::Plugin::Menu)
+
+=cut
+
+sub is_object {
+ my ( $self, $method, $attrs ) = @_;
+ die "Usage: method must be passed as first arg" unless $method;
+ $attrs = join(' ',$self->method_attrs($method)) unless ($attrs);
+ return 1 if $attrs =~ /\bObject\b/i;
+ return 1 if $method =~ /(^view$|^edit$|^delete$)/; # default object actions
+ return 0;
+}
+
+
+=head2 related
+
+This method returns a list of has-many accessors. A brewery has many
+beers, so C<BeerDB::Brewery> needs to return C<beers>.
+
+=cut
+
+sub related {
+ my ( $self, $r ) = @_;
+ return keys %{ $self->meta_info('has_many') || {} };
+}
+
+
+=head2 related_class
+
+Given an accessor name as a method, this function returns the class this accessor returns.
+
+=cut
+
+sub related_class {
+ my ( $self, $r, $accessor ) = @_;
+ my $meta = $self->meta_info;
+ my @rels = keys %$meta;
+ my $related;
+ foreach (@rels) {
+ $related = $meta->{$_}{$accessor};
+ last if $related;
+ }
+ return unless $related;
+
+ my $mapping = $related->{args}->{mapping};
+ if ( $mapping and @$mapping ) {
+ return $related->{foreign_class}->meta_info('has_a')->{$$mapping[0]}->{foreign_class};
+ }
+ else {
+ return $related->{foreign_class};
+ }
+ }
+
+=head2 isa_class
+
+Returns class of a column inherited by is_a, assumes something can be more than one thing (have * is_a rels)
+
+=cut
+
+sub isa_class {
+ my ($class, $col) = @_;
+ $class->_croak( "Need a column for isa_class." ) unless $col;
+ my $isaclass;
+ # class col is first found in is returned
+ my $isa = $class->meta_info("is_a") || {};
+ foreach ( keys %$isa ) {
+ $isaclass = $isa->{$_}->foreign_class;
+ return $isaclass if ($isaclass->find_column($col));
+ }
+ return 0; # col not in a is_a class
+}
+
+=head2 accessor_classes
+
+Returns hash ref of classes for accessors.
+
+This is an attempt at a more efficient method than calling "related_class()"
+a bunch of times when you need it for many relations.
+
+=cut
+
+sub accessor_classes {
+ my ($self, $class) = @_; # can pass a class arg to get accssor classes for
+ $class ||= $self;
+ my $meta = $class->meta_info;
+ my %res;
+ foreach my $rel (keys %$meta) {
+ my $rel_meta = $meta->{$rel};
+ %res = ( %res, map { $_ => $rel_meta->{$_}->{foreign_class} }
+ keys %$rel_meta );
+ }
+ return \%res;
+
+ # 2 liner to get class of accessor for $name
+ #my $meta = $class->meta_info;
+ #my ($isa) = map $_->foreign_class, grep defined,
+ # map $meta->{$_}->{$name}, keys %$meta;
+
+}
+
+
+=head2 stringify_column
+
+=cut
+
+sub stringify_column {
+ my $class = shift;
+ return (
+ $class->columns("Stringify"),
+ ( grep { /^(name|title)$/i } $class->columns ),
+ ( grep { /(name|title)/i } $class->columns ),
+ ( grep { !/id$/i } $class->primary_columns ),
+ )[0];