Class: Immigrant::KeyFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/immigrant.rb,
lib/immigrant/compat/3.0.rb,
lib/immigrant/compat/3.1.rb,
lib/immigrant/compat/4.0.rb,
lib/immigrant/compat/4.2.rb

Instance Method Summary collapse

Instance Method Details

#infer_keys(db_keys = current_foreign_keys, classes = model_classes) ⇒ Object



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
41
42
43
44
45
# File 'lib/immigrant.rb', line 15

def infer_keys(db_keys = current_foreign_keys, classes = model_classes)
  database_keys = Hash[db_keys.map { |foreign_key|
    [foreign_key.hash_key, foreign_key]
  }]

  ignore_keys = Hash[Immigrant.ignore_keys.map { |key|
    [[key[:from_table], key[:column]], true]
  }]

  model_keys, warnings = model_keys(classes)

  new_keys = []
  model_keys.keys.each do |hash_key|
    foreign_key = model_keys[hash_key]
    # if the foreign key exists in the db, we call it good (even if
    # the name is different or :on_delete doesn't match, etc.), though
    # we do warn on clearly broken stuff
    if current_key = database_keys[hash_key]
      if current_key.to_table != foreign_key.to_table || current_key.options[:primary_key] != foreign_key.options[:primary_key]
        warnings[hash_key] = "Skipping #{foreign_key.from_table}.#{foreign_key.options[:column]}: its association references a different key/table than its current foreign key"
      end
      next
    end

    next if ignore_keys[hash_key]
    next unless key_validator.valid?(foreign_key)

    new_keys << foreign_key
  end
  [new_keys.sort_by{ |key| key.options[:name] }, warnings]
end

#qualified_reflection?(reflection, klass) ⇒ Boolean

Returns:

  • (Boolean)


210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/immigrant.rb', line 210

def qualified_reflection?(reflection, klass)
  scope = reflection.scope
  if scope.nil?
    false
  else
    klass.instance_exec(*([nil]*scope.arity), &scope).where_clause.any?
  end
rescue
  # if there's an error evaluating the scope block or whatever, just
  # err on the side of caution and assume there are conditions
  true
end