app/controllers/apicasso/crud_controller.rb in apicasso-0.4.11 vs app/controllers/apicasso/crud_controller.rb in apicasso-0.5.0
- old
+ new
@@ -1,14 +1,13 @@
# frozen_string_literal: true
module Apicasso
# Controller to consume read-only data to be used on client's frontend
class CrudController < Apicasso::ApplicationController
- before_action :set_root_resource
before_action :set_object, except: %i[index create schema]
before_action :set_nested_resource, only: %i[nested_index]
- before_action :set_records, only: %i[index nested_index]
+ before_action :set_records, only: %i[index]
include Orderable
# GET /:resource
# Returns a paginated, ordered and filtered query based response.
# Consider this
# To get all `Channel` sorted by ascending `name` , filtered by
@@ -79,35 +78,20 @@
render json: resource_schema.to_json unless preflight?
end
private
- # Common setup to stablish which model is the resource of this request
- def set_root_resource
- @root_resource = params[:resource].classify.constantize
- end
-
# Common setup to stablish which object this request is querying
def set_object
id = params[:id]
@object = resource.friendly.find(id)
rescue NoMethodError
@object = resource.find(id)
ensure
- authorize! :read, @object
+ authorize! action_name.to_sym, @object
end
- # Setup to stablish the nested model to be queried
- def set_nested_resource
- @nested_resource = @object.send(params[:nested].underscore.pluralize)
- end
-
- # Reutrns root_resource if nested_resource is not set scoped by permissions
- def resource
- (@nested_resource || @root_resource)
- end
-
# Used to setup the resource's schema, mapping attributes and it's types
def resource_schema
schemated = {}
resource.columns_hash.each { |key, value| schemated[key] = value.type }
schemated
@@ -146,11 +130,11 @@
# permissions
def key_scope_records
@records = @records.accessible_by(current_ability).unscope(:order)
end
- # The response for index action, which can be a pagination of a record collection
- # or a grouped count of attributes
+ # The response for index action, which can be a pagination of a
+ # record collection or a grouped count of attributes
def index_json
if params[:group].present?
@records.group(params[:group][:by].split(','))
.send(:calculate,
params[:group][:calculate],