Class: GlobalController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- GlobalController
- Defined in:
- lib/autogenerators/rails_templates/global_controller.rb
Instance Method Summary collapse
-
#black_list ⇒ Object
Black List any of these keys from your response.
- #bulk_create ⇒ Object
- #bulk_create_params ⇒ Object
- #bulk_csv_create ⇒ Object
- #bulk_csv_create_params ⇒ Object
- #bulk_delete_params ⇒ Object
- #bulk_destroy ⇒ Object
- #bulk_update ⇒ Object
- #bulk_update_params ⇒ Object
- #create ⇒ Object
-
#create_params ⇒ Object
Default param declarations.
-
#default_error_handling(data, status_code) ⇒ Object
Querystring Parsing & Error Handling.
- #default_options ⇒ Object
- #destroy ⇒ Object
- #duplicate ⇒ Object
- #handle_bulk_create(body_array) ⇒ Object
- #handle_bulk_destroy(id_array) ⇒ Object
- #handle_bulk_params(*args) ⇒ Object
- #handle_bulk_update(body_hash) ⇒ Object
- #handle_create(body_params) ⇒ Object
- #handle_destroy ⇒ Object
-
#handle_duplicate(id = nil) ⇒ Object
Handlers for Global Controller methods.
- #handle_pagination ⇒ Object
-
#handle_params(*args) ⇒ Object
Custom params.permit methods for Global Controller.
- #handle_show ⇒ Object
- #handle_update(body_params) ⇒ Object
-
#index ⇒ Object
Resource Action Methods.
-
#model ⇒ Object
Default declarations.
- #render_params ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
- #update_params ⇒ Object
Instance Method Details
#black_list ⇒ Object
Black List any of these keys from your response
3 4 5 6 7 8 9 10 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 3 def black_list { # response_key: true, password: true, password_digest: true, reset_password_token: true } end |
#bulk_create ⇒ Object
40 41 42 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 40 def bulk_create default_error_handling(handle_bulk_create(bulk_create_params), 201) end |
#bulk_create_params ⇒ Object
111 112 113 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 111 def bulk_create_params params[:bulk] end |
#bulk_csv_create ⇒ Object
44 45 46 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 44 def bulk_csv_create default_error_handling(handle_bulk_create(bulk_csv_create_params), 201) end |
#bulk_csv_create_params ⇒ Object
119 120 121 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 119 def bulk_csv_create_params params[:csv_json] end |
#bulk_delete_params ⇒ Object
115 116 117 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 115 def bulk_delete_params params[:ids] end |
#bulk_destroy ⇒ Object
61 62 63 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 61 def bulk_destroy default_error_handling(handle_bulk_destroy(bulk_delete_params), 204) end |
#bulk_update ⇒ Object
53 54 55 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 53 def bulk_update default_error_handling(handle_bulk_update(bulk_update_params), 200) end |
#bulk_update_params ⇒ Object
107 108 109 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 107 def bulk_update_params params[:bulk] end |
#create ⇒ Object
36 37 38 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 36 def create default_error_handling(handle_create(create_params), 201) end |
#create_params ⇒ Object
Default param declarations
99 100 101 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 99 def create_params render_params end |
#default_error_handling(data, status_code) ⇒ Object
Querystring Parsing & Error Handling
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 195 def default_error_handling(data, status_code) if status_code == 204 && data render json: data, status: status_code elsif data.respond_to?(:errors) && data.errors.count > 0 render json: data.errors, status: 422 elsif data if params[:include] || ( && [:include]) includes = include_hash(nil, params[:include] ? sorted_include_array(params[:include]) : [:include]) json = inject_includes(data, includes) else json = data end render json: filter_json_response(json), status: status_code else render json: { error: 'Bad Request' }, status: 400 end end |
#default_options ⇒ Object
22 23 24 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 22 def {} end |
#destroy ⇒ Object
57 58 59 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 57 def destroy default_error_handling(handle_destroy, 204) end |
#duplicate ⇒ Object
65 66 67 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 65 def duplicate default_error_handling(handle_duplicate, 201) end |
#handle_bulk_create(body_array) ⇒ Object
168 169 170 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 168 def handle_bulk_create(body_array) model.create(body_array) end |
#handle_bulk_destroy(id_array) ⇒ Object
189 190 191 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 189 def handle_bulk_destroy(id_array) id_array.split(',').map { |id| model.where(id: id).delete_all }.length end |
#handle_bulk_params(*args) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 83 def handle_bulk_params(*args) params[:bulk].map do |element| args.each_with_object({}) do |item, acc| if item.is_a?(Hash) && !item.empty? && element[(item[:key].to_s + '_id').to_sym] acc[item[:key].to_sym] = item[:model].find_by(id: element[(item[:key].to_s + '_id').to_sym]) elsif item.is_a?(String) && element[item.to_sym] acc[item.to_sym] = element[item.to_sym] elsif item.is_a?(Symbol) && element[item] acc[item] = element[item] end end end end |
#handle_bulk_update(body_hash) ⇒ Object
181 182 183 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 181 def handle_bulk_update(body_hash) model.update(body_hash.keys, body_hash.values) end |
#handle_create(body_params) ⇒ Object
164 165 166 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 164 def handle_create(body_params) model.create(body_params) end |
#handle_destroy ⇒ Object
185 186 187 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 185 def handle_destroy model.where(id: params[:id]).delete_all end |
#handle_duplicate(id = nil) ⇒ Object
Handlers for Global Controller methods
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 125 def handle_duplicate(id = nil) associations = model.reflect_on_all_associations.reject { |e| e.is_a?(ActiveRecord::Reflection::BelongsToReflection) } existing = model.find_by(id: id || params[:id]) attributes = existing.attributes.symbolize_keys.without(:id, :created_at, :updated_at) as_keyword_lookup = association_lookup(associations) parent_model_key = (model.name.to_s.underscore + '_id').to_sym created = model.create(unique_attributes(attributes)) all_assocations = associations.select do |e| lookup = e.klass.new.attributes.symbolize_keys as_keyword_lookup[e.klass.name.to_sym] || lookup[parent_model_key] end.map { |e| existing.send(e.name) } all_assocations_with_klass = all_assocations.flat_map { |e| { model: e.klass, data: e } }.reject { |e| e[:data].empty? } all_assocations_with_klass.each do |e| model_name = e[:model].name.to_sym polymorphic = as_keyword_lookup[model_name] model_key = polymorphic || (model.name.to_s.underscore + '_id').to_sym model_type = polymorphic || (model.name.to_s.underscore + '_type').to_sym e[:data].each do |element| attrs = element.attributes.symbolize_keys.without(:id, :created_at, :updated_at) if (attrs[model_key] && !polymorphic) || (polymorphic && attrs[model_type] && attrs[model_key] && attrs[model_type] == e[:model].name) attrs[model_key] = created.id e[:model].create(unique_attributes(attrs)) end end end created end |
#handle_pagination ⇒ Object
156 157 158 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 156 def handle_pagination { data: pagination_data_types, count: pagination_count_type } end |
#handle_params(*args) ⇒ Object
Custom params.permit methods for Global Controller
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 71 def handle_params(*args) args.each_with_object({}) do |item, acc| if item.is_a?(Hash) && !item.empty? && params[(item[:key].to_s + '_id').to_sym] acc[item[:key].to_sym] = item[:model].find_by(id: params[(item[:key].to_s + '_id').to_sym]) elsif item.is_a?(String) && params[item.to_sym] acc[item.to_sym] = params[item.to_sym] elsif item.is_a?(Symbol) && params[item] acc[item] = params[item] end end end |
#handle_show ⇒ Object
160 161 162 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 160 def handle_show show_data_types end |
#handle_update(body_params) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 172 def handle_update(body_params) data = model.where(id: params[:id]).update(body_params) if data.is_a?(Array) && !data.empty? { status: 200, data: data.first } else { status: 400, data: { error: 'Bad Request' } } end end |
#index ⇒ Object
Resource Action Methods
28 29 30 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 28 def index default_error_handling(handle_pagination, 200) end |
#model ⇒ Object
Default declarations
14 15 16 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 14 def model self.class.to_s.chomp("Controller").singularize.constantize end |
#render_params ⇒ Object
18 19 20 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 18 def render_params params.permit(*model.columns.map { |e| e.name.to_sym }) end |
#show ⇒ Object
32 33 34 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 32 def show default_error_handling(handle_show, 200) end |
#update ⇒ Object
48 49 50 51 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 48 def update data = handle_update(update_params) default_error_handling(data[:data], data[:status]) end |
#update_params ⇒ Object
103 104 105 |
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 103 def update_params render_params end |