Module: Reform::Form::Property
- Included in:
- Reform::Form
- Defined in:
- lib/reform/form.rb
Instance Method Summary collapse
-
#property(name, options = {}, &block) ⇒ Object
Add macro logic, e.g.
Instance Method Details
#property(name, options = {}, &block) ⇒ Object
Add macro logic, e.g. for :populator. rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/reform/form.rb', line 21 def property(name, = {}, &block) if (.keys & %i[skip_if populator]).size == 2 raise InvalidOptionsCombinationError.new( "[Reform] #{self}:property:#{name} Do not use skip_if and populator together, use populator with skip! instead" ) end # if composition and inherited we also need this setting # to correctly inherit modules [:_inherited] = [:inherit] if .key?(:on) && .key?(:inherit) if .key?(:parse) [:deserializer] ||= {} [:deserializer][:writeable] = .delete(:parse) end [:writeable] ||= .delete(:writable) if .key?(:writable) # for virtual collection we need at least to have the collection equal to [] to # avoid issue when the populator if (.keys & %i[collection virtual]).size == 2 = { default: [] }.merge() end definition = super # letdisposable and declarative gems sort out inheriting of properties, and so on. definition.merge!(deserializer: {}) unless definition[:deserializer] # always keep :deserializer per property. = definition[:deserializer] # Populators internal_populator = Populator::Sync.new(nil) if block = definition[:populate_if_empty] internal_populator = Populator::IfEmpty.new(block) end if block = definition[:populator] # populator wins over populate_if_empty when :inherit internal_populator = Populator.new(block) end definition.merge!(internal_populator: internal_populator) unless [:internal_populator] external_populator = Populator::External.new # always compute a parse_pipeline for each property of the deserializer and inject it via :parse_pipeline. # first, letrepresentable compute the pipeline functions by invoking #parse_functions. if definition[:nested] parse_pipeline = ->(input, opts) do functions = opts[:binding].send(:parse_functions) pipeline = Representable::Pipeline[*functions] # Pipeline[StopOnExcluded, AssignName, ReadFragment, StopOnNotFound, OverwriteOnNil, Collect[#<Representable::Function::CreateObject:0xa6148ec>, #<Representable::Function::Decorate:0xa6148b0>, Deserialize], Set] pipeline = Representable::Pipeline::Insert.(pipeline, external_populator, replace: Representable::CreateObject::Instance) pipeline = Representable::Pipeline::Insert.(pipeline, Representable::Decorate, delete: true) pipeline = Representable::Pipeline::Insert.(pipeline, Deserialize, replace: Representable::Deserialize) pipeline = Representable::Pipeline::Insert.(pipeline, Representable::SetValue, delete: true) # FIXME: only diff to options without :populator end else parse_pipeline = ->(input, opts) do functions = opts[:binding].send(:parse_functions) pipeline = Representable::Pipeline[*functions] # Pipeline[StopOnExcluded, AssignName, ReadFragment, StopOnNotFound, OverwriteOnNil, Collect[#<Representable::Function::CreateObject:0xa6148ec>, #<Representable::Function::Decorate:0xa6148b0>, Deserialize], Set] # FIXME: this won't work with property :name, inherit: true (where there is a populator set already). pipeline = Representable::Pipeline::Insert.(pipeline, external_populator, replace: Representable::SetValue) if definition[:populator] # FIXME: only diff to options without :populator pipeline end end [:parse_pipeline] ||= parse_pipeline if proc = definition[:skip_if] proc = Reform::Form::Validate::Skip::AllBlank.new if proc == :all_blank .merge!(skip_parse: proc) # TODO: same with skip_parse ==> External end # per default, everything should be writeable for the deserializer (we're only writing on the form). however, allow turning it off. .merge!(writeable: true) unless .key?(:writeable) definition end |