Module: ActiveRecord::ClassMethods
- Included in:
- Base
- Defined in:
- lib/reactive_record/active_record/class_methods.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 56
def method_missing(name, *args, &block)
if args.count == 1 && name =~ /^find_by_/ && !block
find_by(name.gsub(/^find_by_/, "") => args[0])
else
raise "#{self.name}.#{name}(#{args}) (called class method missing)"
end
end
|
Instance Method Details
#_react_param_conversion(param, opt = nil) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 132
def _react_param_conversion(param, opt = nil)
param = Native(param)
param = JSON.from_object(param.to_n) if param.is_a? Native::Object
result = if param.is_a? self
param
elsif param.is_a? Hash
if opt == :validate_only
klass = ReactiveRecord::Base.infer_type_from_hash(self, param)
klass == self or klass < self
else
if param[primary_key]
target = find(param[primary_key])
else
target = new
end
ReactiveRecord::Base.load_from_json(Hash[param.collect { |key, value| [key, [value]] }], target)
target
end
else
nil
end
result
end
|
#abstract_class=(val) ⇒ Object
64
65
66
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 64
def abstract_class=(val)
@abstract_class = val
end
|
#abstract_class? ⇒ Boolean
19
20
21
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 19
def abstract_class?
defined?(@abstract_class) && @abstract_class == true
end
|
#all=(collection) ⇒ Object
82
83
84
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 82
def all=(collection)
ReactiveRecord::Base.class_scopes(self)[:all] = collection
end
|
#base_class ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 5
def base_class
unless self < Base
raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
end
if superclass == Base || superclass.abstract_class?
self
else
superclass.base_class
end
end
|
#column_names ⇒ Object
119
120
121
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 119
def column_names
[] end
|
#composed_of(name, opts = {}) ⇒ Object
115
116
117
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 115
def composed_of(name, opts = {})
Aggregations::AggregationReflection.new(base_class, :composed_of, name, opts)
end
|
#enum(*args) ⇒ Object
52
53
54
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 52
def enum(*args)
end
|
44
45
46
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 44
def find(id)
base_class.instance_eval {ReactiveRecord::Base.find(self, primary_key, id)}
end
|
#find_by(opts = {}) ⇒ Object
48
49
50
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 48
def find_by(opts = {})
base_class.instance_eval {ReactiveRecord::Base.find(self, opts.first.first, opts.first.last)}
end
|
#inheritance_column ⇒ Object
31
32
33
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 31
def inheritance_column
base_class.instance_eval {@inheritance_column_value || "type"}
end
|
#inheritance_column=(name) ⇒ Object
35
36
37
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 35
def inheritance_column=(name)
base_class.instance_eval {@inheritance_column_value = name}
end
|
#model_name ⇒ Object
39
40
41
42
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 39
def model_name
name
end
|
#primary_key ⇒ Object
23
24
25
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 23
def primary_key
base_class.instance_eval { @primary_key_value || :id }
end
|
#primary_key=(val) ⇒ Object
27
28
29
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 27
def primary_key=(val)
base_class.instance_eval { @primary_key_value = val }
end
|
#scope(name, body) ⇒ Object
68
69
70
71
72
73
74
75
76
|
# File 'lib/reactive_record/active_record/class_methods.rb', line 68
def scope(name, body)
singleton_class.send(:define_method, name) do | *args |
args = (args.count == 0) ? name : [name, *args]
ReactiveRecord::Base.class_scopes(self)[args] ||= ReactiveRecord::Collection.new(self, nil, nil, self, args)
end
singleton_class.send(:define_method, "#{name}=") do |collection|
ReactiveRecord::Base.class_scopes(self)[name] = collection
end
end
|