Module: CompositePrimaryKeys::ActiveRecord::FinderMethods
- Defined in:
- lib/composite_primary_keys/relation/finder_methods.rb
Instance Method Summary collapse
- #apply_join_dependency(relation, join_dependency) ⇒ Object
- #exists?(conditions = :none) ⇒ Boolean
- #find_one(id) ⇒ Object
- #find_some(ids) ⇒ Object
- #find_with_ids(*ids) ⇒ Object
- #limited_ids_for(relation) ⇒ Object
Instance Method Details
#apply_join_dependency(relation, join_dependency) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/composite_primary_keys/relation/finder_methods.rb', line 4 def apply_join_dependency(relation, join_dependency) relation = relation.except(:includes, :eager_load, :preload) relation = relation.joins join_dependency if using_limitable_reflections?(join_dependency.reflections) relation else if relation.limit_value limited_ids = limited_ids_for(relation) # CPK #limited_ids.empty? ? relation.none! : relation.where!(table[primary_key].in(limited_ids)) limited_ids.empty? ? relation.none! : relation.where!(cpk_in_predicate(table, self.primary_keys, limited_ids)) end relation.except(:limit, :offset) end end |
#exists?(conditions = :none) ⇒ Boolean
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 |
# File 'lib/composite_primary_keys/relation/finder_methods.rb', line 39 def exists?(conditions = :none) # conditions can be: # Array - ['department_id = ? and location_id = ?', 1, 1] # Array -> [1,2] # CompositeKeys -> [1,2] conditions = conditions.id if ::ActiveRecord::Base === conditions return false if !conditions relation = apply_join_dependency(self, construct_join_dependency) return false if ::ActiveRecord::NullRelation === relation relation = relation.except(:select, :order).select(::ActiveRecord::FinderMethods::ONE_AS_ONE).limit(1) # CPK #case conditions #when Array, Hash # relation = relation.where(conditions) #else # relation = relation.where(table[primary_key].eq(conditions)) if conditions != :none #end case conditions when CompositePrimaryKeys::CompositeKeys relation = relation.where(cpk_id_predicate(table, primary_key, conditions)) when Array pk_length = @klass.primary_keys.length if conditions.length == pk_length # E.g. conditions = ['France', 'Paris'] return self.exists?(conditions.to_composite_keys) else # Assume that conditions contains where relation relation = relation.where(conditions) end when Hash relation = relation.where(conditions) end connection.select_value(relation, "#{name} Exists", relation.bind_values) ? true : false end |
#find_one(id) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/composite_primary_keys/relation/finder_methods.rb', line 102 def find_one(id) # CPK #id = id.id if ActiveRecord::Base === id id = id.id if ::ActiveRecord::Base === id # CPK #column = columns_hash[primary_key] #substitute = connection.substitute_at(column, bind_values.length) #relation = where(table[primary_key].eq(substitute)) #relation.bind_values += [[column, id]] #record = relation.take relation = self values = primary_keys.each_with_index.map do |primary_key, i| column = columns_hash[primary_key] relation.bind_values += [[column, id[i]]] connection.substitute_at(column, bind_values.length - 1) end relation = relation.where(cpk_id_predicate(table, primary_keys, values)) record = relation.take raise_record_not_found_exception!(id, 0, 1) unless record record end |
#find_some(ids) ⇒ Object
126 127 128 129 130 131 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 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/composite_primary_keys/relation/finder_methods.rb', line 126 def find_some(ids) # CPK # result = where(table[primary_key].in(ids)).to_a result = ids.map do |cpk_ids| cpk_ids = if cpk_ids.length == 1 cpk_ids.first.split(CompositePrimaryKeys::ID_SEP).to_composite_keys else cpk_ids.to_composite_keys end unless cpk_ids.length == @klass.primary_keys.length raise "#{cpk_ids.inspect}: Incorrect number of primary keys for #{@klass.name}: #{@klass.primary_keys.inspect}" end new_relation = clone [@klass.primary_keys, cpk_ids].transpose.map do |key, id| new_relation = new_relation.where(key => id) end records = new_relation.to_a if records.empty? conditions = new_relation.arel.where_sql raise(::ActiveRecord::RecordNotFound, "Couldn't find #{@klass.name} with ID=#{cpk_ids} #{conditions}") end records end.flatten expected_size = if limit_value && ids.size > limit_value limit_value else ids.size end # 11 ids with limit 3, offset 9 should give 2 results. if offset_value && (ids.size - offset_value < expected_size) expected_size = ids.size - offset_value end if result.size == expected_size result else raise_record_not_found_exception!(ids, result.size, expected_size) end end |
#find_with_ids(*ids) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/composite_primary_keys/relation/finder_methods.rb', line 79 def find_with_ids(*ids) # CPK handle strings that come w/ calling to_param on CPK-enabled models ids = parse_ids(ids) raise UnknownPrimaryKey.new(@klass) if primary_key.nil? expects_array = ids.first.kind_of?(Array) return ids.first if expects_array && ids.first.empty? # CPK - don't do this, we want an array of arrays #ids = ids.flatten.compact.uniq case ids.size when 0 raise RecordNotFound, "Couldn't find #{@klass.name} without an ID" when 1 result = find_one(ids.first) # CPK # expects_array ? [ result ] : result result else find_some(ids) end end |
#limited_ids_for(relation) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/composite_primary_keys/relation/finder_methods.rb', line 21 def limited_ids_for(relation) # CPK #values = @klass.connection.columns_for_distinct( # "#{quoted_table_name}.#{quoted_primary_key}", relation.order_values) columns = @klass.primary_keys.map do |key| "#{quoted_table_name}.#{connection.quote_column_name(key)}" end values = @klass.connection.columns_for_distinct(columns, relation.order_values) relation = relation.except(:select).select(values).distinct! id_rows = @klass.connection.select_all(relation.arel, 'SQL', relation.bind_values) # CPK #id_rows.map {|row| row[primary_key]} id_rows.map {|row| row.values} end |