Sha256: 2a19997f0ff5262da64314e46675b4f069cd6f582b31f951aac381045fed16ee

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

module DataMapper
  module Associations
    
    class HasNAssociation
      
      attr_reader :adapter, :table, :options
      
      OPTIONS = [
        :class,
        :class_name,
        :foreign_key
      ]
      
      def initialize(klass, association_name, options)
        @adapter = database.adapter
        @table = adapter.table(klass)
        @association_name = association_name.to_sym
        @options = options || Hash.new
        
        define_accessor(klass)
      end
      
      def name
        @association_name
      end

      def constant
        @associated_class || @associated_class = if @options.has_key?(:class) || @options.has_key?(:class_name)
          associated_class_name = (@options[:class] || @options[:class_name])
          if associated_class_name.kind_of?(String)
            Kernel.const_get(Inflector.classify(associated_class_name))
          else
            associated_class_name
          end
        else            
          Kernel.const_get(Inflector.classify(@association_name))
        end
      end
      
      def foreign_key
        @foreign_key || @foreign_key = begin
          association_table[@options[:foreign_key] || table.default_foreign_key]
        end
      end
      
      def association_table
        @association_table || (@association_table = adapter.table(constant))
      end
      
      def to_sql
        "JOIN #{association_table.to_sql} ON #{foreign_key.to_sql(true)} = #{table.key.to_sql(true)}"
      end
      
      def association_columns
        association_table.columns.reject { |column| column.lazy? }
      end
      
      def finder_options
        @finder_options || @finder_options = @options.reject { |k,v| self.class::OPTIONS.include?(k) }
      end
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
datamapper-0.2.0 lib/data_mapper/associations/has_n_association.rb
datamapper-0.2.1 lib/data_mapper/associations/has_n_association.rb
datamapper-0.2.2 lib/data_mapper/associations/has_n_association.rb
datamapper-0.2.3 lib/data_mapper/associations/has_n_association.rb