Sha256: 430ca378151600075d6bcd63020ec4cdc8d08f3c4a3fddaa5a3805da976c236c

Contents?: true

Size: 881 Bytes

Versions: 12

Compression:

Stored size: 881 Bytes

Contents

module Superstore::Associations::Builder
  class Association
    def self.build(model, name, options)
      new(model, name, options).build
    end

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

    def build
      define_writer
      define_reader

      reflection = Superstore::Associations::Reflection.new(macro, name, model, options)
      ActiveRecord::Reflection.add_reflection model, name, reflection
    end

    def mixin
      model.generated_association_methods
    end

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

    def define_reader
      name = self.name
      mixin.redefine_method(name) do
        association(name).reader
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
superstore-2.5.0 lib/superstore/associations/builder/association.rb
superstore-2.4.4 lib/superstore/associations/builder/association.rb
superstore-2.4.3 lib/superstore/associations/builder/association.rb
superstore-2.4.2 lib/superstore/associations/builder/association.rb
superstore-2.4.1 lib/superstore/associations/builder/association.rb
superstore-2.4.0 lib/superstore/associations/builder/association.rb
superstore-2.3.0 lib/superstore/associations/builder/association.rb
superstore-2.2.0 lib/superstore/associations/builder/association.rb
superstore-2.1.3 lib/superstore/associations/builder/association.rb
superstore-2.1.2 lib/superstore/associations/builder/association.rb
superstore-2.1.1 lib/superstore/associations/builder/association.rb
superstore-2.1.0 lib/superstore/associations/builder/association.rb