Sha256: b13f7c66dac5c80cafcc8d7f5be668f7136f5c6073836c053b979ba4a8b4b83f

Contents?: true

Size: 1007 Bytes

Versions: 13

Compression:

Stored size: 1007 Bytes

Contents

require "reform/form/active_model"
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.
      [: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

13 entries across 13 versions & 1 rubygems

Version Path
reform-2.1.0 lib/reform/form/composition.rb
reform-2.1.0.rc1 lib/reform/form/composition.rb
reform-2.0.5 lib/reform/form/composition.rb
reform-2.0.4 lib/reform/form/composition.rb
reform-2.0.3 lib/reform/form/composition.rb
reform-2.0.2 lib/reform/form/composition.rb
reform-2.0.1 lib/reform/form/composition.rb
reform-2.0.0 lib/reform/form/composition.rb
reform-2.0.0.rc3 lib/reform/form/composition.rb
reform-2.0.0.rc2 lib/reform/form/composition.rb
reform-2.0.0.rc1 lib/reform/form/composition.rb
reform-2.0.0.beta2 lib/reform/form/composition.rb
reform-2.0.0.beta1 lib/reform/form/composition.rb