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 +4 -4
- data/lib/neo4apis/activerecord.rb +2 -2
- data/lib/neo4apis/cli/activerecord.rb +35 -7
- data/neo4apis-activerecord.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5888c986c00ef46a8de1156aac64c7544fa75001
|
4
|
+
data.tar.gz: 2a1c749c2e53a56e5ea4043ae520b884b2ccfda9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 :
|
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))
|
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
|
-
|
87
|
+
match = column.name.match(/^(.*)(_id|Id)$/)
|
88
|
+
next if not match
|
79
89
|
|
80
90
|
begin
|
81
|
-
base =
|
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
|