module Forcast module Controller module Definitor 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 def permitted_definition %w[create show put patch edit delete wizard info info_table card association webhook] end def selected_definitions(*selected) all_definitions.slice(*selected) end def excluded_definitions(*selected) all_definitions.except(*selected) end def selected_serializer_definitions(*selected) serializer(all_definitions.slice(*selected)) end def selected_serializer_definitions_mongo(*selected) serializer_mongo(all_definitions.slice(*selected)) end def excluded_serializer_definitions(*selected) serializer(all_definitions.except(*selected)) end def all_serializer_definitions serializer(all_definitions) end 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 def selected_info_serializer_definitions(*selected) serializer_info(all_definitions.slice(*selected)) end 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 def selected_association_serializer_definitions(*selected) serializer_association(all_definitions.slice(*selected)) end 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 def selected_association_serializer_definitions_mongo(*selected) serializer_association_mongo(all_definitions.slice(*selected)) end 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 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 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 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 def definitor_datetime(key, type) constructor(key, type) do |result| result[:type] = 'date' end end def definitor_decimal(key, type) constructor(key, type) do |result| result[:type] = 'input' result[:inputType] = 'number' end end 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 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 def definitor_integer(key, type) constructor(key, type) do |result| result[:type] = 'input' result[:inputType] = 'number' end end def definitor_password(key, type) constructor(key, type) do |result| result[:type] = 'input' result[:inputType] = 'password' end end def definitor_random_string(key, type) constructor(key, type) do |result| random_string_type(result) end end def definitor_string(key, type) constructor(key, type) do |result| input_type(result) end end def definitor_text(key, type) constructor(key, type) do |result| input_type(result) end end def definitor_boolean(key, type) constructor(key, type) do |result| result[:type] = 'checkbutton' end end 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 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 def random_string_type(result) list = ['radiobutton','input','select'] number = rand(0...3) type = list[number] send("#{type}_type", result, ['Opt1', 'Opt2'], 'Opt1') end def select_type(result, array, value) result[:type] = 'select' result[:options] = array result[:value] = value end def input_type(result, array='', value='') result[:type] = 'input' result[:inputType] = 'text' end def radiobutton_type(result, array, value) result[:type] = 'radiobutton' result[:options] = array result[:value] = value end end end end