Sha256: b79dccc8e052406e92a7f8e7bc4cdfd9612bb9a74d36b18b7c9874793942fc47

Contents?: true

Size: 1.26 KB

Versions: 138

Compression:

Stored size: 1.26 KB

Contents

module ActiveRecord::Associations::Builder
  class Association #:nodoc:
    class_attribute :valid_options
    self.valid_options = [:class_name, :foreign_key, :select, :conditions, :include, :extend, :readonly, :validate]

    # Set by subclasses
    class_attribute :macro

    attr_reader :model, :name, :options, :reflection

    def self.build(model, name, options)
      new(model, name, options).build
    end

    def initialize(model, name, options)
      @model, @name, @options = model, name, options
    end

    def mixin
      @model.generated_feature_methods
    end

    def build
      validate_options
      reflection = model.create_reflection(self.class.macro, name, options, model)
      define_accessors
      reflection
    end

    private

      def validate_options
        options.assert_valid_keys(self.class.valid_options)
      end

      def define_accessors
        define_readers
        define_writers
      end

      def define_readers
        name = self.name
        mixin.redefine_method(name) do |*params|
          association(name).reader(*params)
        end
      end

      def define_writers
        name = self.name
        mixin.redefine_method("#{name}=") do |value|
          association(name).writer(value)
        end
      end
  end
end

Version data entries

138 entries across 107 versions & 14 rubygems

Version Path
activerecord-3.2.16 lib/active_record/associations/builder/association.rb
activerecord-3.2.15 lib/active_record/associations/builder/association.rb
activerecord-3.2.15.rc3 lib/active_record/associations/builder/association.rb
activerecord-3.2.15.rc2 lib/active_record/associations/builder/association.rb
activerecord-3.2.15.rc1 lib/active_record/associations/builder/association.rb
activerecord-3.2.14 lib/active_record/associations/builder/association.rb
activerecord-3.2.14.rc2 lib/active_record/associations/builder/association.rb
activerecord-3.2.14.rc1 lib/active_record/associations/builder/association.rb
swipe-rails-0.0.5 vendor/bundle/gems/activerecord-3.2.13/lib/active_record/associations/builder/association.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/builder/association.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/builder/association.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/builder/association.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/builder/association.rb
challah-1.0.0.beta3 vendor/bundle/gems/activerecord-3.2.13/lib/active_record/associations/builder/association.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/builder/association.rb
challah-1.0.0.beta2 vendor/bundle/gems/activerecord-3.2.13/lib/active_record/associations/builder/association.rb
challah-1.0.0.beta vendor/bundle/gems/activerecord-3.2.13/lib/active_record/associations/builder/association.rb
challah-1.0.0.beta vendor/bundle/gems/activerecord-3.2.11/lib/active_record/associations/builder/association.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/builder/association.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/builder/association.rb