Sha256: 878731fd33789e664a4108276101b0eabf6b80c3eda57622537d7f2f1049608e

Contents?: true

Size: 1.34 KB

Versions: 38

Compression:

Stored size: 1.34 KB

Contents

class Tramway::Core::ExtendableForm
  class << self
    def new(name, simple_properties: {}, **more_properties)
      if Object.const_defined? name
        name.constantize
      else
        Object.const_set(name, Class.new(::Tramway::Core::ApplicationForm) do
          properties(*simple_properties.keys) if simple_properties.keys.any?

          define_method 'submit' do |params|
            model.values ||= {}
            extended_params = params.except(*simple_properties.keys)
            model.values = extended_params.permit!.to_h.reduce(model.values) do |hash, pair|
              hash.merge! pair[0] => pair[1]
            end
            super params
          end

          define_method 'properties' do
            hash = simple_properties.reduce({}) do |h, property|
              unless model.class.state_machines.keys.include?(property[0])
                h.merge! property[0] => property[1]
              end
              h
            end
            more_properties.reduce(hash) do |h, property|
              h.merge! property[0] => {
                extended_form_property: property[1]
              }
            end
          end

          more_properties.each do |property|
            define_method property[0] do 
              model.values[property[0]] if model.values
            end
          end
        end)
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
tramway-core-1.9.6 app/forms/tramway/core/extendable_form.rb
tramway-core-1.9.5 app/forms/tramway/core/extendable_form.rb
tramway-core-1.9.4 app/forms/tramway/core/extendable_form.rb
tramway-core-1.9.3 app/forms/tramway/core/extendable_form.rb
tramway-core-1.9.2.1 app/forms/tramway/core/extendable_form.rb
tramway-core-1.9.2 app/forms/tramway/core/extendable_form.rb
tramway-core-1.9.1 app/forms/tramway/core/extendable_form.rb
tramway-core-1.9 app/forms/tramway/core/extendable_form.rb
tramway-core-1.8.4 app/forms/tramway/core/extendable_form.rb
tramway-core-1.8.3 app/forms/tramway/core/extendable_form.rb
tramway-core-1.8.2 app/forms/tramway/core/extendable_form.rb
tramway-core-1.8.1 app/forms/tramway/core/extendable_form.rb
tramway-core-1.8 app/forms/tramway/core/extendable_form.rb
tramway-core-1.7.4 app/forms/tramway/core/extendable_form.rb
tramway-core-1.7.3 app/forms/tramway/core/extendable_form.rb
tramway-core-1.7.2 app/forms/tramway/core/extendable_form.rb
tramway-core-1.7.1 app/forms/tramway/core/extendable_form.rb
tramway-core-1.7 app/forms/tramway/core/extendable_form.rb