-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring attributes/types [#3348 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
- Loading branch information
Showing
25 changed files
with
760 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
module ActiveRecord | |||
module Attributes | |||
|
|||
# Returns true if the given attribute is in the attributes hash | |||
def has_attribute?(attr_name) | |||
_attributes.key?(attr_name) | |||
end | |||
|
|||
# Returns an array of names for the attributes available on this object sorted alphabetically. | |||
def attribute_names | |||
_attributes.keys.sort! | |||
end | |||
|
|||
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values. | |||
def attributes | |||
attributes = _attributes.dup | |||
attributes.typecast! unless _attributes.frozen? | |||
attributes.to_h | |||
end | |||
|
|||
protected | |||
|
|||
# Not to be confused with the public #attributes method, which returns a typecasted Hash. | |||
def _attributes | |||
@attributes | |||
end | |||
|
|||
def initialize_attribute_store(merge_attributes = nil) | |||
@attributes = ActiveRecord::Attributes::Store.new | |||
@attributes.merge!(merge_attributes) if merge_attributes | |||
@attributes.types.merge!(self.class.attribute_types) | |||
@attributes.aliases.merge!('id' => self.class.primary_key) unless 'id' == self.class.primary_key | |||
@attributes | |||
end | |||
|
|||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
module ActiveRecord | |||
module Attributes | |||
module Aliasing | |||
# Allows access to keys using aliased names. | |||
# | |||
# Example: | |||
# class Attributes < Hash | |||
# include Aliasing | |||
# end | |||
# | |||
# attributes = Attributes.new | |||
# attributes.aliases['id'] = 'fancy_primary_key' | |||
# attributes['fancy_primary_key'] = 2020 | |||
# | |||
# attributes['id'] | |||
# => 2020 | |||
# | |||
# Additionally, symbols are always aliases of strings: | |||
# attributes[:fancy_primary_key] | |||
# => 2020 | |||
# | |||
def [](key) | |||
super(unalias(key)) | |||
end | |||
|
|||
def []=(key, value) | |||
super(unalias(key), value) | |||
end | |||
|
|||
def aliases | |||
@aliases ||= {} | |||
end | |||
|
|||
def unalias(key) | |||
key = key.to_s | |||
aliases[key] || key | |||
end | |||
|
|||
end | |||
end | |||
end | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,15 @@ | |||
module ActiveRecord | |||
module Attributes | |||
class Store < Hash | |||
include ActiveRecord::Attributes::Typecasting | |||
include ActiveRecord::Attributes::Aliasing | |||
|
|||
# Attributes not mapped to a column are handled using Type::Unknown, | |||
# which enables boolean typecasting for unmapped keys. | |||
def types | |||
@types ||= Hash.new(Type::Unknown.new) | |||
end | |||
|
|||
end | |||
end | |||
end |
Oops, something went wrong.
f936a1f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change causes decimal objects to be treated as strings, leading to errors in existing applications such as "comparison of String with Float failed" in validation routines such as a the following:
Full scenario and captured output here:
http://intertwingly.net/projects/AWDwR3/checkdepot.html#section-6.4
f936a1f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ticket 3387