Sha256: 7a4b8804af9407d30f2011e101e7540b9767ab8e6caae04ed1d907d1fac6354f

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 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 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
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
granite-form-0.6.0 lib/granite/form/model/associations/reflections/references_any.rb