NAME

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

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';

ATTRIBUTES

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

description

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

generator

Returns the concatenated moniker, VERSION and CODENAME.

keywords

Returns default keywords used in head element of HTML pages.

SUBROUTINES/METHODS

Methods shared among subclasses and in hooks

config

Overwrites the default helper "config" in Mojolicious::Plugin::DefaultHelpers which is actually an alias for "config" in Mojo. Returns configuration specific to the 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->app->config('key').

debug

A shortcut to:

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

list_for_json

Prepares a structure suitable for rendering as JSON for listing an ARRAYref of HASHES or Ado::Model* objects, returned by "select_range" in Ado::Model and returns it. Accepts two ARRAY references and one arbitrary HASH reference as parameters:

my $res = $c->list_for_json([$limit, $offset], \@list_of_AMobjects_or_hashes, $meta);

Use this method to ensure uniform and predictable representation across all listing resources. Use the $meta key for arbitrary metadata, specific to your resource. See http://127.0.0.1:3000/ado-users/list.json for example output and "list" in Ado::Control::Ado::Users for the example source.

my @range = ($c->param('limit') || 10, $c->param('offset') || 0);
return $c->respond_to(
  json => $c->list_for_json(\@range, [Ado::Model::Users->select_range(@range)],{foo=>bar})
);
Outputs:
{
  links => [{href=>$url, rel=>'self'},{...}],
  data=>[..],
  query=>{limit=>10, offset=>0},
  meta=> {foo=>'bar'}
}

return $c->respond_to(
  json => $c->list_for_json(\@range, [$dbix->query($SQL,@range)->hashes])
);

require_formats

Checks for a list of accepted formats or renders "415 - Unsupported Media Type" with a text/html type and links to the preferred formats, and returns false. If the URL is in the required format, returns true. Adds a header C <Content-Location> pointing to the first URL of the required formats.

#in an action serving only json
sub list {
  my $c = shift;
  $c->require_formats('json') || return;
  $c->debug('rendering json only');
    #your stuff here...
    return;
}

This method exists only to show more descriptive message with available formats to the end user and to give a chance to user agents to go to the preferred resource URL.

validate_input

Uses "validation" in Mojolicious::Controller to validate all input parameters at once given a validation template. The template consists of keys matching the input parameters to be validated. The values are HASH references describing the rules. Each rule name corresponds to a method/check in "CHECKS" in Mojolicious::Validator. You can use your own checks if you add them using "add_check" in Mojolicious::Validator.

Returns a HASH reference. In case of errors it contains errors and json HASH references. In case of success contains only output HASH reference from "output" in Mojolicious::Validator::Validation.

my $rules = {
    to_uid => {
        'required' => 1, like => qr/^\d{1,20}$/
    },
    subject => {
        'required' => 1, like => qr/^.{1,255}$/
    },
    #...
}
my $result = $c->validate_input($rules);

#400 Bad Request
return $c->render(
    status => 400,
    json   => $result->{json}
) if $result->{errors};

user

Returns the current user. This is the user guest for not authenticated users. Note that this instance is not meant for manipulation and some fields are not available for security reasons. The fields are: login_password created_by changed_by disabled start_date. TODO: move as much as possible checks and fields retrieval in SQL, not in Perl.

$c->user(Ado::Model::Users->by_login_name($login_name));
my $names = $c->user->name;

SEE ALSO

Mojolicious::Controller, Ado::Manual::Controllers, Ado::Manual::RESTAPI, DBIx::Simple

AUTHOR

Красимир Беров (Krasimir Berov)

COPYRIGHT AND LICENSE

Copyright 2013-2014 Красимир Беров (Krasimir Berov).

This program is free software, you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License v3 (LGPL-3.0). You may copy, distribute and modify the software provided that modifications are open source. However, software that includes the license may release under a different license.

See http://opensource.org/licenses/lgpl-3.0.html for more information.