Module: Neo4Apis::ModelResolver

Included in:
CLI::ActiveRecord
Defined in:
lib/neo4apis/model_resolver.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(included_class) ⇒ Object



8
9
10
# File 'lib/neo4apis/model_resolver.rb', line 8

def self.included(included_class)
  included_class.send(:include, TableResolver)
end

Instance Method Details

#apply_identified_model_associations!(model_class) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/neo4apis/model_resolver.rb', line 32

def apply_identified_model_associations!(model_class)
  model_class.columns.each do |column|
    match = column.name.match(/^(.+)_id$/i) || column.name.match(/^(.+)id$/i)
    next if not match

    begin
      base = match[1].gsub(/ +/, '_').tableize

      if identify_table_name(tables, base.classify) && model_class.name != base.classify
        debug_log "Defining: belongs_to #{base.singularize.to_sym.inspect}, foreign_key: #{column.name.inspect}, class_name: #{base.classify.inspect}"
        model_class.belongs_to base.singularize.to_sym, foreign_key: column.name, class_name: base.classify
      end
    rescue UnfoundTableError
      nil
    end
  end
end

#apply_identified_primary_key!(model_class) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/neo4apis/model_resolver.rb', line 55

def apply_identified_primary_key!(model_class)
  identity = if model_class.columns.map(&:type) == [:integer, :integer]
    model_class.columns.map(&:name)
  else
    ::ActiveRecord::Base.connection.primary_key(model_class.table_name)
  end
  identity ||= identify_primary_key(model_class.column_names, model_class.name)

  model_class.primary_key = identity if identity
end

#apply_identified_table_name!(model_class) ⇒ Object



50
51
52
53
# File 'lib/neo4apis/model_resolver.rb', line 50

def apply_identified_table_name!(model_class)
  identity = identify_table_name(tables, model_class.name)
  model_class.table_name = identity if identity
end

#get_model(model_or_table_name) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/neo4apis/model_resolver.rb', line 12

def get_model(model_or_table_name)
  get_model_class(model_or_table_name).tap do |model_class|
    if options[:identify_model]
      apply_identified_table_name!(model_class)
      apply_identified_primary_key!(model_class)
      apply_identified_model_associations!(model_class)
    end
  end
end

#get_model_class(model_or_table_name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/neo4apis/model_resolver.rb', line 22

def get_model_class(model_or_table_name)
  return model_or_table_name if model_or_table_name.is_a?(Class) && model_or_table_name.ancestors.include?(::ActiveRecord::Base)

  model_class = model_or_table_name.gsub(/\s+/, '_')
  model_class = model_or_table_name.classify unless model_or_table_name.match(/^[A-Z]/)
  model_class.constantize
rescue NameError
  Object.const_set(model_class, Class.new(::ActiveRecord::Base))
end