Module: Forcast::Controller::Definitor

Included in:
Api::VARIABLEController, DefinitorPage
Defined in:
lib/forcast/controllers/controller/definitor/definitor.rb

Instance Method Summary collapse

Instance Method Details

#all_definitions(name_controller = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 42

def all_definitions(name_controller = nil)
  name_controller = controller_name if name_controller.nil?
  model = name_controller.classify.constantize
  attrs = model.attribute_names.map { |n| [n.to_sym, model.type_for_attribute(n).type] }.to_h
  model.reflect_on_all_associations(:belongs_to).each do |relation|
    relation = relation.name.to_s + '_id'
    attrs[relation.to_sym] = 'relation'
  end
  attrs
end

#all_serializer_definitionsObject



38
39
40
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 38

def all_serializer_definitions
  serializer(all_definitions)
end

#constructor(key, type) {|result| ... } ⇒ Object

Yields:

  • (result)


158
159
160
161
162
163
164
165
166
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 158

def constructor(key, type)
  result = Hash.new
  result[:label] = key.titleize
  result[:typeAttribute] = type
  result[:name] = key
  result[:required] = model.parametros_necesario_create.include?(key)
  yield(result)
  result
end

#definitorObject



4
5
6
7
8
9
10
11
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 4

def definitor
  raise Application::Error::General.new(t("operation_notValid")) unless permitted_definition.include?(@operation)
  if @operation == 'put' || @operation == 'patch' || @operation == 'edit'
    @operation = 'put_or_patch'
  end
  @response = send("#{@operation}_view_definitor")
  json_response(@response, 'ok')
end

#definitor_boolean(key, type) ⇒ Object



254
255
256
257
258
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 254

def definitor_boolean(key, type)
  constructor(key, type) do |result|
    result[:type] = 'checkbutton'
  end
end

#definitor_datetime(key, type) ⇒ Object



168
169
170
171
172
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 168

def definitor_datetime(key, type)
  constructor(key, type) do |result|
    result[:type] = 'date'
  end
end

#definitor_decimal(key, type) ⇒ Object



174
175
176
177
178
179
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 174

def definitor_decimal(key, type)
  constructor(key, type) do |result|
    result[:type] = 'input'
    result[:inputType] = 'number'
  end
end

#definitor_integer(key, type) ⇒ Object



222
223
224
225
226
227
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 222

def definitor_integer(key, type)
  constructor(key, type) do |result|
    result[:type] = 'input'
    result[:inputType] = 'number'
  end
end

#definitor_json(key, type) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 260

def definitor_json(key, type)
  constructor(key, type) do |result|
    result[:key] = key
    result[:type] = 'json'
    result[:inputType] = 'text'
    result[:label] = key.classify
    result[:typeAttribute] = 'string'
    result[:name] = key.to_s
    result[:meta] = {
                    selectItem: {
                      type: 'select',
                      label: key.classify,
                      name: key.to_s+'Key',
                      required: false,
                      options: []
                    },
                    areaItem: {
                      type: 'input',
                      inputType: 'text',
                      label: key.classify,
                      typeAttribute: 'string',
                      name: key.to_s+'Value',
                      required: false,
                    }
                    }      
  end
end

#definitor_password(key, type) ⇒ Object



229
230
231
232
233
234
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 229

def definitor_password(key, type)
  constructor(key, type) do |result|
    result[:type] = 'input'
    result[:inputType] = 'password'
  end
end

#definitor_random_string(key, type) ⇒ Object



236
237
238
239
240
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 236

def definitor_random_string(key, type)
  constructor(key, type) do |result|
    random_string_type(result)
  end
end

#definitor_relation(key, type) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 181

def definitor_relation(key, type)
  constructor(key, type) do |result|
    controller_name = key.gsub('_id', '')
    model = controller_name.singularize.classify.constantize
    args =[:id]
    if model.attribute_names.include?('name')
      args.push(:name) 
    elsif model.attribute_names.include?('serie')
      args.push(:serie) 
    elsif model.attribute_names.include?('model')
        args.push(:model)
    end
    options = model.all.pluck(*args).map { |e| e.size == 2 ? {id: e[0], label: e[1]} : {id: e, label: controller_name.singularize.classify } }
    result[:type] = "dataselect"
    result[:label] = controller_name.singularize.classify
    result[:disabled] = true
    result[:options] = options
    result[:typeAttribute] = "string"
  end
end

#definitor_relation_mongo(key, type) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 202

def definitor_relation_mongo(key, type)
  constructor(key, type) do |result|
    controller_name = key.gsub('_id', '') + '_mongo'
    model = (controller_name).singularize.classify.constantize
    args =[:id]
    if model.attribute_names.include?('name')
      args.push(:name) 
    elsif model.attribute_names.include?('serie')
      args.push(:serie) 
    elsif model.attribute_names.include?('model')
        args.push(:model)
    end
    options = model.all.pluck(*args).map { |e| e.is_a?(Array) ? {id: e[0].to_s, label: e[0].to_s.last(5)+' - '+e[1].to_s} : {id: e.to_s, label: e.to_s.last(5)+' - '+model.to_s } }
    result[:type] = "dataselect"
    result[:label] = key.singularize.classify
    result[:disabled] = true
    result[:options] = options
  end
end

#definitor_string(key, type) ⇒ Object



242
243
244
245
246
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 242

def definitor_string(key, type)
  constructor(key, type) do |result|
    input_type(result)
  end
end

#definitor_text(key, type) ⇒ Object



248
249
250
251
252
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 248

def definitor_text(key, type)
  constructor(key, type) do |result|
    input_type(result)
  end
end

#definitor_with_options(type, object, array, value) ⇒ Object



288
289
290
291
292
293
294
295
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 288

def definitor_with_options(type, object, array, value)
  object = all_definitions.slice(object)
  key = object.keys.first.to_s
  definition_result = constructor(key, object[key]) do |result|
    send("#{type}_type", result, array, value)
  end
  [definition_result]
end

#excluded_definitions(*selected) ⇒ Object



21
22
23
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 21

def excluded_definitions(*selected)
  all_definitions.except(*selected)
end

#excluded_serializer_definitions(*selected) ⇒ Object



34
35
36
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 34

def excluded_serializer_definitions(*selected)
  serializer(all_definitions.except(*selected))
end

#input_type(result, array = '', value = '') ⇒ Object



310
311
312
313
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 310

def input_type(result, array='', value='')
  result[:type] = 'input'
  result[:inputType] = 'text'
end

#permitted_definitionObject



13
14
15
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 13

def permitted_definition
  %w[create show put patch edit delete wizard info info_table card association webhook]
end

#radiobutton_type(result, array, value) ⇒ Object



315
316
317
318
319
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 315

def radiobutton_type(result, array, value)
  result[:type] = 'radiobutton'
  result[:options] = array
  result[:value] = value
end

#random_string_type(result) ⇒ Object



297
298
299
300
301
302
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 297

def random_string_type(result)
  list = ['radiobutton','input','select']
  number = rand(0...3)
  type = list[number]
  send("#{type}_type", result, ['Opt1', 'Opt2'], 'Opt1')
end

#select_type(result, array, value) ⇒ Object



304
305
306
307
308
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 304

def select_type(result, array, value)
  result[:type] = 'select'
  result[:options] = array
  result[:value] = value
end

#selected_association_serializer_definitions(*selected) ⇒ Object



69
70
71
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 69

def selected_association_serializer_definitions(*selected)
    serializer_association(all_definitions.slice(*selected))
end

#selected_association_serializer_definitions_mongo(*selected) ⇒ Object



101
102
103
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 101

def selected_association_serializer_definitions_mongo(*selected)
    serializer_association_mongo(all_definitions.slice(*selected))
end

#selected_definitions(*selected) ⇒ Object



17
18
19
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 17

def selected_definitions(*selected)
  all_definitions.slice(*selected)
end

#selected_info_serializer_definitions(*selected) ⇒ Object



53
54
55
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 53

def selected_info_serializer_definitions(*selected)
  serializer_info(all_definitions.slice(*selected))
end

#selected_serializer_definitions(*selected) ⇒ Object



25
26
27
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 25

def selected_serializer_definitions(*selected)
  serializer(all_definitions.slice(*selected))
end

#selected_serializer_definitions_mongo(*selected) ⇒ Object



29
30
31
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 29

def selected_serializer_definitions_mongo(*selected)
  serializer_mongo(all_definitions.slice(*selected))
end

#serializer(object) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 133

def serializer(object)
  definitor = []
  object.delete(:id) if params[:operation] == 'create'
  keys = object.keys
  keys.each do |key|
    type = object[key]
    result = send("definitor_#{type}", key.to_s, type)
    definitor.push(result)
  end
  definitor
end

#serializer_association(object) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 73

def serializer_association(object)
    definitor = []
    keys = object.keys.map {|e| e.to_s.gsub('_id', 's')}
    sender = {type: "select", label: "Resource", 
              name: "resource", required: false, disabled: false, 
              options: keys
            }
    definitor.push(sender)
    keys.each do |key|
      model = key[0...-1].classify.constantize
      args =[:id]
      if model.attribute_names.include?('name')
        args.push(:name) 
      elsif model.attribute_names.include?('serie')
        args.push(:serie) 
      elsif model.attribute_names.include?('model')
        args.push(:model)
      end
      options = model.all.pluck(*args).map { |e| e.size == 2 ? {id: e[0], label: e[1]} : {id: e} }
      sender = {type: "dataselect", label: "ID", 
                name: key[0...-1]+'_id', required: false, disabled: true, 
                options: options
                }
      definitor.push(sender)
    end
    definitor
end

#serializer_association_mongo(object) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 105

def serializer_association_mongo(object)
    definitor = []
    keys = object.keys.map {|e| e.to_s.gsub('_id', 's')}
    sender = {type: "select", label: "Resource", 
              name: "resource", required: false, disabled: false,  
              options: keys
            }
    definitor.push(sender)
    keys.each do |key|
      model = (key[0...-1]+'_mongo').classify.constantize
      args =[:_id]
      if model.attribute_names.include?('name')
        args.push(:name) 
      elsif model.attribute_names.include?('serie')
        args.push(:serie) 
      elsif model.attribute_names.include?('model')
        args.push(:model)
      end
      options = model.all.pluck(*args).map { |e| e.is_a?(Array) ? {id: e[0].to_s, label: e[0].to_s.last(5)+' - '+e[1].to_s} : {id: e.to_s, label: e.to_s.last(5)+' - '+model.to_s } }
      sender = {type: "dataselect", label: "ID", 
                name: key[0...-1]+'_id', required: false, disabled: true, 
                options: options
                }
      definitor.push(sender)
    end
    definitor
end

#serializer_info(object) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 57

def serializer_info(object)
    definitor = []
    keys = object.keys
    keys.each do |key|
      type = object[key]
      result = send("definitor_#{type}", key.to_s, type)
      sender = {key: key, type: result[:typeAttribute], label: result[:label]}
      definitor.push(sender)
    end
    definitor
end

#serializer_mongo(object) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/forcast/controllers/controller/definitor/definitor.rb', line 145

def serializer_mongo(object)
  definitor = []
  object.delete(:id) if params[:operation] == 'create'
  keys = object.keys
  keys.each do |key|
    type = object[key]
    result = send("definitor_#{type}", key.to_s, type)
    result = send("definitor_#{type}_mongo", key.to_s, type) if type == 'relation'
    definitor.push(result)
  end
  definitor
end