Module: Datasource::Adapters::ActiveRecord
- Defined in:
- lib/datasource/adapters/active_record.rb
Defined Under Namespace
Modules: DatasourceGenerator, Model, ScopeExtensions
Class Method Summary collapse
- .association_klass(reflection) ⇒ Object
- .association_loaded?(records, name, assoc_select) ⇒ Boolean
- .association_reflection(klass, name) ⇒ Object
- .ensure_table_join!(ds, name, att) ⇒ Object
- .get_rows(ds) ⇒ Object
- .get_table_name(klass) ⇒ Object
- .has_attribute?(record, name) ⇒ Boolean
- .is_scope?(obj) ⇒ Boolean
- .load_association(records, name, assoc_select, params) ⇒ Object
- .load_associations(ds, records) ⇒ Object
- .primary_scope_table(ds) ⇒ Object
- .scope_loaded?(scope) ⇒ Boolean
- .scope_to_class(scope) ⇒ Object
- .scope_to_records(scope) ⇒ Object
- .select_scope(ds) ⇒ Object
- .to_query(ds) ⇒ Object
- .upgrade_records(ds, records) ⇒ Object
Class Method Details
.association_klass(reflection) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/datasource/adapters/active_record.rb', line 159 def association_klass(reflection) if reflection.macro == :belongs_to && reflection.[:polymorphic] fail Datasource::Error, "polymorphic belongs_to not supported, write custom loader" else reflection.klass end end |
.association_loaded?(records, name, assoc_select) ⇒ Boolean
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/datasource/adapters/active_record.rb', line 167 def association_loaded?(records, name, assoc_select) if records.first.association(name).loaded? all_loaded = records.all? { |record| record.association(name).loaded? } if assoc_select == ["*"] all_loaded elsif all_loaded records.all? do |record| assoc_sample = Array(record.send(name)).first assoc_sample.nil? || assoc_sample._datasource_instance end else false end else false end end |
.association_reflection(klass, name) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/datasource/adapters/active_record.rb', line 125 def association_reflection(klass, name) if reflection = klass.reflect_on_association(name) { klass: reflection.klass, macro: reflection.macro, foreign_key: reflection.try(:foreign_key) } end end |
.ensure_table_join!(ds, name, att) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/datasource/adapters/active_record.rb', line 288 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
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/datasource/adapters/active_record.rb', line 265 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?(:datasource_set) scope = scope.spawn.datasource_set(datasource_class: nil) end scope.includes_values = [] scope.to_a.tap do |records| load_associations(ds, records) end end |
.get_table_name(klass) ⇒ Object
135 136 137 |
# File 'lib/datasource/adapters/active_record.rb', line 135 def get_table_name(klass) klass.table_name.to_sym end |
.has_attribute?(record, name) ⇒ Boolean
155 156 157 |
# File 'lib/datasource/adapters/active_record.rb', line 155 def has_attribute?(record, name) record.attributes.key?(name.to_s) end |
.is_scope?(obj) ⇒ Boolean
139 140 141 |
# File 'lib/datasource/adapters/active_record.rb', line 139 def is_scope?(obj) obj.kind_of?(::ActiveRecord::Relation) end |
.load_association(records, name, assoc_select, params) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/datasource/adapters/active_record.rb', line 185 def load_association(records, name, assoc_select, params) return if records.empty? name = name.to_sym klass = records.first.class if reflection = klass.reflect_on_association(name) assoc_class = association_klass(reflection) datasource_class = assoc_class.default_datasource scope = assoc_class.all datasource = datasource_class.new(scope) assoc_select_attributes = assoc_select.reject { |att| att.kind_of?(Hash) } assoc_select_associations = assoc_select.inject({}) do |hash, att| hash.deep_merge!(att) if att.kind_of?(Hash) hash end Datasource::Base.reflection_select(association_reflection(klass, name), [], assoc_select_attributes) datasource.params(params) Datasource.logger.debug { "load_association #{records.first.try!(:class)} #{name}: #{assoc_select_attributes.inspect}" } datasource.select(*assoc_select_attributes) select_values = datasource.get_select_values # TODO: manually load associations, and load them all at once for # nested associations, eg. in following, load all Users in 1 query: # {"user"=>["*"], "players"=>["*"], "picked_players"=>["*", # {:position=>["*"]}], "parent_picked_team"=>["*", {:user=>["*"]}]} 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 unless assoc_records.empty? if Datasource.logger.info? && !assoc_select_associations.empty? Datasource.logger.info { "Loading associations " + assoc_select_associations.keys.map(&:to_s).join(", ") + " for #{assoc_records.first.try!(:class)}s" } end assoc_select_associations.each_pair do |assoc_name, assoc_select| Datasource.logger.debug { "load_association nested association #{assoc_name}: #{assoc_select.inspect}" } load_association(assoc_records, assoc_name, assoc_select, params) end datasource.results(assoc_records) end 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
255 256 257 258 259 260 261 262 263 |
# File 'lib/datasource/adapters/active_record.rb', line 255 def load_associations(ds, records) if Datasource.logger.info? && !ds.expose_associations.empty? Datasource.logger.info { "Loading associations " + ds.expose_associations.keys.map(&:to_s).join(", ") + " for #{records.first.try!(:class)}s" } end Datasource.logger.debug { "load_associations (#{records.size} #{records.first.try!(:class)}): #{ds.expose_associations.inspect}" } ds.expose_associations.each_pair do |assoc_name, assoc_select| load_association(records, assoc_name, assoc_select, ds.params) end end |
.primary_scope_table(ds) ⇒ Object
284 285 286 |
# File 'lib/datasource/adapters/active_record.rb', line 284 def primary_scope_table(ds) ds.scope.klass.table_name end |
.scope_loaded?(scope) ⇒ Boolean
147 148 149 |
# File 'lib/datasource/adapters/active_record.rb', line 147 def scope_loaded?(scope) scope.loaded? end |
.scope_to_class(scope) ⇒ Object
143 144 145 |
# File 'lib/datasource/adapters/active_record.rb', line 143 def scope_to_class(scope) scope.klass end |
.scope_to_records(scope) ⇒ Object
151 152 153 |
# File 'lib/datasource/adapters/active_record.rb', line 151 def scope_to_records(scope) scope.to_a end |
.select_scope(ds) ⇒ Object
245 246 247 |
# File 'lib/datasource/adapters/active_record.rb', line 245 def select_scope(ds) ds.scope.select(*ds.get_select_values) end |
.to_query(ds) ⇒ Object
239 240 241 242 243 |
# File 'lib/datasource/adapters/active_record.rb', line 239 def to_query(ds) ::ActiveRecord::Base.uncached do ds.scope.select(*ds.get_select_values).to_sql end end |
.upgrade_records(ds, records) ⇒ Object
249 250 251 252 253 |
# File 'lib/datasource/adapters/active_record.rb', line 249 def upgrade_records(ds, records) Datasource.logger.debug { "Upgrading records #{records.map(&:class).map(&:name).join(', ')}" } load_associations(ds, records) ds.results(records) end |