Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Crudable
- extend ActiveSupport::Concern
- included do
- before_action :set_resource, only: [:audits, :show, :update, :destroy]
- def index
- # @todo: hmmm....
- if params[:portal_id].present?
- render json: _resource_class.send(:all_for_portal, params[:portal_id])
- else
- render json: _resource_class.all.try(:with_all)
- end
- end
- def page
- return index unless _resource_class.respond_to? :page
- return _page_result unless params[:search].present?
- return _page_result unless _resource_class.respond_to? :search
- return _search_result
- end
- def show
- render partial: _resource_name and return if _partial_exist?
- render json: _resource_class.find(params[:id])
- end
- def create
- res = _resource_class.new _get_strong_params
- if res.save
- render json: res, status: :created
- else
- render json: res.errors, status: :unprocessable_entity
- end
- end
- def update
- res = instance_variable_get %(@#{_resource_name})
- if res.update _get_strong_params
- render json: res, status: :ok
- else
- render json: res.errors, status: :unprocessable_entity
- end
- end
- def destroy
- instance_variable_get(%(@#{_resource_name})).destroy
- head 204
- end
- private
- @@resource_class ||= nil
- @@resource_name ||= nil
- def set_resource
- instance_variable_set %(@#{_resource_name}), _resource_class.find(params[:id])
- end
- def _partial_exist?
- lookup_context.template_exists? _resource_name, _resource_class.name.pluralize.underscore, true
- end
- def _resource_class
- @@resource_class || self.class.name.gsub('Controller', '').singularize.constantize
- end
- def _resource_name
- @@resource_name || self.class.name.gsub('Controller', '').singularize.demodulize.downcase
- end
- def _get_strong_params
- strong_params = %(#{_resource_name}_params).to_sym
- raise NoMethodError, %(Method #{strong_params} does not exist) unless self.private_methods.include? strong_params
- send strong_params
- end
- def _search_result
- page_num = params[:page] || 1
- list = _resource_class
- # @todo: very bad decision
- params[:search] = %(title.title:(#{params[:search].gsub('/', '_')}))
- list = list.search(params[:search]) if list.respond_to? :search
- list = list.page(page_num)
- list = list.per(params[:per].to_i) if params[:per].present?
- render body: JSON.generate({
- total: list.total_count,
- list: params[:portal_id].present? ? list.records.try(:all_for_portal, params[:portal_id]) : list.records.try(:with_all)
- }), content_type: 'application/json; charset=utf-8'
- end
- def _page_result
- page_num = params[:page] || 1
- list = _resource_class.all
- if params[:portal_id].present?
- list = _resource_class.all.try(:all_for_portal, params[:portal_id])
- elsif list.respond_to? :with_all
- list = list.with_all
- else
- list = _resource_class.all
- end
- if params[:json_filter].present? && list.respond_to?(:params_json_filter)
- list = list.params_json_filter params[:json_filter]
- end
- list = list.page(page_num)
- list = list.per(params[:per].to_i) if params[:per].present?
- # we should use JSON.generate to call .to_json for every component; "render json" does not work
- render body: JSON.generate({
- total: list.total_count,
- list: list
- }), content_type: 'application/json; charset=utf-8'
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement