Sha256: 612bc7905a791a5f856bd5bf1dcce6760b055793196e6f11c1e618c5f11226f9

Contents?: true

Size: 1.2 KB

Versions: 55

Compression:

Stored size: 1.2 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 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

        model.redefine_method(name) do |*params|
          association(name).reader(*params)
        end
      end

      def define_writers
        name = self.name

        model.redefine_method("#{name}=") do |value|
          association(name).writer(value)
        end
      end
  end
end

Version data entries

55 entries across 55 versions & 2 rubygems

Version Path
activerecord-3.1.5 lib/active_record/associations/builder/association.rb
activerecord-3.1.5.rc1 lib/active_record/associations/builder/association.rb
dirty_history-0.5.3 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.5.2 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
activerecord-3.1.4 lib/active_record/associations/builder/association.rb
activerecord-3.1.4.rc1 lib/active_record/associations/builder/association.rb
dirty_history-0.5.1 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.5.0 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.4.10 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.4.9 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.4.8 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.4.7 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.4.6 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.4.5 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.4.4 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.4.3 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
dirty_history-0.4.2 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb
activerecord-3.1.3 lib/active_record/associations/builder/association.rb
activerecord-3.1.2 lib/active_record/associations/builder/association.rb
activerecord-3.1.2.rc2 lib/active_record/associations/builder/association.rb