neo4apis-activerecord 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e872c76387db29333b1d6b009ea65045888f2fa2
4
- data.tar.gz: 938e81267ab658e6857050cb20ee204127ae87a4
3
+ metadata.gz: 5888c986c00ef46a8de1156aac64c7544fa75001
4
+ data.tar.gz: 2a1c749c2e53a56e5ea4043ae520b884b2ccfda9
5
5
  SHA512:
6
- metadata.gz: dca90c53f4e3cb387333e67ffbd2c0b2ebbf2a7ab088e04e3508507909d0a104340e991c381dda82c4311117f43d34d403d3cba1d65511da6d6337bd342b9c4b
7
- data.tar.gz: 8c6f51d5a96207ab8e69f8679cd7c5e29a044ba860896f5de19fb564769f3309ce494971bbe6aed7fd815b8e8515e79149346bd7b3e5ddc300f4ff2ae586daa7
6
+ metadata.gz: e4f60e464ba903af191bb11d813d398632813daccdc66561801f2d0bdbb2665713975f8f1a621401469fc274b58c549ad049592aae570fe935529b85cd80d06c
7
+ data.tar.gz: 9c2730fabc4c485fa84225a625a28a8a45496ccd1bf493849017a4b01aef071a705047a29fb7c683412db56d71b0e14fb84535842f01be719b29ddeda4130c48
@@ -18,12 +18,12 @@ module Neo4Apis
18
18
  when :belongs_to, :has_one
19
19
  if options[:"import_#{association_reflection.macro}"]
20
20
  referenced_object = object.send(association_reflection.name)
21
- add_model_relationship association_reflection.name, node, referenced_object
21
+ add_model_relationship association_reflection.name, node, referenced_object if referenced_object
22
22
  end
23
23
  when :has_many
24
24
  if options[:import_has_many]
25
25
  object.send(association_reflection.name).each do |referenced_object|
26
- add_model_relationship association_reflection.name, node, referenced_object
26
+ add_model_relationship association_reflection.name, node, referenced_object if referenced_object
27
27
  end
28
28
  end
29
29
  end
@@ -13,7 +13,7 @@ module Neo4Apis
13
13
  class_option :import_has_one, type: :boolean, default: nil
14
14
  class_option :import_has_many, type: :boolean, default: nil
15
15
 
16
- class_option :guess_associations, type: :boolean, default: false
16
+ class_option :guess_model, type: :boolean, default: false
17
17
 
18
18
  class_option :active_record_config_path, type: :string, default: './config/database.yml'
19
19
  class_option :active_record_environment, type: :string, default: 'development'
@@ -63,33 +63,61 @@ module Neo4Apis
63
63
  options[:"import_#{type}"].nil? ? options[:import_associations] : options[:"import_#{type}"]
64
64
  end
65
65
 
66
+
66
67
  def get_model(model_or_table_name)
68
+ get_model_class(model_or_table_name).tap do |model_class|
69
+ if options[:guess_model]
70
+ apply_guessed_table_name!(model_class)
71
+ apply_guessed_primary_key!(model_class)
72
+ apply_guessed_model_associations!(model_class)
73
+ end
74
+ end
75
+ end
76
+
77
+ def get_model_class(model_or_table_name)
67
78
  model_class = model_or_table_name
68
79
  model_class = model_or_table_name.classify unless model_or_table_name.match(/^[A-Z]/)
69
80
  model_class.constantize
70
81
  rescue NameError
71
- Object.const_set(model_class, Class.new(::ActiveRecord::Base)).tap do |model_class|
72
- apply_guessed_model_associations!(model_class) if options[:guess_associations]
73
- end
82
+ Object.const_set(model_class, Class.new(::ActiveRecord::Base))
74
83
  end
75
84
 
76
85
  def apply_guessed_model_associations!(model_class)
77
86
  model_class.columns.each do |column|
78
- next if not column.name.match(/_id$/)
87
+ match = column.name.match(/^(.*)(_id|Id)$/)
88
+ next if not match
79
89
 
80
90
  begin
81
- base = column.name.humanize.tableize.split(' ').join('_')
91
+ base = match[1].tableize
82
92
 
83
- model_class.belongs_to base.singularize.to_sym
93
+ model_class.belongs_to base.singularize.to_sym, foreign_key: column.name, class_name: base.classify if guessed_table_name(base.classify)
84
94
  rescue NameError
85
95
  end
86
96
  end
87
97
  end
88
98
 
99
+ def apply_guessed_table_name!(model_class)
100
+ guess = guessed_table_name(model_class.name)
101
+ model_class.table_name = guess if guess
102
+ end
103
+
104
+ def apply_guessed_primary_key!(model_class)
105
+ name = model_class.name
106
+ guess = (model_class.column_names & ['id', name.foreign_key, name.foreign_key.classify, 'uuid']).first
107
+ model_class.primary_key = guess if guess
108
+ end
109
+
110
+
89
111
  def active_record_config
90
112
  require 'yaml'
91
113
  YAML.load(File.read(options[:active_record_config_path]))[options[:active_record_environment]]
92
114
  end
115
+
116
+ private
117
+
118
+ def guessed_table_name(model_name)
119
+ (::ActiveRecord::Base.connection.tables & [model_name.tableize, model_name.classify, model_name.tableize.singularize, model_name.classify.pluralize]).first
120
+ end
93
121
  end
94
122
 
95
123
  class Base < Thor
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'neo4apis-activerecord'
6
- s.version = '0.1.0'
6
+ s.version = '0.2.0'
7
7
  s.required_ruby_version = '>= 1.9.1'
8
8
 
9
9
  s.authors = 'Brian Underwood'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neo4apis-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Underwood