Sha256: 1a664bcd80a5e6c54d0097bafb565edcacbd5eed105fbb4291ae0d9a23e20eaa

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

module Granite
  module Form
    module Model
      module Associations
        module Reflections
          class ReferencesAny < Base
            def self.build(_target, generated_methods, name, *args)
              reflection = new(name, *args)
              generate_methods name, generated_methods
              reflection
            end

            def self.persistence_adapter(klass)
              adapter = klass.granite_persistence_adapter if klass.respond_to?(:granite_persistence_adapter)
              adapter or raise PersistenceAdapterMissing, klass
            end

            delegate :primary_key, to: :persistence_adapter

            def initialize(name, *args)
              @options = args.extract_options!
              @scope_proc = args.first
              @name = name.to_sym
            end

            def klass
              @klass ||= if options[:data_source].present?
                options[:data_source].to_s.constantize
              else
                super
              end
            end

            alias_method :data_source, :klass

            def persistence_adapter
              @persistence_adapter ||= self.class.persistence_adapter(klass)
                .new(data_source, options[:primary_key], @scope_proc)
            end

            def read_source(object)
              object.read_attribute(reference_key)
            end

            def write_source(object, value)
              object.write_attribute(reference_key, value)
            end

            def embedded?
              false
            end

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

            def autosave?
              !!options[:autosave]
            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/references_any.rb
granite-form-0.1.1 lib/granite/form/model/associations/reflections/references_any.rb
granite-form-0.1.0 lib/granite/form/model/associations/reflections/references_any.rb