diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2018-06-01 14:24:06 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-01 14:24:06 -0400 |
commit | c1844477a1461ffc9db3d899212f608c2a0c77c9 (patch) | |
tree | 204ef39c0a71ba5a96a79e6b18addd711aba1c0b /activerecord | |
parent | c61d67296c2e8a0682779c2f4eb9acea0c4b4a7e (diff) | |
parent | 6c7e6abfaad149da02dbec4e4f2bd62c5d68805f (diff) | |
download | rails-c1844477a1461ffc9db3d899212f608c2a0c77c9.tar.gz rails-c1844477a1461ffc9db3d899212f608c2a0c77c9.tar.bz2 rails-c1844477a1461ffc9db3d899212f608c2a0c77c9.zip |
Merge pull request #33018 from kddeisz/defined-attachments
ActiveStorage reflection
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 22d195c9a4..6d2f75a3ae 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -13,33 +13,37 @@ module ActiveRecord class_attribute :aggregate_reflections, instance_writer: false, default: {} end - def self.create(macro, name, scope, options, ar) - klass = \ - case macro - when :composed_of - AggregateReflection - when :has_many - HasManyReflection - when :has_one - HasOneReflection - when :belongs_to - BelongsToReflection - else - raise "Unsupported Macro: #{macro}" - end + class << self + def create(macro, name, scope, options, ar) + reflection = reflection_class_for(macro).new(name, scope, options, ar) + options[:through] ? ThroughReflection.new(reflection) : reflection + end - reflection = klass.new(name, scope, options, ar) - options[:through] ? ThroughReflection.new(reflection) : reflection - end + def add_reflection(ar, name, reflection) + ar.clear_reflections_cache + name = name.to_s + ar._reflections = ar._reflections.except(name).merge!(name => reflection) + end - def self.add_reflection(ar, name, reflection) - ar.clear_reflections_cache - name = name.to_s - ar._reflections = ar._reflections.except(name).merge!(name => reflection) - end + def add_aggregate_reflection(ar, name, reflection) + ar.aggregate_reflections = ar.aggregate_reflections.merge(name.to_s => reflection) + end - def self.add_aggregate_reflection(ar, name, reflection) - ar.aggregate_reflections = ar.aggregate_reflections.merge(name.to_s => reflection) + private + def reflection_class_for(macro) + case macro + when :composed_of + AggregateReflection + when :has_many + HasManyReflection + when :has_one + HasOneReflection + when :belongs_to + BelongsToReflection + else + raise "Unsupported Macro: #{macro}" + end + end end # \Reflection enables the ability to examine the associations and aggregations of |