Sha256: f4954453e398edcba8e00faee0c929dc9cfb4bcdb2ecbd455a4ab4ef5063631e

Contents?: true

Size: 973 Bytes

Versions: 8

Compression:

Stored size: 973 Bytes

Contents

require "disposable/twin/composition"

module Reform::Form::Composition
  # Automatically creates a Composition object for you when initializing the form.
  def self.included(base)
    base.class_eval do
      # extend Reform::Form::ActiveModel::ClassMethods # ::model.
      extend ClassMethods
      include Disposable::Twin::Composition
    end
  end

  module ClassMethods
    # Same as ActiveModel::model but allows you to define the main model in the composition
    # using +:on+.
    #
    # class CoverSongForm < Reform::Form
    #   model :song, on: :cover_song
    def model(main_model, options = {})
      super

      composition_model = options[:on] || main_model

      # FIXME: this should just delegate to :model as in FB, and the comp would take care of it internally.
      %i[persisted? to_key to_param].each do |method|
        define_method method do
          model[composition_model].send(method)
        end
      end

      self
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
reform-2.6.2 lib/reform/form/composition.rb
reform-2.6.1 lib/reform/form/composition.rb
reform-2.6.0 lib/reform/form/composition.rb
reform-2.5.0 lib/reform/form/composition.rb
reform-2.3.3 lib/reform/form/composition.rb
reform-2.3.2 lib/reform/form/composition.rb
reform-2.3.1 lib/reform/form/composition.rb
reform-2.3.0.rc2 lib/reform/form/composition.rb