Skip to content

Commit

Permalink
Extract readonly_attribute?
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed May 26, 2019
1 parent a1c269f commit b55f5a3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
6 changes: 1 addition & 5 deletions activerecord/lib/active_record/attribute_methods.rb
Expand Up @@ -437,7 +437,7 @@ def attributes_with_values(attribute_names)
def attributes_for_update(attribute_names)
attribute_names &= self.class.column_names
attribute_names.delete_if do |name|
readonly_attribute?(name)
self.class.readonly_attribute?(name)
end
end

Expand All @@ -460,10 +460,6 @@ def format_for_inspect(value)
end
end

def readonly_attribute?(name)
self.class.readonly_attributes.include?(name)
end

This comment has been minimized.

Copy link
@bf4

bf4 Dec 27, 2021

Contributor

Came across this change upgrading from Rails 6.0 to 6.1

I had relied on overriding this instance method in Rails <= 6.0 to, in super special circumstances, return 'false' and thereby skip this check.

May I may an issue or PR to restore an instance method readonly_attribute? (which calls the class method?)

I think it's reasonable since the instance method verify_readonly_attribute? still exists and delegates to a class method

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Jan 5, 2022

Member

This method is private API of the framework. We never exposed it and I don't think it makes sense for us to keep it.

This comment has been minimized.

Copy link
@bf4

bf4 Jan 6, 2022

Contributor

Thanks for getting back. Oh well


def pk_attribute?(name)
name == @primary_key
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/persistence.rb
Expand Up @@ -939,7 +939,7 @@ def _create_record(attribute_names = self.attribute_names)
end

def verify_readonly_attribute(name)
raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attribute?(name)
end

def _raise_record_not_destroyed
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/readonly_attributes.rb
Expand Up @@ -19,6 +19,10 @@ def attr_readonly(*attributes)
def readonly_attributes
_attr_readonly
end

def readonly_attribute?(name) # :nodoc:
_attr_readonly.include?(name)
end
end
end
end

0 comments on commit b55f5a3

Please sign in to comment.