aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-12-08 13:13:20 -0500
committerSean Griffin <sean@seantheprogrammer.com>2016-12-08 13:13:51 -0500
commite2933a7e3849d19f28971eb917029b951ca363ec (patch)
treef188bde63e70b3e5b6b680540705d373ccba07a2 /activerecord
parent55ebf6c1accb8df2db7ab0c76bf3a179f665bb3b (diff)
downloadrails-e2933a7e3849d19f28971eb917029b951ca363ec.tar.gz
rails-e2933a7e3849d19f28971eb917029b951ca363ec.tar.bz2
rails-e2933a7e3849d19f28971eb917029b951ca363ec.zip
Add some documentation around internal APIs [ci skip]
While these method aren't public API, they are a non-trivial internal API which warrant a bit of explanation.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/attribute_decorators.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/attribute_decorators.rb b/activerecord/lib/active_record/attribute_decorators.rb
index 340dfe11cf..c39e9ce4c5 100644
--- a/activerecord/lib/active_record/attribute_decorators.rb
+++ b/activerecord/lib/active_record/attribute_decorators.rb
@@ -8,12 +8,34 @@ module ActiveRecord
end
module ClassMethods # :nodoc:
+ # This method is an internal API used to create class macros such as
+ # +serialize+, and features like time zone aware attributes.
+ #
+ # Used to wrap the type of an attribute in a new type.
+ # When the schema for a model is loaded, attributes with the same name as
+ # +column_name+ will have their type yielded to the given block. The
+ # return value of that block will be used instead.
+ #
+ # Subsequent calls where +column_name+ and +decorator_name+ are the same
+ # will override the previous decorator, not decorate twice. This can be
+ # used to create idempotent class macros like +serialize+
def decorate_attribute_type(column_name, decorator_name, &block)
matcher = ->(name, _) { name == column_name.to_s }
key = "_#{column_name}_#{decorator_name}"
decorate_matching_attribute_types(matcher, key, &block)
end
+ # This method is an internal API used to create higher level features like
+ # time zone aware attributes.
+ #
+ # When the schema for a model is loaded, +matcher+ will be called for each
+ # attribute with its name and type. If the matcher returns a truthy value,
+ # the type will then be yielded to the given block, and the return value
+ # of that block will replace the type.
+ #
+ # Subsequent calls to this method with the same value for +decorator_name+
+ # will replace the previous decorator, not decorate twice. This can be
+ # used to ensure that class macros are idempotent.
def decorate_matching_attribute_types(matcher, decorator_name, &block)
reload_schema_from_cache
decorator_name = decorator_name.to_s