Sha256: 759f260eaa07930997aafed99246eedda830d99f7a65bb074e8c427b3ac77d0d

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

module Granite
  module Form
    module Model
      module Associations
        module Reflections
          class EmbedsAny < Base
            class << self
              def build(target, generated_methods, name, options = {}, &block)
                if block
                  options[:class] = proc do |reflection|
                    superclass = reflection.options[:class_name].to_s.presence.try(:constantize)
                    klass = build_class(superclass)
                    target.const_set(name.to_s.classify, klass)
                    klass.class_eval(&block)
                    klass
                  end
                end
                super
              end

              private def build_class(superclass)
                Class.new(superclass || Granite::Form.base_class) do
                  include Granite::Form::Model
                  include Granite::Form::Model::Associations
                  include Granite::Form::Model::Lifecycle
                  include Granite::Form::Model::Primary
                  include Granite::Form.base_concern if Granite::Form.base_concern
                end
              end
            end

            def klass
              @klass ||= if options[:class]
                options[:class].call(self)
              else
                super
              end
            end

            def inspect
              "#{self.class.name.demodulize}(#{klass})"
            end

            def embedded?
              true
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
granite-form-0.2.0 lib/granite/form/model/associations/reflections/embeds_any.rb
granite-form-0.1.1 lib/granite/form/model/associations/reflections/embeds_any.rb
granite-form-0.1.0 lib/granite/form/model/associations/reflections/embeds_any.rb