Controller (Resourcey::Controller
class) gives you ability to perform REST actions on your resources.
For controller named UsersController
, model is User
by default (it uses controller_name
method from the ActionController::Base
). If you want to change model used by controller, just call use_model
method inside, and pass model class or model name:
class AdminsController < Resourcey::Controller
use_model User
# or
use_model :user
# or
use_model 'User'
end
For controller named UsersController
, serializer is UserSerializer
by default. If you want to change serializer class used by controller, just call serialize_with
method in controller:
class ActiveUsersController < Resourcey::Controller
serialize_with UserSerializer
# also possible:
# serialize_with 'UserSerializer'
# serialize_with :user_serializer
end
If you need different serializers for some actions, you can use configure_serialization
method:
class ActiveUsersController < Resourcey::Controller
configure_serialization do |config|
config.action :index, serializer: UserSerializer::Index
config.action :show, serializer: UserSerializer::Show
# other actions will use serializer from `serialize_with` method, or you can set it
# like this:
config.default serializer: UserSerializer
end
end
By default, Resourcey::Controller
inherits from ActionController::Base
, you can change this behaviour by changing controller_parent
config variable. See examples below:
Resourcey.configure do |config|
config.controller_parent = 'MyCustomController'
# or
config.controller_parent = MyCustomController
end
Sometimes you need to transform your collection (activerecord_relation object) before it's rendered as json. In order to do this, call collection_scope
method in your controller, and pass a block inside (the block will receive original collection scope). Original scope has pagination and filters already applied. See example:
# somewhere, in post.rb
# class Post < ApplicationRecord
# belongs_to :author
# scope :most_recent, -> { where('posts.created_at > ?', 2.days.ago) }
# end
class PostsController < Resourcey::Controller
collection_scope do |scope|
scope.most_recent
end
# or shorter way
collection_scope &:most_recent
# sometimes you need to `include` some relation
collection_scope do |scope|
scope.includes(:author)
end
# ...
end
This method only applies for index
action. If you want to pass parameters to scope method, filters are what you're looking for.
class MostRecentPostsController < Resourcey::Controller
use_model Post
paginate_with :from_to
collection_scope &:most_recent
configure_serialization do |config|
config.action :index, serializer: MostRecentPostSerializer::Index
config.default serializer: MostRecentPostSerializer::Base
end
end