app/controllers/simple_admin/resources_controller.rb in simpleadmin-1.2.7 vs app/controllers/simple_admin/resources_controller.rb in simpleadmin-1.3.0
- old
+ new
@@ -1,33 +1,24 @@
module SimpleAdmin
class ResourcesController < BaseController
def index
- resource_service = ResourcesService.new(params[:model_klass_name], params[:model_fields])
- .index_action(params[:per_page], params[:page], params[:sort],
- params[:query], params[:model_attributes], params[:reflection_tables],
- params[:reflection_columns])
+ resource_service = ResourceService.new(model_klass, model_fields)
- render json: {
- resources: resource_service.resources,
- total: resource_service.total
- }
+ render json: resource_service.index_action(params[:per_page], params[:page], params[:sort],
+ params[:query], params[:model_attributes], params[:reflection_tables], params[:reflection_columns])
end
def show
- model_fields = params[:model_fields].map { |model_field| model_field['field_name'] }
+ resource = model_klass.find(params[:id])
- resource = model_klass.find(params[:id]).attributes.slice(*model_fields)
-
- render json: resource
+ render json: resource.attributes.slice(*model_fields)
end
def show_by
- model_fields = params[:model_fields].map { |model_field| model_field['field_name'] }
+ resource = model_klass.find_by(params[:column_name] => params[:column_value])
- resource = model_klass.find_by(params[:column_name] => params[:column_value]).attributes.slice(*model_fields)
-
- render json: resource
+ render json: resource.attributes.slice(*model_fields)
end
def show_total
render json: model_klass.count
end
@@ -60,9 +51,13 @@
private
def model_klass
params[:model_klass_name].constantize
+ end
+
+ def model_fields
+ params[:model_fields].map { |model_field| model_field['field_name'] }
end
def resource_params
params.require(:resource).permit!
end