提交 825c05d4 编写于 作者: J Jon Leighton

Unprivatise all the things

Well, not all of them, but some of them.

I don't think there's much reason for these methods to be private.
上级 ea8181b6
...@@ -52,42 +52,40 @@ def valid_options ...@@ -52,42 +52,40 @@ def valid_options
Association.valid_options Association.valid_options
end end
private def validate_options
options.assert_valid_keys(valid_options)
end
def validate_options def define_accessors
options.assert_valid_keys(valid_options) define_readers
end define_writers
end
def define_accessors def define_readers
define_readers name = self.name
define_writers mixin.redefine_method(name) do |*params|
association(name).reader(*params)
end end
end
def define_readers def define_writers
name = self.name name = self.name
mixin.redefine_method(name) do |*params| mixin.redefine_method("#{name}=") do |value|
association(name).reader(*params) association(name).writer(value)
end
end end
end
def define_writers def validate_dependent_option(valid_options)
name = self.name unless valid_options.include? options[:dependent]
mixin.redefine_method("#{name}=") do |value| raise ArgumentError, "The :dependent option must be one of #{valid_options}, but is :#{options[:dependent]}"
association(name).writer(value)
end
end end
def validate_dependent_option(valid_options) if options[:dependent] == :restrict
unless valid_options.include? options[:dependent] ActiveSupport::Deprecation.warn(
raise ArgumentError, "The :dependent option must be one of #{valid_options}, but is :#{options[:dependent]}" "The :restrict option is deprecated. Please use :restrict_with_exception instead, which " \
end "provides the same functionality."
)
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
end end
end end
...@@ -21,71 +21,69 @@ def build ...@@ -21,71 +21,69 @@ def build
reflection reflection
end end
private def add_counter_cache_callbacks(reflection)
cache_column = reflection.counter_cache_column
def add_counter_cache_callbacks(reflection) name = self.name
cache_column = reflection.counter_cache_column
name = self.name method_name = "belongs_to_counter_cache_after_create_for_#{name}"
mixin.redefine_method(method_name) do
record = send(name)
record.class.increment_counter(cache_column, record.id) unless record.nil?
end
model.after_create(method_name)
method_name = "belongs_to_counter_cache_after_create_for_#{name}" method_name = "belongs_to_counter_cache_before_destroy_for_#{name}"
mixin.redefine_method(method_name) do mixin.redefine_method(method_name) do
unless marked_for_destruction?
record = send(name) record = send(name)
record.class.increment_counter(cache_column, record.id) unless record.nil? record.class.decrement_counter(cache_column, record.id) unless record.nil?
end
model.after_create(method_name)
method_name = "belongs_to_counter_cache_before_destroy_for_#{name}"
mixin.redefine_method(method_name) do
unless marked_for_destruction?
record = send(name)
record.class.decrement_counter(cache_column, record.id) unless record.nil?
end
end end
model.before_destroy(method_name)
model.send(:module_eval,
"#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)", __FILE__, __LINE__
)
end end
model.before_destroy(method_name)
model.send(:module_eval,
"#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)", __FILE__, __LINE__
)
end
def add_touch_callbacks(reflection) def add_touch_callbacks(reflection)
name = self.name name = self.name
method_name = "belongs_to_touch_after_save_or_destroy_for_#{name}" method_name = "belongs_to_touch_after_save_or_destroy_for_#{name}"
touch = options[:touch] touch = options[:touch]
mixin.redefine_method(method_name) do mixin.redefine_method(method_name) do
record = send(name) record = send(name)
unless record.nil? unless record.nil?
if touch == true if touch == true
record.touch record.touch
else else
record.touch(touch) record.touch(touch)
end
end end
end end
model.after_save(method_name)
model.after_touch(method_name)
model.after_destroy(method_name)
end end
def configure_dependency model.after_save(method_name)
if dependent = options[:dependent] model.after_touch(method_name)
validate_dependent_option [:destroy, :delete] model.after_destroy(method_name)
end
model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1) def configure_dependency
def #{dependency_method_name} if dependent = options[:dependent]
association(:#{name}).handle_dependency validate_dependent_option [:destroy, :delete]
end
eoruby
model.after_destroy dependency_method_name model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
end def #{dependency_method_name}
end association(:#{name}).handle_dependency
end
eoruby
def dependency_method_name model.after_destroy dependency_method_name
"belongs_to_dependent_for_#{name}"
end end
end
def dependency_method_name
"belongs_to_dependent_for_#{name}"
end
end end
end end
...@@ -34,53 +34,51 @@ def show_deprecation_warnings ...@@ -34,53 +34,51 @@ def show_deprecation_warnings
end end
end end
private def wrap_block_extension
if block_extension
def wrap_block_extension @extension_module = mod = Module.new(&block_extension)
if block_extension silence_warnings do
@extension_module = mod = Module.new(&block_extension) model.parent.const_set(extension_module_name, mod)
silence_warnings do
model.parent.const_set(extension_module_name, mod)
end
prev_scope = @scope
if prev_scope
@scope = proc { |owner| instance_exec(owner, &prev_scope).extending(mod) }
else
@scope = proc { extending(mod) }
end
end end
end
def extension_module_name prev_scope = @scope
@extension_module_name ||= "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
if prev_scope
@scope = proc { |owner| instance_exec(owner, &prev_scope).extending(mod) }
else
@scope = proc { extending(mod) }
end
end end
end
def define_callback(callback_name) def extension_module_name
full_callback_name = "#{callback_name}_for_#{name}" @extension_module_name ||= "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
end
# TODO : why do i need method_defined? I think its because of the inheritance chain def define_callback(callback_name)
model.class_attribute full_callback_name.to_sym unless model.method_defined?(full_callback_name) full_callback_name = "#{callback_name}_for_#{name}"
model.send("#{full_callback_name}=", Array(options[callback_name.to_sym]))
end
def define_readers # TODO : why do i need method_defined? I think its because of the inheritance chain
super model.class_attribute full_callback_name.to_sym unless model.method_defined?(full_callback_name)
model.send("#{full_callback_name}=", Array(options[callback_name.to_sym]))
end
name = self.name def define_readers
mixin.redefine_method("#{name.to_s.singularize}_ids") do super
association(name).ids_reader
end name = self.name
mixin.redefine_method("#{name.to_s.singularize}_ids") do
association(name).ids_reader
end end
end
def define_writers def define_writers
super super
name = self.name name = self.name
mixin.redefine_method("#{name.to_s.singularize}_ids=") do |ids| mixin.redefine_method("#{name.to_s.singularize}_ids=") do |ids|
association(name).ids_writer(ids) association(name).ids_writer(ids)
end
end end
end
end end
end end
...@@ -24,18 +24,16 @@ def show_deprecation_warnings ...@@ -24,18 +24,16 @@ def show_deprecation_warnings
end end
end end
private def define_destroy_hook
name = self.name
def define_destroy_hook model.send(:include, Module.new {
name = self.name class_eval <<-RUBY, __FILE__, __LINE__ + 1
model.send(:include, Module.new { def destroy_associations
class_eval <<-RUBY, __FILE__, __LINE__ + 1 association(#{name.to_sym.inspect}).delete_all
def destroy_associations super
association(#{name.to_sym.inspect}).delete_all end
super RUBY
end })
RUBY end
})
end
end end
end end
...@@ -15,23 +15,21 @@ def build ...@@ -15,23 +15,21 @@ def build
reflection reflection
end end
private def configure_dependency
if dependent = options[:dependent]
validate_dependent_option [:destroy, :delete_all, :nullify, :restrict, :restrict_with_error, :restrict_with_exception]
def configure_dependency name = self.name
if dependent = options[:dependent] mixin.redefine_method(dependency_method_name) do
validate_dependent_option [:destroy, :delete_all, :nullify, :restrict, :restrict_with_error, :restrict_with_exception] association(name).handle_dependency
name = self.name
mixin.redefine_method(dependency_method_name) do
association(name).handle_dependency
end
model.before_destroy dependency_method_name
end end
end
def dependency_method_name model.before_destroy dependency_method_name
"has_many_dependent_for_#{name}"
end end
end
def dependency_method_name
"has_many_dependent_for_#{name}"
end
end end
end end
...@@ -21,23 +21,21 @@ def build ...@@ -21,23 +21,21 @@ def build
reflection reflection
end end
private def configure_dependency
if dependent = options[:dependent]
validate_dependent_option [:destroy, :delete, :nullify, :restrict, :restrict_with_error, :restrict_with_exception]
def configure_dependency name = self.name
if dependent = options[:dependent] mixin.redefine_method(dependency_method_name) do
validate_dependent_option [:destroy, :delete, :nullify, :restrict, :restrict_with_error, :restrict_with_exception] association(name).handle_dependency
name = self.name
mixin.redefine_method(dependency_method_name) do
association(name).handle_dependency
end
model.before_destroy dependency_method_name
end end
end
def dependency_method_name model.before_destroy dependency_method_name
"has_one_dependent_for_#{name}"
end end
end
def dependency_method_name
"has_one_dependent_for_#{name}"
end
end end
end end
...@@ -13,22 +13,20 @@ def define_accessors ...@@ -13,22 +13,20 @@ def define_accessors
define_constructors if constructable? define_constructors if constructable?
end end
private def define_constructors
name = self.name
def define_constructors mixin.redefine_method("build_#{name}") do |*params, &block|
name = self.name association(name).build(*params, &block)
end
mixin.redefine_method("build_#{name}") do |*params, &block|
association(name).build(*params, &block)
end
mixin.redefine_method("create_#{name}") do |*params, &block| mixin.redefine_method("create_#{name}") do |*params, &block|
association(name).create(*params, &block) association(name).create(*params, &block)
end end
mixin.redefine_method("create_#{name}!") do |*params, &block| mixin.redefine_method("create_#{name}!") do |*params, &block|
association(name).create!(*params, &block) association(name).create!(*params, &block)
end
end end
end
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册