Module: Resourceful
- Defined in:
- lib/resourceful/version.rb,
lib/resourceful/model/xml.rb,
lib/resourceful/agent/base.rb,
lib/resourceful/exceptions.rb,
lib/resourceful/extensions.rb,
lib/resourceful/model/base.rb,
lib/resourceful/model/json.rb,
lib/resourceful/model/findable.rb,
lib/resourceful/resource/cache.rb,
lib/resourceful/agent/mechanize.rb,
lib/resourceful/resource/format.rb,
lib/resourceful/model/eager_load.rb,
lib/resourceful/agent/rest_client.rb,
lib/resourceful/shoulda/test_unit.rb,
lib/resourceful/model/attribute_types.rb,
lib/resourceful/model/embedded_associations.rb,
lib/resourceful/model/external_associations.rb,
lib/resourceful/model/activerecord_associations.rb
Defined Under Namespace
Modules: Agent, Exceptions, Extensions, Model, Resource, Shoulda, Version
Class Method Summary collapse
- .add_to_associations(klass_name, name, data) ⇒ Object
-
.eager_load(collection, associations) ⇒ Object
The idea here is to take a collection of object instances that have resourceful associations and => for each association specified => get the foreign key name => get the foreign key values for each item in the collection => grab the data for all the foreign keys in one call => build object for each result in data => for each item in the collection, set it’s association value from built objects.
- .get_association_data(klass, name) ⇒ Object
Class Method Details
.add_to_associations(klass_name, name, data) ⇒ Object
37 38 39 40 41 |
# File 'lib/resourceful/model/eager_load.rb', line 37 def self.add_to_associations(klass_name, name, data) @@resourceful_associations ||= {} @@resourceful_associations[klass_name] ||= {} @@resourceful_associations[klass_name][name] = data end |
.eager_load(collection, associations) ⇒ Object
The idea here is to take a collection of object instances that have resourceful associations and
> for each association specified
=> get the foreign key name
=> get the foreign key values for each item in the collection
=> grab the data for all the foreign keys in one call
=> build object for each result in data
=> for each item in the collection, set it's association value from built objects
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 |
# File 'lib/resourceful/model/eager_load.rb', line 10 def self.eager_load(collection, associations) if (collection.respond_to?('each') && (collection.respond_to?('empty?') && !collection.empty?)) && \ (associations.respond_to?('each') && (associations.respond_to?('empty?') && !associations.empty?)) klass = collection.first.class associations.each do |association_name| clean_association_name = Resourceful::Model::Base.cleanup_name(association_name.to_s) assoc_data = get_association_data(klass, clean_association_name) assoc_klass = if klass.ancestors.include?(Resourceful::Model::Base) klass.get_namespaced_klass(assoc_data[:class_name]) else if assoc_data[:class_name].kind_of?(::String) assoc_data[:class_name].resourceful_constantize else assoc_data[:class_name] end end fk_values = collection.inject([]) {|vals, item| vals << item.send(assoc_data[:foreign_key_method])} fk_results = assoc_klass.send(assoc_data[:find_method_name], :all, {assoc_data[:foreign_key_name] => fk_values}, true) collection.each do |item| item_results = fk_results.reject{|result| result.send(assoc_data[:foreign_key_name]) != item.send(assoc_data[:foreign_key_method])} item.instance_variable_set("@#{clean_association_name}", [:belongs_to, :has_one].include?(assoc_data[:type]) ? item_results.first : item_results) end end end collection end |
.get_association_data(klass, name) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/resourceful/model/eager_load.rb', line 42 def self.get_association_data(klass, name) @@resourceful_associations ||= {} assoc_data = nil klass.ancestors.each do |anc| break if @@resourceful_associations[anc.to_s] && (assoc_data = @@resourceful_associations[anc.to_s][name]) end assoc_data end |