Module: ActionForm::SchemaDSL::ClassMethods
- Defined in:
- lib/action_form/schema_dsl.rb
Overview
rubocop:disable Style/Documentation
Instance Method Summary collapse
-
#create_params_definition(elements_definitions: elements) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- #inherited_params_blocks ⇒ Object
- #params(&block) ⇒ Object
- #params_blocks ⇒ Object
- #params_class ⇒ Object
- #params_definition ⇒ Object
Instance Method Details
#create_params_definition(elements_definitions: elements) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/action_form/schema_dsl.rb', line 38 def create_params_definition(elements_definitions: elements) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength schema = Class.new(params_class) elements_definitions.each do |name, element_definition| if element_definition < ActionForm::SubformsCollection # nested forms are passed as a hash that looks like this: # { "0" => { "id" => "1" }, "1" => { "id" => "2" } } # it is coercing to an array of hashes: # [['0', { "id" => "1" }], ['1', { "id" => "2" }]] # we need to normalize it to an array of hashes: # [ { "id" => "1" }, { " # id" => "2" } ] schema.each(:"#{name}_attributes", element_definition.subform_definition.params_definition, normalize: ->(value) { value.flatten.select { |v| v.is_a?(Hash) } }, default: element_definition.default) elsif element_definition < ActionForm::Subform schema.has(:"#{name}_attributes", element_definition.params_definition, default: element_definition.default) elsif element_definition < ActionForm::Element = element_definition..dup method_name = .delete(:type) schema.public_send(method_name, name, **) end end patches = inherited_params_blocks + params_blocks patches.each do |block| schema = Class.new(schema, &block) end schema.form_class = self schema end |
#inherited_params_blocks ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/action_form/schema_dsl.rb', line 28 def inherited_params_blocks parent = superclass blocks = [] while parent.respond_to?(:params_blocks) parent.params_blocks.each { |block| blocks.unshift(block) } parent = parent.superclass end blocks end |
#params(&block) ⇒ Object
24 25 26 |
# File 'lib/action_form/schema_dsl.rb', line 24 def params(&block) params_blocks << block if block end |
#params_blocks ⇒ Object
20 21 22 |
# File 'lib/action_form/schema_dsl.rb', line 20 def params_blocks @params_blocks ||= [] end |
#params_class ⇒ Object
12 13 14 |
# File 'lib/action_form/schema_dsl.rb', line 12 def params_class ActionForm::Params end |
#params_definition ⇒ Object
16 17 18 |
# File 'lib/action_form/schema_dsl.rb', line 16 def params_definition @params_definition ||= create_params_definition end |