Class: GlobalController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/autogenerators/rails_templates/global_controller.rb

Instance Method Summary collapse

Instance Method Details

#black_listObject

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_createObject



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_paramsObject



111
112
113
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 111

def bulk_create_params
  params[:bulk]
end

#bulk_csv_createObject



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_paramsObject



119
120
121
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 119

def bulk_csv_create_params
  params[:csv_json]
end

#bulk_delete_paramsObject



115
116
117
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 115

def bulk_delete_params
  params[:ids]
end

#bulk_destroyObject



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_updateObject



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_paramsObject



107
108
109
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 107

def bulk_update_params
  params[:bulk]
end

#createObject



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_paramsObject

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] || (default_options && default_options[:include])
      includes = include_hash(nil, params[:include] ? sorted_include_array(params[:include]) : default_options[: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_optionsObject



22
23
24
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 22

def default_options
  {}
end

#destroyObject



57
58
59
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 57

def destroy
  default_error_handling(handle_destroy, 204)
end

#duplicateObject



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_destroyObject



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_paginationObject



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_showObject



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

#indexObject

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

#modelObject

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_paramsObject



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

#showObject



32
33
34
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 32

def show
  default_error_handling(handle_show, 200)
end

#updateObject



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_paramsObject



103
104
105
# File 'lib/autogenerators/rails_templates/global_controller.rb', line 103

def update_params
  render_params
end