Module: ActiveRecord::ActsAs::Relation::ClassMethods
- Defined in:
- lib/active_record/acts_as/relation.rb
Class Method Summary collapse
Instance Method Summary collapse
- #actable(scope = nil, **options) ⇒ Object
- #actable? ⇒ Boolean
- #acting_as?(other = nil) ⇒ Boolean
- #acts_as(name, scope = nil, options = {}) ⇒ Object
- #is_a?(klass) ⇒ Boolean
Class Method Details
.callable_by_submodel(method) ⇒ Object
94 95 96 97 |
# File 'lib/active_record/acts_as/relation.rb', line 94 def self.callable_by_submodel(method) @methods_callable_by_submodel ||= Set.new @methods_callable_by_submodel << method end |
.methods_callable_by_submodel ⇒ Object
90 91 92 |
# File 'lib/active_record/acts_as/relation.rb', line 90 def self.methods_callable_by_submodel @methods_callable_by_submodel ||= Set.new end |
.scope ⇒ Object
99 100 101 |
# File 'lib/active_record/acts_as/relation.rb', line 99 def self.scope(*) super.tap(&method(:callable_by_submodel)) end |
Instance Method Details
#actable(scope = nil, **options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/active_record/acts_as/relation.rb', line 79 def actable(scope = nil, **) name = .delete(:as) || :actable reflections = belongs_to(name, scope, **.reverse_merge(validate: false, polymorphic: true, dependent: :destroy, autosave: true, inverse_of: to_s.underscore)) cattr_reader(:actable_reflection) { reflections.stringify_keys[name.to_s] } def self.methods_callable_by_submodel @methods_callable_by_submodel ||= Set.new end def self.callable_by_submodel(method) @methods_callable_by_submodel ||= Set.new @methods_callable_by_submodel << method end def self.scope(*) super.tap(&method(:callable_by_submodel)) end alias_method :specific, name end |
#actable? ⇒ Boolean
106 107 108 109 |
# File 'lib/active_record/acts_as/relation.rb', line 106 def actable? respond_to?(:actable_reflection) && actable_reflection.is_a?(ActiveRecord::Reflection::AssociationReflection) end |
#acting_as?(other = nil) ⇒ Boolean
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active_record/acts_as/relation.rb', line 59 def acting_as?(other = nil) if respond_to?(:acting_as_reflection) && acting_as_reflection.is_a?(ActiveRecord::Reflection::AssociationReflection) case other when Class acting_as_reflection.class_name == other.to_s when Symbol, String acting_as_reflection.class_name.underscore == other.to_s when NilClass true end else false end end |
#acts_as(name, scope = nil, options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_record/acts_as/relation.rb', line 7 def acts_as(name, scope = nil, = {}) , scope = scope, nil if Hash === scope association_method = .delete(:association_method) touch = .delete(:touch) as = .delete(:as) || :actable validates_actable = !.key?(:validates_actable) || .delete(:validates_actable) = .reverse_merge(as: as, validate: false, autosave: true, inverse_of: as) reflections = has_one(name, scope, **) default_scope -> { case association_method when :eager_load eager_load(name) when :joins joins(name) else includes(name) end } validate :actable_must_be_valid if validates_actable unless touch == false after_update :touch, if: :saved_changes? end before_save do @_acting_as_changed = acting_as.has_changes_to_save? true end after_commit do @_acting_as_changed = nil end # Workaround for https://github.com/rails/rails/issues/13609 after_destroy do acting_as.destroy if acting_as && !acting_as.destroyed? end cattr_reader(:acting_as_reflection) { reflections.stringify_keys[name.to_s] } cattr_reader(:acting_as_name) { name.to_s } cattr_reader(:acting_as_model) { ([:class_name] || name.to_s.camelize).constantize } class_eval "def #{name}; super || build_#{name}(acting_as_model.actable_reflection.name => self); end" alias_method :acting_as, name alias_method :acting_as=, "#{name}=".to_sym include ActsAs::InstanceMethods singleton_class.module_eval do include ActsAs::ClassMethods end end |
#is_a?(klass) ⇒ Boolean
75 76 77 |
# File 'lib/active_record/acts_as/relation.rb', line 75 def is_a?(klass) super || acting_as?(klass) end |