Module: Datasource::Adapters::ActiveRecord
- Defined in:
- lib/datasource/adapters/active_record.rb
Defined Under Namespace
Modules: DatasourceGenerator, Model, ScopeExtensions
Class Method Summary
collapse
Class Method Details
.association_klass(reflection) ⇒ Object
131
132
133
134
135
136
137
|
# File 'lib/datasource/adapters/active_record.rb', line 131
def association_klass(reflection)
if reflection.macro == :belongs_to && reflection.options[:polymorphic]
fail Datasource::Error, "polymorphic belongs_to not supported, write custom loader"
else
reflection.klass
end
end
|
.association_reflection(klass, name) ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/datasource/adapters/active_record.rb', line 101
def association_reflection(klass, name)
if reflection = klass.reflections[name]
{
klass: reflection.klass,
macro: reflection.macro,
foreign_key: reflection.try(:foreign_key)
}
end
end
|
.ensure_table_join!(ds, name, att) ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/datasource/adapters/active_record.rb', line 223
def ensure_table_join!(ds, name, att)
join_value = ds.scope.joins_values.find do |value|
if value.is_a?(Symbol)
value.to_s == att[:name]
elsif value.is_a?(String)
if value =~ /join (\w+)/i
$1 == att[:name]
end
end
end
fail Datasource::Error, "given scope does not join on #{name}, but it is required by #{att[:name]}" unless join_value
end
|
.get_rows(ds) ⇒ Object
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/datasource/adapters/active_record.rb', line 200
def get_rows(ds)
append_select = []
ds.expose_associations.each_pair do |assoc_name, assoc_select|
if reflection = association_reflection(ds.class.orm_klass, assoc_name.to_sym)
Datasource::Base.reflection_select(reflection, append_select, [])
end
end
ds.select(*append_select)
scope = select_scope(ds)
if scope.respond_to?(:use_datasource)
scope = scope.spawn.use_datasource(nil)
end
scope.includes_values = []
scope.to_a.tap do |records|
load_associations(ds, records)
end
end
|
.get_table_name(klass) ⇒ Object
111
112
113
|
# File 'lib/datasource/adapters/active_record.rb', line 111
def get_table_name(klass)
klass.table_name.to_sym
end
|
.has_attribute?(record, name) ⇒ Boolean
127
128
129
|
# File 'lib/datasource/adapters/active_record.rb', line 127
def has_attribute?(record, name)
record.attributes.key?(name.to_s)
end
|
.is_scope?(obj) ⇒ Boolean
115
116
117
|
# File 'lib/datasource/adapters/active_record.rb', line 115
def is_scope?(obj)
obj.kind_of?(::ActiveRecord::Relation)
end
|
.load_association(records, name) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/datasource/adapters/active_record.rb', line 139
def load_association(records, name)
return if records.empty?
return if records.first.association(name.to_sym).loaded?
klass = records.first.class
if reflection = klass.reflections[name.to_sym]
assoc_class = association_klass(reflection)
datasource_class = assoc_class.default_datasource
serializer_class = Datasource::Base.consumer_adapter.get_serializer_for(assoc_class)
scope = assoc_class.all
datasource = datasource_class.new(scope)
datasource_select = serializer_class._attributes.dup
Datasource::Base.reflection_select(association_reflection(klass, name.to_sym), [], datasource_select)
datasource.select(*datasource_select)
select_values = datasource.get_select_values
begin
::ActiveRecord::Associations::Preloader
.new.preload(records, name, assoc_class.select(*select_values))
rescue ArgumentError
::ActiveRecord::Associations::Preloader
.new(records, name, assoc_class.select(*select_values)).run
end
assoc_records = records.flat_map { |record| record.send(name) }.compact
serializer_class._associations.each_pair do |assoc_name, options|
load_association(assoc_records, assoc_name)
end
datasource.results(assoc_records)
end
rescue Exception => ex
if ex.is_a?(SystemStackError) || ex.is_a?(Datasource::RecursionError)
fail Datasource::RecursionError, "recursive association (involving #{name})"
else
raise
end
end
|
.load_associations(ds, records) ⇒ Object
194
195
196
197
198
|
# File 'lib/datasource/adapters/active_record.rb', line 194
def load_associations(ds, records)
ds.expose_associations.each_pair do |assoc_name, assoc_select|
load_association(records, assoc_name)
end
end
|
.primary_scope_table(ds) ⇒ Object
219
220
221
|
# File 'lib/datasource/adapters/active_record.rb', line 219
def primary_scope_table(ds)
ds.scope.klass.table_name
end
|
.scope_loaded?(scope) ⇒ Boolean
123
124
125
|
# File 'lib/datasource/adapters/active_record.rb', line 123
def scope_loaded?(scope)
scope.loaded?
end
|
.scope_to_class(scope) ⇒ Object
119
120
121
|
# File 'lib/datasource/adapters/active_record.rb', line 119
def scope_to_class(scope)
scope.klass
end
|
.select_scope(ds) ⇒ Object
185
186
187
|
# File 'lib/datasource/adapters/active_record.rb', line 185
def select_scope(ds)
ds.scope.select(*ds.get_select_values)
end
|
.to_query(ds) ⇒ Object
179
180
181
182
183
|
# File 'lib/datasource/adapters/active_record.rb', line 179
def to_query(ds)
::ActiveRecord::Base.uncached do
ds.scope.select(*ds.get_select_values).to_sql
end
end
|
.upgrade_records(ds, records) ⇒ Object
189
190
191
192
|
# File 'lib/datasource/adapters/active_record.rb', line 189
def upgrade_records(ds, records)
load_associations(ds, records)
ds.results(records)
end
|