add index/collections/paging. overriding most of snorlax now |:)
This commit is contained in:
parent
8375b32915
commit
7117068d34
5 changed files with 94 additions and 5 deletions
|
@ -7,7 +7,7 @@ module Api
|
||||||
snorlax_used_rest!
|
snorlax_used_rest!
|
||||||
|
|
||||||
load_and_authorize_resource only: [:show, :update, :destroy]
|
load_and_authorize_resource only: [:show, :update, :destroy]
|
||||||
|
|
||||||
def create
|
def create
|
||||||
instantiate_resource
|
instantiate_resource
|
||||||
resource.user = current_user
|
resource.user = current_user
|
||||||
|
@ -34,6 +34,18 @@ module Api
|
||||||
"Api::V1::#{resource_name.camelize}Serializer".constantize
|
"Api::V1::#{resource_name.camelize}Serializer".constantize
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def respond_with_resource(scope: default_scope, serializer: resource_serializer, root: serializer_root)
|
||||||
|
if resource.errors.empty?
|
||||||
|
render json: resource, scope: scope, serializer: serializer, root: root
|
||||||
|
else
|
||||||
|
respond_with_errors
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def respond_with_collection(resources: collection, scope: default_scope, serializer: resource_serializer, root: serializer_root)
|
||||||
|
render json: resources, scope: scope, each_serializer: serializer, root: root, meta: pagination(resources), meta_key: :page
|
||||||
|
end
|
||||||
|
|
||||||
def default_scope
|
def default_scope
|
||||||
{
|
{
|
||||||
embeds: embeds
|
embeds: embeds
|
||||||
|
@ -59,6 +71,40 @@ module Api
|
||||||
def permitted_params
|
def permitted_params
|
||||||
@permitted_params ||= PermittedParams.new(params)
|
@permitted_params ||= PermittedParams.new(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def serializer_root
|
||||||
|
'data'
|
||||||
|
end
|
||||||
|
|
||||||
|
def pagination(collection)
|
||||||
|
per = (params[:per] || 25).to_i
|
||||||
|
current_page = params[:page].to_i
|
||||||
|
total_pages = (collection.total_count / per).to_i + 1
|
||||||
|
prev_page = current_page > 1 ? current_page - 1 : 0
|
||||||
|
next_page = current_page < total_pages ? current_page + 1 : 0
|
||||||
|
{
|
||||||
|
current_page: current_page,
|
||||||
|
next_page: next_page,
|
||||||
|
prev_page: prev_page,
|
||||||
|
total_pages: total_pages,
|
||||||
|
total_count: collection.total_count
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def instantiate_collection
|
||||||
|
collection = accessible_records
|
||||||
|
collection = yield collection if block_given?
|
||||||
|
collection = collection.page(params[:page]).per(params[:per])
|
||||||
|
self.collection = collection
|
||||||
|
end
|
||||||
|
|
||||||
|
def visible_records
|
||||||
|
policy_scope(resource_class)
|
||||||
|
end
|
||||||
|
|
||||||
|
def public_records
|
||||||
|
policy_scope(resource_class)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
32
app/serializers/api/v1/embeddable.rb
Normal file
32
app/serializers/api/v1/embeddable.rb
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
module Api
|
||||||
|
module V1
|
||||||
|
module Embeddable
|
||||||
|
def self.embeddable
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
|
||||||
|
def embeds
|
||||||
|
@embeds ||= (scope[:embeds] || []).select { |e| self.class.embeddable.keys.include?(e) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.inherited(child)
|
||||||
|
child.class_eval do
|
||||||
|
embeddable.each_pair do |key, opts|
|
||||||
|
attr = opts.delete(:attr) || key
|
||||||
|
binding.pry if key == :topic2
|
||||||
|
if attr.to_s.pluralize == attr.to_s
|
||||||
|
attribute "#{attr.to_s.singularize}_ids".to_sym, opts.merge(unless: -> { embeds.include?(key) }) do
|
||||||
|
object.send(attr).map(&:id)
|
||||||
|
end
|
||||||
|
has_many attr, opts.merge(if: -> { embeds.include?(key) })
|
||||||
|
else
|
||||||
|
id_opts = opts.merge(key: "#{key}_id")
|
||||||
|
attribute "#{attr}_id".to_sym, id_opts.merge(unless: -> { embeds.include?(key) })
|
||||||
|
attribute key, opts.merge(if: -> { embeds.include?(key) })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
1
config/initializers/active_model_serializers.rb
Normal file
1
config/initializers/active_model_serializers.rb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
ActiveModelSerializers.config.adapter = :json
|
10
config/initializers/kaminari_config.rb
Normal file
10
config/initializers/kaminari_config.rb
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
Kaminari.configure do |config|
|
||||||
|
# config.default_per_page = 25
|
||||||
|
# config.max_per_page = nil
|
||||||
|
# config.window = 4
|
||||||
|
# config.outer_window = 0
|
||||||
|
# config.left = 0
|
||||||
|
# config.right = 0
|
||||||
|
# config.page_method_name = :page
|
||||||
|
# config.param_name = :page
|
||||||
|
end
|
|
@ -11,10 +11,10 @@ Metamaps::Application.routes.draw do
|
||||||
|
|
||||||
namespace :api, path: '/api', default: { format: :json } do
|
namespace :api, path: '/api', default: { format: :json } do
|
||||||
namespace :v1, path: '/v1' do
|
namespace :v1, path: '/v1' do
|
||||||
resources :maps, only: [:create, :show, :update, :destroy]
|
resources :maps, only: [:index, :create, :show, :update, :destroy]
|
||||||
resources :synapses, only: [:create, :show, :update, :destroy]
|
resources :synapses, only: [:index, :create, :show, :update, :destroy]
|
||||||
resources :topics, only: [:create, :show, :update, :destroy]
|
resources :topics, only: [:index, :create, :show, :update, :destroy]
|
||||||
resources :mappings, only: [:create, :show, :update, :destroy]
|
resources :mappings, only: [:index, :create, :show, :update, :destroy]
|
||||||
resources :tokens, only: [:create, :destroy] do
|
resources :tokens, only: [:create, :destroy] do
|
||||||
get :my_tokens, on: :collection
|
get :my_tokens, on: :collection
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue