Ado

 view release on metacpan or  search on metacpan

lib/Ado/Control.pm  view on Meta::CPAN

        #field
        my $f =
            $checks->{required}
          ? $v->required($param)
          : $v->optional($param);
        foreach my $check (keys %$checks) {
            next if $check eq 'required';
            if (ref $$checks{$check} eq 'HASH') {
                $f->$check(%{$checks->{$check}});
            }
            elsif (ref $$checks{$check} eq 'ARRAY') {
                $f->$check(@{$checks->{$check}});
            }
            else { $f->$check($checks->{$check}) }
        }    #end foreach my $check
        $errors->{$param} = $f->error($param)
          if $f->error($param);

    }    #end foreach my $param

    return {
        (   !!keys %{$errors}
            ? ( errors => $errors,
                json   => {
                    status  => 'error',
                    code    => 400,
                    message => $errors,
                    data    => 'validate_input'
                }
              )
            : (output => $v->output)
        )
    };
}

sub user {
    my ($c, $user) = @_;
    state $delete_fields = [qw(login_password created_by changed_by disabled start_date email)];
    if ($user) {

        # Remove as much as possible user data.
        delete @{$user->data}{@$delete_fields};
        $c->{user} = $user;
        return $c;
    }
    elsif ($c->{user}) {
        return $c->{user};
    }

    # Called for the first time without a $user object.
    # Defaults to current user or Guest.
    $c->{user} = Ado::Model::Users->by_login_name($c->session->{login_name} //= 'guest');
    delete @{$c->{user}->data}{@$delete_fields};
    return $c->{user};
}

1;

=pod

=encoding utf8

=head1 NAME

Ado::Control - The base class for all controllers!

=head1 SYNOPSIS

It must be inherited by all controllers. Put code here only to be shared by
it's subclasses or used in hooks.

  package Ado::Control::Hello;
  use Mojo::Base 'Ado::Control';

=head1 ATTRIBUTES

Ado::Control inherits all attributes from L<Mojolicious::Controller>
and implements the following new ones.

=head2 description

Returns a default description used in C<head> element of HTML pages.

=head2 generator

Returns the concatenated moniker, VERSION and L<CODENAME>.

=head2 keywords

Returns default keywords used in C<head> element of HTML pages.

=head1 SUBROUTINES/METHODS

Methods shared among subclasses and in hooks

=head2 config

Overwrites the default helper L<Mojolicious::Plugin::DefaultHelpers/config>
which is actually an alias for L<Mojo/config>. Returns configuration specific
to the I<current controller> package only.

  #in Ado::Control::List or Ado::Control::Foo or...
  my $myvalue = $c->config('mykey');
  #a shortcut to
  my $myvalue = $app->config(__PACKAGE__)->{mykey}
  ...

To access the application-wide configuration use
C<$c-E<gt>app-E<gt>config('key')>.

=head2 debug

A shortcut to:

  $c->app->log->debug(@_);

=head2 list_for_json

Prepares a structure suitable for rendering as JSON for  listing  an ARRAYref
of HASHES or L<Ado::Model>* objects, returned by L<Ado::Model/select_range>
and returns it. Accepts two C<ARRAY> references  and one arbitrary C<HASH>



( run in 1.156 second using v1.01-cache-2.11-cpan-39bf76dae61 )