Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: ea8181b656
Fetching contributors…

Cannot retrieve contributors at this time

94 lines (75 sloc) 2.188 kB
module ActiveRecord::Associations::Builder
class Association #:nodoc:
class << self
attr_accessor :valid_options
end
self.valid_options = [:class_name, :foreign_key, :validate]
attr_reader :model, :name, :scope, :options, :reflection
def self.build(*args, &block)
new(*args, &block).build
end
def initialize(model, name, scope, options)
@model = model
@name = name
if scope.is_a?(Hash)
@scope = nil
@options = scope
else
@scope = scope
@options = options
end
if @scope && @scope.arity == 0
prev_scope = @scope
@scope = proc { instance_exec(&prev_scope) }
end
end
def mixin
@model.generated_feature_methods
end
include Module.new { def build; end }
def build
validate_options
define_accessors
@reflection = model.create_reflection(macro, name, scope, options, model)
super # provides an extension point
@reflection
end
def macro
raise NotImplementedError
end
def valid_options
Association.valid_options
end
private
def validate_options
options.assert_valid_keys(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
def validate_dependent_option(valid_options)
unless valid_options.include? options[:dependent]
raise ArgumentError, "The :dependent option must be one of #{valid_options}, but is :#{options[:dependent]}"
end
if options[:dependent] == :restrict
ActiveSupport::Deprecation.warn(
"The :restrict option is deprecated. Please use :restrict_with_exception instead, which " \
"provides the same functionality."
)
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.