Module: CompositePrimaryKeys::ActiveRecord::FinderMethods::InstanceMethods

Defined in:
lib/composite_primary_keys/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#construct_limited_ids_condition(relation) ⇒ Object



5
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
36
37
38
39
40
# File 'lib/composite_primary_keys/finder_methods.rb', line 5

def construct_limited_ids_condition(relation)
  orders = relation.order_values.join(", ")

  # CPK
  # values = @klass.connection.distinct("#{@klass.connection.quote_table_name @klass.table_name}.#{@klass.primary_key}", orders)
  keys = @klass.primary_keys.map do |key|
    "#{@klass.connection.quote_table_name @klass.table_name}.#{key}"
  end

  values = @klass.connection.distinct(keys.join(', '), orders)

  ids_array = relation.select(values).collect {|row| row[@klass.primary_key]}

  # CPK
  # ids_array.empty? ? raise(ThrowResult) : primary_key.in(ids_array)

  # OR together each and expression (key=value and key=value) that matches an id set
  # since we only need to match 0 or more records
  or_expressions = ids_array.map do |id_set|
    # AND together "key=value" exprssios to match each id set
    and_expressions = [self.primary_keys, id_set].transpose.map do |key, id|
      table[key].eq(id)
    end

    # Merge all the ands together
    first = and_expressions.shift
    Arel::Nodes::Grouping.new(and_expressions.inject(first) do |memo, expr|
        Arel::Nodes::And.new(memo, expr)
    end)
  end

  first = or_expressions.shift
  Arel::Nodes::Grouping.new(or_expressions.inject(first) do |memo, expr|
      Arel::Nodes::Or.new(memo, expr)
  end)
end

#exists?(id = nil) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/composite_primary_keys/finder_methods.rb', line 42

def exists?(id = nil)
  # ID can be:
  #   Array - ['department_id = ? and location_id = ?', 1, 1]
  #   Array -> [1,2]
  #   CompositeKeys -> [1,2]

  id = id.id if ::ActiveRecord::Base === id

  case id
  # CPK
  when CompositePrimaryKeys::CompositeKeys
    relation = select(primary_keys).limit(1)
    relation = relation.where(ids_predicate(id)) if id
    relation.first ? true : false
  when Array
    # CPK
    if id.first.is_a?(String) and id.first.match(/\?/)
      where(id).exists?
    else
      exists?(id.to_composite_keys)
    end
  when Hash
    where(id).exists?
  else
     # CPK
     #relation = select(primary_key).limit(1)
     #relation = relation.where(primary_key.eq(id)) if id
     relation = select(primary_keys).limit(1)
     relation = relation.where(ids_predicate(id)) if id

    relation.first ? true : false
  end
end

#find_with_ids(*ids, &block) ⇒ Object



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
115
116
117
118
119
120
# File 'lib/composite_primary_keys/finder_methods.rb', line 76

def find_with_ids(*ids, &block)
  return to_a.find { |*block_args| yield(*block_args) } if block_given?

  # Supports:
  #   find('1,2')             ->  ['1,2']
  #   find(1,2)               ->  [1,2]
  #   find([1,2])             -> [['1,2']]
  #   find([1,2], [3,4])      -> [[1,2],[3,4]]
  #
  # Does *not* support:
  #   find('1,2', '3,4')      ->  ['1,2','3,4']

  # Normalize incoming data.  Note the last arg can be nil.  Happens
  # when find is called with nil options like the reload method does.
  ids.compact!
  ids = [ids] unless ids.first.kind_of?(Array)

  results = 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

  ids.length == 1 ? results.first : results
end