Module: ActiveRecord::ActsAs::InstanceMethods
- Defined in:
- lib/active_record/acts_as/instance_methods.rb
Instance Method Summary
collapse
-
#_write_attribute(attr_name, value, *args, &block) ⇒ Object
-
#acting_as?(other = nil) ⇒ Boolean
-
#attribute_names ⇒ Object
-
#attributes ⇒ Object
-
#changed? ⇒ Boolean
-
#column_for_attribute(name) ⇒ Object
-
#dup ⇒ Object
-
#has_attribute?(attr_name, as_original_class = false) ⇒ Boolean
-
#is_a?(klass) ⇒ Boolean
-
#method_missing(method, *args, &block) ⇒ Object
-
#read_attribute(attr_name, *args, &block) ⇒ Object
-
#read_store_attribute(store_attribute, key) ⇒ Object
-
#respond_to?(name, include_private = false, as_original_class = false) ⇒ Boolean
-
#saved_changes? ⇒ Boolean
-
#self_respond_to?(name, include_private = false) ⇒ Boolean
-
#touch(*args, time: nil) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
120
121
122
123
124
125
126
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 120
def method_missing(method, *args, &block)
if !self_respond_to?(method) && acting_as.respond_to?(method)
acting_as.send(method, *args, &block)
else
super
end
end
|
Instance Method Details
#_write_attribute(attr_name, value, *args, &block) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 47
def _write_attribute(attr_name, value, *args, &block)
if attribute_method?(attr_name.to_s)
super
else
acting_as.send(:_write_attribute, attr_name, value, *args, &block)
end
end
|
#acting_as?(other = nil) ⇒ Boolean
4
5
6
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 4
def acting_as?(other = nil)
self.class.acting_as? other
end
|
#attribute_names ⇒ Object
76
77
78
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 76
def attribute_names
super | (acting_as.attribute_names - [acting_as_reflection.type, acting_as_reflection.foreign_key])
end
|
#attributes ⇒ Object
72
73
74
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 72
def attributes
acting_as.attributes.except(acting_as_reflection.type, acting_as_reflection.foreign_key).merge(super)
end
|
#changed? ⇒ Boolean
16
17
18
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 16
def changed?
super || acting_as.changed? || (defined?(@_acting_as_changed) ? @_acting_as_changed : false)
end
|
#column_for_attribute(name) ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 88
def column_for_attribute(name)
if has_attribute?(name, true)
super(name)
else
acting_as.column_for_attribute(name)
end
end
|
#dup ⇒ Object
114
115
116
117
118
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 114
def dup
duplicate = super
duplicate.acting_as = acting_as.dup
duplicate
end
|
#has_attribute?(attr_name, as_original_class = false) ⇒ Boolean
80
81
82
83
84
85
86
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 80
def has_attribute?(attr_name, as_original_class = false)
if as_original_class
super(attr_name)
else
super(attr_name) || acting_as.has_attribute?(attr_name)
end
end
|
#is_a?(klass) ⇒ Boolean
8
9
10
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 8
def is_a?(klass)
super || acting_as?(klass)
end
|
#read_attribute(attr_name, *args, &block) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 31
def read_attribute(attr_name, *args, &block)
if attribute_method?(attr_name.to_s)
super
else
acting_as.read_attribute(attr_name, *args, &block)
end
end
|
#read_store_attribute(store_attribute, key) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 55
def read_store_attribute(store_attribute, key)
if attribute_method?(store_attribute.to_s)
super
else
acting_as.send(:read_store_attribute, store_attribute, key)
end
end
|
#respond_to?(name, include_private = false, as_original_class = false) ⇒ Boolean
102
103
104
105
106
107
108
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 102
def respond_to?(name, include_private = false, as_original_class = false)
if as_original_class
super(name, include_private)
else
super(name, include_private) || acting_as.respond_to?(name)
end
end
|
#saved_changes? ⇒ Boolean
12
13
14
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 12
def saved_changes?
super || acting_as.has_changes_to_save? || (defined?(@_acting_as_changed) ? @_acting_as_changed : false)
end
|
#self_respond_to?(name, include_private = false) ⇒ Boolean
110
111
112
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 110
def self_respond_to?(name, include_private = false)
respond_to? name, include_private, true
end
|
#touch(*args, time: nil) ⇒ Object
96
97
98
99
100
|
# File 'lib/active_record/acts_as/instance_methods.rb', line 96
def touch(*args, time: nil)
self_args, acting_as_args = args.partition { |arg| has_attribute?(arg, true) }
super(*self_args, time: time) if self_args.any?
acting_as.touch(*acting_as_args, time: time) if acting_as.persisted?
end
|