Repository navigation
Controllers
To extend Lolita::RestController you should start as usually.
class MyController < Lolita::RestController
#[your code here]
endNext step is to make it recoginze mapping object. Every request to Lolita::RestController will put in enviroment variable (request.env['lolita.mapping']), that is Lolita::Mapping instance. So what you need to do is to create new mapping object or use one of existing ones. Object exists in global mapping Array when it is defined with lolita_for :resources, then you can access it through Lolita.mappings[:resource] ( !NB singular not plural name). Or you can create mapping of your own. Lolita::Mapping.new(:resources) also you may pass options to it (see Lolita::Mapping documentation for details). This will allow Lolita work normaly. Also you may define resource_class and resource_name if you want different behaviour (see Lolita::Controllers::InternalHelpers for details).
####Example
class MyController < Lolita::RestController
#[your code here]
# add this if you use some authorization in Lolita like Pundit or CanCan
def lolita_mapping
Lolita::Mapping.new(:my_resources)
# or Lolita.mappings[:user] if we have lolita_for :users in routes.
end
# this is neccessary only in rare cases, when your mapping can't be converted to appropriate model name.
def resource_class
MyOtherResource
end
# this is neccessary if your mapping name does not match what your forms is sending you, in this case Lolita::RestController will wait for my_resource, but in reality i will receive resource, so i need to changes this.
def resource_name
"resource"
end
endThis example explains how to create your custom controller extended from Lolita::RestController, but there are times when you just need to add one or two action to lolita resource. For example. You have lolita resource :users, but also you want to put :set_as_admin action to it. First you need to define this action in routes. Second you need to extend UsersController with Lolita::RestController. ####Example
# in routes.rb
resources :users, :only => :none do
member do
post :set_as_admin
end
end
lolita_for :users
# in users_controller.rb
class UsersController < Lolita::RestController
def set_as_admin
user = User.find_by_id(params[:id])
user.is_admin = true
user.save!
render :nothing => true, :layout => false
end
def is_lolita_resource?
true
end
endIn this case you don't need to provide mapping for controller if you don't want to use components and views and other Lolita::RestController specific logic. Here I just render nothing and let JS or some other caller to know that request successfully completed. But nothing will changed if I would put lolita_mapping and returned Lolita.mappings[:user], it could been even better, because in that case I would have access to all views/controllers functionality of Lolita::Controllers
When you want to create custom page, for example, "Help" to add some info for client about how to use CMS or some server information page.
Create controller app/controllers/lolita/help_controller.rb with content:
class Lolita::HelpController < Lolita::RestController
def index
end
endThen add template with your HTML:
#content
.box
.boxtitle
%h1.black Help
.arrow
%form
%h2 Some text to help users to use CMSAnd finaly add route to routes.rb
lolita_for :help, :controller => "lolita/help", :title => "Help"