neo4apis-activerecord 0.5.3 → 0.6.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: 0706c9d104595af2a52ddbc315361902f558f0c9
4
- data.tar.gz: 07ff2e98764387cb0e69fd2aed3c3c08dcbb3821
3
+ metadata.gz: 580b65a076134da3d58184846112a0f2c3691d62
4
+ data.tar.gz: 9cb375fca65896d8a0bd1a8f042cd7a98041bb8e
5
5
  SHA512:
6
- metadata.gz: e1aa43e045756944de599cad3db6bac7f0b1c209ed12d6df6f33c743a7c068d5fbd9aca687603bf6394dbf7b946941f287c15d2c420ae11879aea6af0743c0d8
7
- data.tar.gz: 87514ff0ad2d9cced4bf0272fba7524b8f0810fdc341c8b1dfa125e4eb14842e11460d0bd79fbda733ff74ba1b3c86987f8c84f4368b923217b8067b0c108db5
6
+ metadata.gz: fc590521e76c4310d35ad43f93d8e81c3dc8aca85312d823564c15595247a91654fff30e27712771975fb6fde8edad557393bd3ee1e2167364e1a0730cb93bc1
7
+ data.tar.gz: c868742cd693db9b2fe514209d995dcdf7bbfa295d1a78a25a82d7f778a6dc6868f7290e02a010b305578786d836ece668a6fddc0e83a4a7d47529c41571f60b
data/Gemfile CHANGED
@@ -14,4 +14,5 @@ group :test do
14
14
 
15
15
  gem 'guard'
16
16
  gem 'guard-rspec'
17
+ gem 'guard-rubocop'
17
18
  end
@@ -8,6 +8,7 @@ module Neo4Apis
8
8
  batch_size 1000
9
9
 
10
10
  def self.model_importer(model_class)
11
+ return if model_class.primary_key.nil?
11
12
  uuid model_class.name.to_sym, model_class.primary_key
12
13
 
13
14
  importer model_class.name.to_sym do |object|
@@ -41,12 +42,25 @@ module Neo4Apis
41
42
  def add_model_node(model_class, object)
42
43
  object_data = OpenStruct.new
43
44
 
44
- object.attributes.each do |column, value|
45
- v = object.attributes_for_coder[column] || value
46
- object_data.send("#{column}=", v)
45
+ object.class.column_names.each do |column_name|
46
+ object_data.send("#{column_name}=", attribute_for_coder(object, column_name))
47
47
  end
48
48
 
49
49
  add_node model_class.name.to_sym, object_data, model_class.column_names
50
50
  end
51
+
52
+ def attribute_for_coder(object, column_name)
53
+ column = object.class.columns_hash[column_name]
54
+ if column.respond_to?(:cast_type)
55
+ column.cast_type.type_cast_from_user(object.attributes[column_name])
56
+ else
57
+ value = object.attributes[column_name]
58
+ if coder = object.class.serialized_attributes[column_name]
59
+ coder.dump(value)
60
+ else
61
+ value
62
+ end
63
+ end
64
+ end
51
65
  end
52
66
  end
@@ -2,10 +2,14 @@ require 'active_record'
2
2
  require 'active_support/inflector'
3
3
  require 'thor'
4
4
  require 'colorize'
5
+ require 'neo4apis/table_resolver'
6
+ require 'neo4apis/cli/base'
5
7
 
6
8
  module Neo4Apis
7
9
  module CLI
8
- class ActiveRecord < Thor
10
+ class ActiveRecord < CLI::Base
11
+ include TableResolver
12
+
9
13
  class_option :import_all_associations, type: :boolean, default: false, desc: 'Shortcut for --import-belongs-to --import-has-many --import-has-one'
10
14
  class_option :import_belongs_to, type: :boolean, default: nil
11
15
  class_option :import_has_one, type: :boolean, default: nil
@@ -61,7 +65,15 @@ module Neo4Apis
61
65
 
62
66
  neo4apis_client.batch do
63
67
  model_classes.each do |model_class|
64
- model_class.find_each do |object|
68
+ query = model_class.all
69
+
70
+ # Eager load association for faster import
71
+ include_list = model_class.reflect_on_all_associations.map do |association_reflection|
72
+ association_reflection.name if import_association?(association_reflection.macro)
73
+ end.compact
74
+ query = query.includes(*include_list.map(&:to_sym)) if include_list.present?
75
+
76
+ query.find_each do |object|
65
77
  neo4apis_client.import model_class.name.to_sym, object
66
78
  end
67
79
  end
@@ -101,9 +113,9 @@ module Neo4Apis
101
113
  end
102
114
 
103
115
  def get_model_class(model_or_table_name)
104
- return model_or_table_name if model_or_table_name.ancestors.include?(::ActiveRecord::Base)
116
+ return model_or_table_name if model_or_table_name.is_a?(Class) && model_or_table_name.ancestors.include?(::ActiveRecord::Base)
105
117
 
106
- model_class = model_or_table_name
118
+ model_class = model_or_table_name.gsub(/\s+/, '_')
107
119
  model_class = model_or_table_name.classify unless model_or_table_name.match(/^[A-Z]/)
108
120
  model_class.constantize
109
121
  rescue NameError
@@ -116,24 +128,24 @@ module Neo4Apis
116
128
  next if not match
117
129
 
118
130
  begin
119
- base = match[1].tableize
131
+ base = match[1].gsub(/ +/, '_').tableize
120
132
 
121
- if identified_table_name(base.classify) && model_class.name != base.classify
133
+ if identify_table_name(tables, base.classify) && model_class.name != base.classify
122
134
  model_class.belongs_to base.singularize.to_sym, foreign_key: column.name, class_name: base.classify
123
135
  end
124
- rescue NameError
136
+ rescue UnfoundTableError
137
+ nil
125
138
  end
126
139
  end
127
140
  end
128
141
 
129
142
  def apply_identified_table_name!(model_class)
130
- identity = identified_table_name(model_class.name)
143
+ identity = identify_table_name(tables, model_class.name)
131
144
  model_class.table_name = identity if identity
132
145
  end
133
146
 
134
147
  def apply_identified_primary_key!(model_class)
135
- name = model_class.name
136
- identity = (model_class.column_names & ['id', name.foreign_key, name.foreign_key.classify, 'uuid']).first
148
+ identity = identify_primary_key(model_class.column_names, model_class.name)
137
149
  model_class.primary_key = identity if identity
138
150
  end
139
151
 
@@ -143,10 +155,8 @@ module Neo4Apis
143
155
  YAML.load(File.read(options[:active_record_config_path]))[options[:active_record_environment]]
144
156
  end
145
157
 
146
- private
147
-
148
- def identified_table_name(model_name)
149
- (::ActiveRecord::Base.connection.tables & [model_name.tableize, model_name.classify, model_name.tableize.singularize, model_name.classify.pluralize]).first
158
+ def tables
159
+ ::ActiveRecord::Base.connection.tables
150
160
  end
151
161
  end
152
162
 
@@ -0,0 +1,39 @@
1
+
2
+ module Neo4Apis
3
+ module TableResolver
4
+ class UnfoundTableError < StandardError
5
+ end
6
+
7
+ class UnfoundPrimaryKeyError < StandardError
8
+ end
9
+
10
+ def identify_table_name(tables, class_name)
11
+ potential_table_comparisons = [class_name.tableize, class_name.tableize.singularize].map(&method(:standardize))
12
+ tables.detect do |table_name|
13
+ potential_table_comparisons.include?(standardize(table_name))
14
+ end.tap do |found_name| # rubocop:disable Style/MultilineBlockChain
15
+ fail UnfoundTableError, "Could not find a table for #{class_name}." if found_name.nil?
16
+ end
17
+ end
18
+
19
+ def identify_primary_key(columns, class_name)
20
+ (columns & %w(id uuid)).first
21
+ columns.detect do |column|
22
+ case standardize(column)
23
+ when 'id', 'uuid'
24
+ true
25
+ when /#{standardize(class_name)}id/
26
+ true
27
+ end
28
+ end.tap do |found_key| # rubocop:disable Style/MultilineBlockChain
29
+ fail UnfoundPrimaryKeyError, "Could not find a primary key for #{class_name}." if found_key.nil?
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def standardize(string)
36
+ string.downcase.gsub(/[ _]+/, '')
37
+ end
38
+ end
39
+ end
@@ -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.5.3'
6
+ s.version = '0.6.0'
7
7
  s.required_ruby_version = '>= 1.9.1'
8
8
 
9
9
  s.authors = 'Brian Underwood'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neo4apis-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Underwood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-08 00:00:00.000000000 Z
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: neo4apis
@@ -50,6 +50,7 @@ files:
50
50
  - lib/neo4apis-activerecord.rb
51
51
  - lib/neo4apis/activerecord.rb
52
52
  - lib/neo4apis/cli/activerecord.rb
53
+ - lib/neo4apis/table_resolver.rb
53
54
  - neo4apis-activerecord.gemspec
54
55
  homepage: https://github.com/neo4jrb/neo4apis-activerecord/
55
56
  licenses: