intercom-rails 0.2.17 → 0.2.18
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/intercom-rails/import.rb +38 -14
- data/lib/intercom-rails/version.rb +1 -1
- data/test/import_test_setup.rb +22 -0
- data/test/intercom-rails/import_unit_test.rb +16 -3
- metadata +12 -7
@@ -33,11 +33,24 @@ module IntercomRails
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
# Check for ActiveRecord OR Mongoid
|
37
|
+
def active_record?(user_klass)
|
38
|
+
(defined?(ActiveRecord::Base) && (user_klass < ActiveRecord::Base))
|
39
|
+
end
|
40
|
+
|
41
|
+
def mongoid?(user_klass)
|
42
|
+
(defined?(Mongoid::Document) && (user_klass < Mongoid::Document))
|
43
|
+
end
|
44
|
+
|
45
|
+
def supported_orm?(user_klass)
|
46
|
+
active_record?(user_klass) || mongoid?(user_klass)
|
47
|
+
end
|
48
|
+
|
36
49
|
def assert_runnable
|
37
50
|
raise ImportError, "You can only import your users from your production environment" unless Rails.env.production?
|
38
51
|
raise ImportError, "We couldn't find your user class, please set one in config/initializers/intercom_rails.rb" unless user_klass.present?
|
39
52
|
info "Found user class: #{user_klass}"
|
40
|
-
raise ImportError, "Only ActiveRecord models are supported" unless
|
53
|
+
raise ImportError, "Only ActiveRecord and Mongoid models are supported" unless supported_orm?(user_klass)
|
41
54
|
raise ImportError, "Please add an Intercom API Key to config/initializers/intercom.rb" unless IntercomRails.config.api_key.present?
|
42
55
|
info "Intercom API key found"
|
43
56
|
end
|
@@ -67,22 +80,33 @@ module IntercomRails
|
|
67
80
|
private
|
68
81
|
MAX_BATCH_SIZE = 100
|
69
82
|
def batches
|
70
|
-
user_klass
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
yield(prepare_batch(users_for_wire), users_for_wire.count) unless users_for_wire.count.zero?
|
83
|
+
if active_record?(user_klass)
|
84
|
+
user_klass.find_in_batches(:batch_size => MAX_BATCH_SIZE) do |users|
|
85
|
+
users_for_wire = map_to_users_for_wire(users)
|
86
|
+
|
87
|
+
yield(prepare_batch(users_for_wire), users_for_wire.count) unless users_for_wire.count.zero?
|
88
|
+
end
|
89
|
+
elsif mongoid?(user_klass)
|
90
|
+
0.step(user_klass.all.count, MAX_BATCH_SIZE) do |offset|
|
91
|
+
users_for_wire = map_to_users_for_wire(user_klass.limit(MAX_BATCH_SIZE).skip(offset))
|
92
|
+
yield(prepare_batch(users_for_wire), users_for_wire.count) unless users_for_wire.count.zero?
|
93
|
+
end
|
83
94
|
end
|
84
95
|
end
|
85
96
|
|
97
|
+
def map_to_users_for_wire(users)
|
98
|
+
users.map do |u|
|
99
|
+
user_proxy = Proxy::User.new(u)
|
100
|
+
next unless user_proxy.valid?
|
101
|
+
|
102
|
+
for_wire = user_proxy.to_hash
|
103
|
+
companies = Proxy::Company.companies_for_user(user_proxy)
|
104
|
+
for_wire.merge!(:companies => companies.map(&:to_hash)) if companies.present?
|
105
|
+
|
106
|
+
for_wire
|
107
|
+
end.compact
|
108
|
+
end
|
109
|
+
|
86
110
|
def prepare_batch(batch)
|
87
111
|
{:users => batch}.to_json
|
88
112
|
end
|
data/test/import_test_setup.rb
CHANGED
@@ -11,6 +11,28 @@ module ActiveRecord
|
|
11
11
|
class Base; end
|
12
12
|
end
|
13
13
|
|
14
|
+
module Mongoid
|
15
|
+
module Document
|
16
|
+
def self.included(klass)
|
17
|
+
klass.extend ClassMethods
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def all
|
22
|
+
@_users ||= User.all
|
23
|
+
end
|
24
|
+
|
25
|
+
def limit(*args)
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def skip(*args)
|
30
|
+
all
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
14
36
|
class User
|
15
37
|
|
16
38
|
attr_reader :id, :email, :name
|
@@ -25,14 +25,14 @@ class ImportUnitTest < MiniTest::Unit::TestCase
|
|
25
25
|
assert_equal exception.message, "We couldn't find your user class, please set one in config/initializers/intercom_rails.rb"
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
28
|
+
def test_run_with_unsupported_user_class
|
29
29
|
IntercomRails::Import.any_instance.stub(:user_klass).and_return(Class)
|
30
30
|
|
31
31
|
exception = assert_raises IntercomRails::ImportError do
|
32
32
|
IntercomRails::Import.run
|
33
33
|
end
|
34
34
|
|
35
|
-
assert_equal exception.message, "Only ActiveRecord models are supported"
|
35
|
+
assert_equal exception.message, "Only ActiveRecord and Mongoid models are supported"
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_run_with_no_api_key
|
@@ -45,6 +45,19 @@ class ImportUnitTest < MiniTest::Unit::TestCase
|
|
45
45
|
assert_equal exception.message, "Please add an Intercom API Key to config/initializers/intercom.rb"
|
46
46
|
end
|
47
47
|
|
48
|
+
def test_mongoid
|
49
|
+
klass = Class.new
|
50
|
+
klass.class_eval do
|
51
|
+
include Mongoid::Document
|
52
|
+
end
|
53
|
+
IntercomRails::Import.any_instance.stub(:user_klass).and_return(klass)
|
54
|
+
|
55
|
+
@import = IntercomRails::Import.new
|
56
|
+
@import.should_receive(:map_to_users_for_wire).with(klass.all).and_call_original
|
57
|
+
@import.should_receive(:send_users).and_return('failed' => [])
|
58
|
+
@import.run
|
59
|
+
end
|
60
|
+
|
48
61
|
def test_status_output
|
49
62
|
@import = IntercomRails::Import.new(:status_enabled => true)
|
50
63
|
@import.stub(:send_users).and_return('failed' => [1])
|
@@ -62,7 +75,7 @@ class ImportUnitTest < MiniTest::Unit::TestCase
|
|
62
75
|
..F
|
63
76
|
* Successfully created 2 users
|
64
77
|
* Failed to create 1 user, this is likely due to bad data
|
65
|
-
output
|
78
|
+
output
|
66
79
|
$stdout.flush
|
67
80
|
|
68
81
|
assert_equal expected_output, @output.string
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: intercom-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.18
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-04-
|
14
|
+
date: 2013-04-22 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
@@ -66,17 +66,17 @@ dependencies:
|
|
66
66
|
requirement: !ruby/object:Gem::Requirement
|
67
67
|
none: false
|
68
68
|
requirements:
|
69
|
-
- -
|
69
|
+
- - ~>
|
70
70
|
- !ruby/object:Gem::Version
|
71
|
-
version: '
|
71
|
+
version: '2.12'
|
72
72
|
type: :development
|
73
73
|
prerelease: false
|
74
74
|
version_requirements: !ruby/object:Gem::Requirement
|
75
75
|
none: false
|
76
76
|
requirements:
|
77
|
-
- -
|
77
|
+
- - ~>
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
version: '
|
79
|
+
version: '2.12'
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: pry
|
82
82
|
requirement: !ruby/object:Gem::Requirement
|
@@ -194,12 +194,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
194
194
|
- - ! '>='
|
195
195
|
- !ruby/object:Gem::Version
|
196
196
|
version: '0'
|
197
|
+
segments:
|
198
|
+
- 0
|
199
|
+
hash: -3918465583784430054
|
197
200
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
198
201
|
none: false
|
199
202
|
requirements:
|
200
203
|
- - ! '>='
|
201
204
|
- !ruby/object:Gem::Version
|
202
205
|
version: '0'
|
206
|
+
segments:
|
207
|
+
- 0
|
208
|
+
hash: -3918465583784430054
|
203
209
|
requirements: []
|
204
210
|
rubyforge_project: intercom-rails
|
205
211
|
rubygems_version: 1.8.23
|
@@ -218,4 +224,3 @@ test_files:
|
|
218
224
|
- test/intercom-rails/script_tag_helper_test.rb
|
219
225
|
- test/intercom-rails/script_tag_test.rb
|
220
226
|
- test/test_setup.rb
|
221
|
-
has_rdoc:
|