Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.63 KB | None | 0 0
  1. module Crudable
  2.   extend ActiveSupport::Concern
  3.   included do
  4.     before_action :set_resource, only: [:audits, :show, :update, :destroy]
  5.  
  6.     def index
  7.       # @todo: hmmm....
  8.       if params[:portal_id].present?
  9.         render json: _resource_class.send(:all_for_portal, params[:portal_id])
  10.       else
  11.         render json: _resource_class.all.try(:with_all)
  12.       end
  13.     end
  14.  
  15.     def page
  16.       return index unless _resource_class.respond_to? :page
  17.       return _page_result unless params[:search].present?
  18.       return _page_result unless _resource_class.respond_to? :search
  19.       return _search_result
  20.     end
  21.  
  22.     def show
  23.       render partial: _resource_name and return if _partial_exist?
  24.       render json: _resource_class.find(params[:id])
  25.     end
  26.  
  27.     def create
  28.       res = _resource_class.new _get_strong_params
  29.       if res.save
  30.         render json: res, status: :created
  31.       else
  32.         render json: res.errors, status: :unprocessable_entity
  33.       end
  34.     end
  35.  
  36.     def update
  37.       res = instance_variable_get %(@#{_resource_name})
  38.       if res.update _get_strong_params
  39.         render json: res, status: :ok
  40.       else
  41.         render json: res.errors, status: :unprocessable_entity
  42.       end
  43.     end
  44.  
  45.     def destroy
  46.       instance_variable_get(%(@#{_resource_name})).destroy
  47.       head 204
  48.     end
  49.  
  50.     private
  51.  
  52.     @@resource_class ||= nil
  53.     @@resource_name  ||= nil
  54.  
  55.     def set_resource
  56.       instance_variable_set %(@#{_resource_name}), _resource_class.find(params[:id])
  57.     end
  58.  
  59.     def _partial_exist?
  60.       lookup_context.template_exists? _resource_name, _resource_class.name.pluralize.underscore, true
  61.     end
  62.  
  63.     def _resource_class
  64.       @@resource_class || self.class.name.gsub('Controller', '').singularize.constantize
  65.     end
  66.  
  67.     def _resource_name
  68.       @@resource_name || self.class.name.gsub('Controller', '').singularize.demodulize.downcase
  69.     end
  70.  
  71.     def _get_strong_params
  72.       strong_params = %(#{_resource_name}_params).to_sym
  73.       raise NoMethodError, %(Method #{strong_params} does not exist) unless self.private_methods.include? strong_params
  74.       send strong_params
  75.     end
  76.  
  77.     def _search_result
  78.       page_num = params[:page] || 1
  79.       list = _resource_class
  80.  
  81.       # @todo: very bad decision
  82.       params[:search] = %(title.title:(#{params[:search].gsub('/', '_')}))
  83.       list = list.search(params[:search]) if list.respond_to? :search
  84.       list = list.page(page_num)
  85.  
  86.       list = list.per(params[:per].to_i) if params[:per].present?
  87.       render body: JSON.generate({
  88.         total: list.total_count,
  89.         list: params[:portal_id].present? ? list.records.try(:all_for_portal, params[:portal_id]) : list.records.try(:with_all)
  90.       }), content_type: 'application/json; charset=utf-8'
  91.     end
  92.  
  93.     def _page_result
  94.       page_num = params[:page] || 1
  95.       list = _resource_class.all
  96.  
  97.       if params[:portal_id].present?
  98.         list = _resource_class.all.try(:all_for_portal, params[:portal_id])
  99.       elsif list.respond_to? :with_all
  100.         list = list.with_all
  101.       else
  102.         list = _resource_class.all
  103.       end
  104.  
  105.       if params[:json_filter].present? && list.respond_to?(:params_json_filter)
  106.         list = list.params_json_filter params[:json_filter]
  107.       end
  108.  
  109.       list = list.page(page_num)
  110.       list = list.per(params[:per].to_i) if params[:per].present?
  111.       # we should use JSON.generate to call .to_json for every component; "render json" does not work
  112.       render body: JSON.generate({
  113.         total: list.total_count,
  114.         list: list
  115.       }), content_type: 'application/json; charset=utf-8'
  116.     end
  117.   end
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement