Sha256: 524131a0b33e787e51c721bf0333594a7b3ab5134192eed2af2d7f7d6b298a15

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

module Superstore
  module Associations
    class Reflection
      attr_reader :macro, :name, :model, :options
      def initialize(macro, name, model, options)
        @macro  = macro
        @name   = name
        @model  = model
        @options = options
      end

      def association_class
        case macro
        when :belongs_to
          Superstore::Associations::BelongsTo
        when :has_many
          Superstore::Associations::HasMany
        when :has_one
          Superstore::Associations::HasOne
        end
        
      end

      def instance_variable_name
        "@#{name}"
      end

      def foreign_key
        @foreign_key ||= options[:foreign_key] || derive_foreign_key
      end

      def primary_key
        options[:primary_key] || "id"
      end

      def default_primary_key?
        primary_key == "id"
      end

      def polymorphic_column
        "#{name}_type"
      end

      def polymorphic?
        options[:polymorphic]
      end

      def class_name
        @class_name ||= (options[:class_name] || name.to_s.classify)
      end

      private
      
      def derive_foreign_key
        case macro
        when :has_many, :has_one
          model.name.foreign_key
        when :belongs_to
          "#{name}_id"
        end
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
superstore-2.4.4 lib/superstore/associations/reflection.rb
superstore-2.4.3 lib/superstore/associations/reflection.rb
superstore-2.4.2 lib/superstore/associations/reflection.rb
superstore-2.4.1 lib/superstore/associations/reflection.rb
superstore-2.4.0 lib/superstore/associations/reflection.rb
superstore-2.3.0 lib/superstore/associations/reflection.rb
superstore-2.2.0 lib/superstore/associations/reflection.rb
superstore-2.1.3 lib/superstore/associations/reflection.rb
superstore-2.1.2 lib/superstore/associations/reflection.rb
superstore-2.1.1 lib/superstore/associations/reflection.rb
superstore-2.1.0 lib/superstore/associations/reflection.rb
superstore-2.0.1 lib/superstore/associations/reflection.rb
superstore-2.0.0 lib/superstore/associations/reflection.rb