Module: SnfCore::Common
- Extended by:
- ActiveSupport::Concern
- Includes:
- Pagination
- Defined in:
- app/controllers/concerns/common.rb
Instance Method Summary collapse
Instance Method Details
#create ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/controllers/concerns/common.rb', line 63 def create obj = nil = {} if block_given? incoming = yield if incoming.instance_of?(Array) obj, = incoming elsif incoming.instance_of?(Hash) obj = @clazz.new(model_params) = incoming else obj = incoming end else obj = @clazz.new(model_params) end if obj.save result = { success: true, data: serialize(obj, ) } render json: result, status: :created else render json: { success: false, error: obj.errors.[0] }, status: 422 end rescue StandardError => e render json: { success: false, error: e. } end |
#index ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/controllers/concerns/common.rb', line 11 def index data = nil = {} if block_given? incoming = yield if incoming.instance_of?(Array) data, = incoming elsif incoming.instance_of?(Hash) = incoming else data = incoming end else data = @clazz.all end total = data.count data = data.then(&paginate) if params[:page] result = { success: true, data: serialize(data, ) } if params[:page] result[:page] = params[:page] result[:total] = total end render json: result end |
#show ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/controllers/concerns/common.rb', line 40 def show data = nil = {} if block_given? incoming = yield if incoming.instance_of?(Array) data, = incoming elsif incoming.instance_of?(Hash) data = @obj = incoming else data = incoming end else data = @obj end result = { success: true, data: serialize(data, ) } render json: result end |
#update ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/controllers/concerns/common.rb', line 93 def update obj = nil = {} if block_given? incoming = yield if incoming.instance_of?(Array) obj, = incoming elsif incoming.instance_of?(Hash) obj = set_object = incoming else obj = incoming end else obj = set_object end if obj.update(model_params) result = { success: true, data: serialize(obj, ) } render json: result else render json: { success: false, error: obj.errors.[0] }, status: 422 end rescue StandardError => e render json: { success: false, error: e. } end |