mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
AR::AttributeMethods does not need to be included in an AR::Base class.
This commit is contained in:
parent
ce76cfc61f
commit
30679bc705
2 changed files with 67 additions and 1 deletions
|
@ -18,7 +18,11 @@ module ActiveRecord
|
||||||
# method is defined by Active Record though.
|
# method is defined by Active Record though.
|
||||||
def instance_method_already_implemented?(method_name)
|
def instance_method_already_implemented?(method_name)
|
||||||
method_name = method_name.to_s
|
method_name = method_name.to_s
|
||||||
@_defined_class_methods ||= ancestors.first(ancestors.index(ActiveRecord::Base)).sum([]) { |m| m.instance_methods(false) | m.private_instance_methods(false) }.map {|m| m.to_s }.to_set
|
index = ancestors.index(ActiveRecord::Base) || ancestors.length
|
||||||
|
@_defined_class_methods ||= ancestors.first(index).map { |m|
|
||||||
|
m.instance_methods(false) | m.private_instance_methods(false)
|
||||||
|
}.flatten.map {|m| m.to_s }.to_set
|
||||||
|
|
||||||
@@_defined_activerecord_methods ||= defined_activerecord_methods
|
@@_defined_activerecord_methods ||= defined_activerecord_methods
|
||||||
raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name)
|
raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name)
|
||||||
@_defined_class_methods.include?(method_name)
|
@_defined_class_methods.include?(method_name)
|
||||||
|
|
62
activerecord/test/cases/attribute_methods/read_test.rb
Normal file
62
activerecord/test/cases/attribute_methods/read_test.rb
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
require "cases/helper"
|
||||||
|
|
||||||
|
module ActiveRecord
|
||||||
|
module AttributeMethods
|
||||||
|
class ReadTest < ActiveRecord::TestCase
|
||||||
|
class FakeColumn < Struct.new(:name)
|
||||||
|
def type_cast_code(var)
|
||||||
|
var
|
||||||
|
end
|
||||||
|
|
||||||
|
def type; :integer; end
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
@klass = Class.new do
|
||||||
|
include ActiveRecord::AttributeMethods
|
||||||
|
include ActiveRecord::AttributeMethods::Read
|
||||||
|
|
||||||
|
def self.column_names
|
||||||
|
%w{ one two three }
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.primary_key
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.columns
|
||||||
|
column_names.map { FakeColumn.new(name) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.columns_hash
|
||||||
|
puts caller
|
||||||
|
Hash[column_names.map { |name|
|
||||||
|
[name, FakeColumn.new(name)]
|
||||||
|
}]
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.serialized_attributes; {}; end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_define_attribute_methods
|
||||||
|
instance = @klass.new
|
||||||
|
|
||||||
|
@klass.column_names.each do |name|
|
||||||
|
assert ! instance.methods.map(&:to_s).include?(name)
|
||||||
|
end
|
||||||
|
|
||||||
|
@klass.define_attribute_methods
|
||||||
|
|
||||||
|
@klass.column_names.each do |name|
|
||||||
|
assert(instance.methods.map(&:to_s).include?(name), "#{name} is not defined")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_attribute_methods_generated?
|
||||||
|
assert(!@klass.attribute_methods_generated?, 'attribute_methods_generated?')
|
||||||
|
@klass.define_attribute_methods
|
||||||
|
assert(@klass.attribute_methods_generated?, 'attribute_methods_generated?')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue