Module: Bodhi::Associations::ClassMethods
- Defined in:
- lib/bodhi-slam/associations.rb
Instance Method Summary collapse
- #associations ⇒ Object
- #belongs_to(association_name, options = {}) ⇒ Object
- #has_many(association_name, options = {}) ⇒ Object
- #has_one(association_name, options = {}) ⇒ Object
Instance Method Details
#associations ⇒ Object
4 |
# File 'lib/bodhi-slam/associations.rb', line 4 def associations; @associations; end |
#belongs_to(association_name, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/bodhi-slam/associations.rb', line 116 def belongs_to(association_name, ={}) = Bodhi::Support.symbolize_keys() define_association(:belongs_to, association_name, ) # Define a new helper method to get the association define_method(association_name) do # Get the value from the instance object's source_key. Default is :sys_id association = self.class.associations[association_name.to_sym] instance_id = self.send(association[:primary_key]) # Define & call the query. Returns a single Object or nil query = Bodhi::Query.new(association[:class_name]).from(self.bodhi_context) query.where(association[:foreign_key].to_sym => instance_id) query.and(association[:query]) puts query.url query.first end end |
#has_many(association_name, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/bodhi-slam/associations.rb', line 37 def has_many(association_name, ={}) = Bodhi::Support.symbolize_keys() define_association(:has_many, association_name, ) # Define a new helper method to get the association define_method(association_name) do # Get the value from the instance object's source_key. Default is :sys_id association = self.class.associations[association_name.to_sym] query = Bodhi::Query.new(association[:class_name]).from(self.bodhi_context) if association[:through] through_query = Bodhi::Query.new(association[:through][:class_name]).from(self.bodhi_context) through_query.where(association[:through][:foreign_key].to_sym => self.send(association[:primary_key])) through_query.select(association[:through][:primary_key]) count = through_query.count pages = (count.to_f / 100.0).ceil instance_ids = [] method_chain = association[:through][:primary_key].split('.') # loop through each page of the through query if pages > 0 pages.times.collect do |n| paged_query = through_query.clone paged_query.page(n+1) puts paged_query.url records = paged_query.all instance_ids << records.map{ |item| method_chain.reduce(item){ |memo, method| memo.send(method) } } end instance_ids.flatten!.uniq! end # partition the target query if larger than 4K test_query = query.clone query_size = test_query.where(association[:foreign_key].to_sym => { "$in" => instance_ids }).and(association[:query]).url.bytesize if query_size > 4000 records = [] instance_ids.each_slice(100) do |slice| sliced_query = query.clone sliced_query.where(association[:foreign_key].to_sym => { "$in" => slice }) sliced_query.and(association[:query]) puts sliced_query.url records << sliced_query.all end records.flatten! return records else query.where(association[:foreign_key].to_sym => { "$in" => instance_ids }) query.and(association[:query]) puts query.url return query.all end else # default :has_many flow instance_id = self.send(association[:primary_key]) if instance_id.is_a?(Array) query.where(association[:foreign_key].to_sym => { "$in" => instance_id }) else query.where(association[:foreign_key].to_sym => instance_id) end query.and(association[:query]) puts query.url return query.all end end end |
#has_one(association_name, options = {}) ⇒ Object
6 7 8 9 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/bodhi-slam/associations.rb', line 6 def has_one(association_name, ={}) = Bodhi::Support.symbolize_keys() define_association(:has_one, association_name, ) # Define a new helper method to get the association define_method(association_name) do association = self.class.associations[association_name.to_sym] query = Bodhi::Query.new(association[:class_name]).from(self.bodhi_context) if association[:through] through_query = Bodhi::Query.new(association[:through][:class_name]).from(self.bodhi_context) through_query.where(association[:through][:foreign_key].to_sym => self.send(association[:primary_key])) through_query.select(association[:through][:primary_key]) puts through_query.url instance_id = through_query.first.send(association[:through][:primary_key]) query.where(association[:foreign_key].to_sym => instance_id) else instance_id = self.send(association[:primary_key]) query.where(association[:foreign_key].to_sym => instance_id) end query.and(association[:query]) puts query.url query.first end end |