Skip to content

Commit

Permalink
Makes validates_acceptance_of to not override database fields [#4460
Browse files Browse the repository at this point in the history
…state:committed]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
spastorino authored and josevalim committed Apr 28, 2010
1 parent b9ab4c7 commit ce48b31
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions activemodel/lib/active_model/validations.rb
Expand Up @@ -133,6 +133,9 @@ def validators_on(attribute)
_validators[attribute.to_sym]
end

def attribute_method?(attribute)
method_defined?(attribute)
end
private

def _merge_attributes(attr_names)
Expand Down
6 changes: 4 additions & 2 deletions activemodel/lib/active_model/validations/acceptance.rb
Expand Up @@ -14,8 +14,10 @@ def validate_each(record, attribute, value)
def setup(klass)
# Note: instance_methods.map(&:to_s) is important for 1.9 compatibility
# as instance_methods returns symbols unlike 1.8 which returns strings.
new_attributes = attributes.reject { |name| klass.instance_methods.map(&:to_s).include?("#{name}=") }
klass.send(:attr_accessor, *new_attributes)
attr_readers = attributes.reject { |name| klass.attribute_method?(name) }
attr_writers = attributes.reject { |name| klass.attribute_method?("#{name}=") }
klass.send(:attr_reader, *attr_readers)
klass.send(:attr_writer, *attr_writers)
end
end

Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -931,6 +931,10 @@ def reset_column_information_and_inheritable_attributes_for_all_subclasses#:nodo
subclasses.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information }
end

def attribute_method?(attribute)
super || column_names.include?(attribute.to_s.sub(/=$/, ''))
end

# Set the lookup ancestors for ActiveModel.
def lookup_ancestors #:nodoc:
klass = self
Expand Down

0 comments on commit ce48b31

Please sign in to comment.