intercom-rails 0.0.9 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/intercom-rails.rb +2 -1
- data/lib/intercom-rails/config.rb +42 -5
- data/lib/intercom-rails/custom_data_helper.rb +18 -0
- data/lib/intercom-rails/exceptions.rb +1 -1
- data/lib/intercom-rails/import.rb +5 -11
- data/lib/intercom-rails/railtie.rb +3 -5
- data/lib/intercom-rails/script_tag.rb +12 -7
- data/lib/intercom-rails/user_proxy.rb +83 -0
- data/lib/intercom-rails/version.rb +1 -1
- data/lib/rails/generators/intercom/config/intercom.rb.erb +21 -4
- data/test/action_controller_test_setup.rb +1 -0
- data/test/intercom-rails/auto_include_filter_test.rb +19 -0
- data/test/intercom-rails/config_test.rb +35 -0
- data/test/intercom-rails/import_network_test.rb +1 -0
- data/test/intercom-rails/import_unit_test.rb +1 -23
- data/test/intercom-rails/script_tag_test.rb +29 -0
- data/test/intercom-rails/user_proxy_test.rb +89 -0
- data/test/test_setup.rb +9 -0
- metadata +8 -7
- data/lib/intercom-rails/current_user.rb +0 -53
- data/test/intercom-rails/current_user_test.rb +0 -54
data/lib/intercom-rails.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'intercom-rails/exceptions'
|
2
|
-
require 'intercom-rails/
|
2
|
+
require 'intercom-rails/user_proxy'
|
3
3
|
require 'intercom-rails/script_tag'
|
4
4
|
require 'intercom-rails/script_tag_helper'
|
5
|
+
require 'intercom-rails/custom_data_helper'
|
5
6
|
require 'intercom-rails/auto_include_filter'
|
6
7
|
require 'intercom-rails/config'
|
7
8
|
require 'intercom-rails/import'
|
@@ -2,6 +2,14 @@ module IntercomRails
|
|
2
2
|
|
3
3
|
module Config
|
4
4
|
|
5
|
+
def self.reset!
|
6
|
+
[self, InboxConfig].each do |configer|
|
7
|
+
configer.instance_variables.each do |var|
|
8
|
+
configer.send(:remove_instance_variable, var)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
5
13
|
# Your Intercom app_id
|
6
14
|
def self.app_id=(value)
|
7
15
|
@app_id = value
|
@@ -50,13 +58,42 @@ module IntercomRails
|
|
50
58
|
end
|
51
59
|
|
52
60
|
# Widget options
|
53
|
-
def self.inbox
|
54
|
-
|
55
|
-
@inbox = value
|
61
|
+
def self.inbox
|
62
|
+
InboxConfig
|
56
63
|
end
|
57
64
|
|
58
|
-
def self.
|
59
|
-
|
65
|
+
def self.custom_data=(value)
|
66
|
+
raise ArgumentError, "custom_data should be a hash" unless value.kind_of?(Hash)
|
67
|
+
unless value.reject { |_,v| v.kind_of?(Proc) || v.kind_of?(Symbol) }.count.zero?
|
68
|
+
raise ArgumentError, "all custom_data attributes should be either a Proc or a symbol"
|
69
|
+
end
|
70
|
+
|
71
|
+
@custom_data = value
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.custom_data
|
75
|
+
@custom_data
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
module InboxConfig
|
81
|
+
|
82
|
+
def self.style=(value)
|
83
|
+
raise ArgumentError, "inbox.style must be one of :default or :custom" unless [:default, :custom].include?(value)
|
84
|
+
@style = value
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.style
|
88
|
+
@style
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.counter=(value)
|
92
|
+
@counter = value
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.counter
|
96
|
+
@counter
|
60
97
|
end
|
61
98
|
|
62
99
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module IntercomRails
|
2
|
+
|
3
|
+
module CustomDataHelper
|
4
|
+
|
5
|
+
# This helper allows custom data attributes to be added to a user
|
6
|
+
# for the current request from within the controller. e.g.
|
7
|
+
#
|
8
|
+
# def destroy
|
9
|
+
# intercom_custom_data['canceled_at'] = Time.now
|
10
|
+
# ...
|
11
|
+
# end
|
12
|
+
def intercom_custom_data
|
13
|
+
@_request_specific_intercom_custom_data ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -68,7 +68,11 @@ module IntercomRails
|
|
68
68
|
MAX_BATCH_SIZE = 100
|
69
69
|
def batches
|
70
70
|
user_klass.find_in_batches(:batch_size => MAX_BATCH_SIZE) do |users|
|
71
|
-
users_for_wire = users.map
|
71
|
+
users_for_wire = users.map do |u|
|
72
|
+
user_proxy = UserProxy.new(u)
|
73
|
+
user_proxy.valid? ? user_proxy.to_hash : nil
|
74
|
+
end.compact
|
75
|
+
|
72
76
|
yield(prepare_batch(users_for_wire), users_for_wire.count) unless users_for_wire.count.zero?
|
73
77
|
end
|
74
78
|
end
|
@@ -77,16 +81,6 @@ module IntercomRails
|
|
77
81
|
{:users => batch}.to_json
|
78
82
|
end
|
79
83
|
|
80
|
-
def user_for_wire(user)
|
81
|
-
wired = {}.tap do |h|
|
82
|
-
h[:user_id] = user.id if user.respond_to?(:id) && user.id.present?
|
83
|
-
h[:email] = user.email if user.respond_to?(:email) && user.email.present?
|
84
|
-
h[:name] = user.name if user.respond_to?(:name) && user.name.present?
|
85
|
-
end
|
86
|
-
|
87
|
-
(wired[:user_id] || wired[:email]) ? wired : nil
|
88
|
-
end
|
89
|
-
|
90
84
|
def user_klass
|
91
85
|
if IntercomRails.config.user_model.present?
|
92
86
|
IntercomRails.config.user_model.call
|
@@ -1,11 +1,9 @@
|
|
1
1
|
module IntercomRails
|
2
2
|
class Railtie < Rails::Railtie
|
3
|
-
initializer "
|
3
|
+
initializer "intercom-rails" do |app|
|
4
4
|
ActionView::Base.send :include, ScriptTagHelper
|
5
|
-
|
6
|
-
|
7
|
-
initializer "intercom_on_rails.auto_include_filter.rb" do |app|
|
8
|
-
ActionController::Base.send :after_filter, AutoIncludeFilter
|
5
|
+
ActionController::Base.send :include, CustomDataHelper
|
6
|
+
ActionController::Base.send :after_filter, AutoIncludeFilter
|
9
7
|
end
|
10
8
|
|
11
9
|
rake_tasks do
|
@@ -14,7 +14,7 @@ module IntercomRails
|
|
14
14
|
|
15
15
|
def initialize(options = {})
|
16
16
|
self.secret = options[:secret] || Config.api_secret
|
17
|
-
self.widget_options = options[:widget] ||
|
17
|
+
self.widget_options = widget_options_from_config.merge(options[:widget] || {})
|
18
18
|
self.controller = options[:controller]
|
19
19
|
self.user_details = options[:find_current_user_details] ? find_current_user_details : options[:user_details]
|
20
20
|
end
|
@@ -62,15 +62,15 @@ module IntercomRails
|
|
62
62
|
@user_details = @user_details.with_indifferent_access.tap do |u|
|
63
63
|
[:email, :name, :user_id].each { |k| u.delete(k) if u[k].nil? }
|
64
64
|
|
65
|
-
u[:user_hash] ||= user_hash if secret.present?
|
65
|
+
u[:user_hash] ||= user_hash if secret.present? && (u[:user_id] || u[:email]).present?
|
66
66
|
u[:app_id] ||= app_id
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
70
|
def find_current_user_details
|
71
71
|
return {} unless controller.present?
|
72
|
-
|
73
|
-
rescue
|
72
|
+
UserProxy.from_current_user_in_object(controller).to_hash
|
73
|
+
rescue NoUserFoundError
|
74
74
|
{}
|
75
75
|
end
|
76
76
|
|
@@ -88,16 +88,21 @@ module IntercomRails
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def widget_options_from_config
|
91
|
-
|
91
|
+
config = {}
|
92
92
|
|
93
|
-
activator = case Config.inbox
|
93
|
+
activator = case Config.inbox.style
|
94
94
|
when :default
|
95
95
|
'#IntercomDefaultWidget'
|
96
96
|
when :custom
|
97
97
|
'#Intercom'
|
98
|
+
else
|
99
|
+
nil
|
98
100
|
end
|
99
101
|
|
100
|
-
|
102
|
+
config[:activator] = activator if activator
|
103
|
+
config[:use_counter] = Config.inbox.counter if Config.inbox.counter
|
104
|
+
|
105
|
+
config
|
101
106
|
end
|
102
107
|
|
103
108
|
def convert_dates_to_unix_timestamps(object)
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module IntercomRails
|
2
|
+
|
3
|
+
class UserProxy
|
4
|
+
|
5
|
+
POTENTIAL_USER_OBJECTS = [
|
6
|
+
Proc.new { instance_eval &IntercomRails.config.current_user if IntercomRails.config.current_user.present? },
|
7
|
+
Proc.new { current_user },
|
8
|
+
Proc.new { @user }
|
9
|
+
]
|
10
|
+
|
11
|
+
def self.from_current_user_in_object(search_object)
|
12
|
+
POTENTIAL_USER_OBJECTS.each do |potential_user|
|
13
|
+
begin
|
14
|
+
user_proxy = new(search_object.instance_eval(&potential_user), search_object)
|
15
|
+
return user_proxy if user_proxy.valid?
|
16
|
+
rescue NameError
|
17
|
+
next
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
raise NoUserFoundError
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_reader :search_object, :user
|
25
|
+
|
26
|
+
def initialize(user, search_object = nil)
|
27
|
+
@user = user
|
28
|
+
@search_object = search_object
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_hash
|
32
|
+
hsh = {}
|
33
|
+
|
34
|
+
hsh[:user_id] = user.id if attribute_present?(:id)
|
35
|
+
[:email, :name, :created_at].each do |attribute|
|
36
|
+
hsh[attribute] = user.send(attribute) if attribute_present?(attribute)
|
37
|
+
end
|
38
|
+
|
39
|
+
hsh[:custom_data] = custom_data
|
40
|
+
hsh.delete(:custom_data) unless hsh[:custom_data].present?
|
41
|
+
|
42
|
+
hsh
|
43
|
+
end
|
44
|
+
|
45
|
+
def custom_data
|
46
|
+
custom_data_from_config.merge custom_data_from_request
|
47
|
+
end
|
48
|
+
|
49
|
+
def valid?
|
50
|
+
return true if user.respond_to?(:id) && user.id.present?
|
51
|
+
return true if user.respond_to?(:email) && user.email.present?
|
52
|
+
false
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
def attribute_present?(attribute)
|
57
|
+
user.respond_to?(attribute) && user.send(attribute).present?
|
58
|
+
end
|
59
|
+
|
60
|
+
def custom_data_value_from_proc_or_symbol(proc_or_symbol)
|
61
|
+
if proc_or_symbol.kind_of?(Symbol)
|
62
|
+
user.send(proc_or_symbol)
|
63
|
+
elsif proc_or_symbol.kind_of?(Proc)
|
64
|
+
proc_or_symbol.call(user)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def custom_data_from_request
|
69
|
+
search_object.intercom_custom_data
|
70
|
+
rescue NoMethodError
|
71
|
+
{}
|
72
|
+
end
|
73
|
+
|
74
|
+
def custom_data_from_config
|
75
|
+
return {} if Config.custom_data.blank?
|
76
|
+
Config.custom_data.reduce({}) do |custom_data, (k,v)|
|
77
|
+
custom_data.merge(k => custom_data_value_from_proc_or_symbol(v))
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
@@ -33,15 +33,32 @@ IntercomRails.config do |config|
|
|
33
33
|
# The class which defines your user model
|
34
34
|
#
|
35
35
|
# config.user_model = Proc.new { User }
|
36
|
+
|
37
|
+
# == Custom Data
|
38
|
+
# A hash of additional data you wish to send about your users.
|
39
|
+
# You can provide either a method name which will be sent to the current
|
40
|
+
# user object, or a Proc which will be passed the current user.
|
41
|
+
#
|
42
|
+
# config.custom_data = {
|
43
|
+
# 'plan' => Proc.new { |current_user| current_user.plan.name },
|
44
|
+
# 'favorite_color' => :favorite_color
|
45
|
+
# }
|
36
46
|
|
37
|
-
# == Inbox
|
47
|
+
# == Inbox Style
|
38
48
|
# This enables the Intercom inbox which allows your users to read their
|
39
|
-
# past conversations with your app, as well as start new ones.
|
49
|
+
# past conversations with your app, as well as start new ones. It is
|
50
|
+
# disabled by default.
|
40
51
|
# * :default shows a small tab with a question mark icon on it
|
41
52
|
# * :custom attaches the inbox open event to an anchor with an
|
42
53
|
# id of #Intercom.
|
43
54
|
#
|
44
|
-
# config.inbox = :default
|
45
|
-
# config.inbox = :custom
|
55
|
+
# config.inbox.style = :default
|
56
|
+
# config.inbox.style = :custom
|
57
|
+
|
58
|
+
# == Inbox Counter
|
59
|
+
# If you're using the custom inbox style, you can request that Intercom
|
60
|
+
# insert an `em` element into your anchor with the count of unread messages
|
61
|
+
#
|
62
|
+
# config.inbox.counter = true
|
46
63
|
|
47
64
|
end
|
@@ -10,6 +10,12 @@ class TestController < ActionController::Base
|
|
10
10
|
@user = dummy_user
|
11
11
|
render :text => params[:body], :content_type => 'text/html'
|
12
12
|
end
|
13
|
+
|
14
|
+
def with_user_instance_variable_and_custom_data
|
15
|
+
@user = dummy_user
|
16
|
+
intercom_custom_data['testing_stuff'] = true
|
17
|
+
render :text => params[:body], :content_type => 'text/html'
|
18
|
+
end
|
13
19
|
|
14
20
|
def with_unusable_user_instance_variable
|
15
21
|
@user = Object.new
|
@@ -34,12 +40,18 @@ end
|
|
34
40
|
|
35
41
|
class AutoIncludeFilterTest < ActionController::TestCase
|
36
42
|
|
43
|
+
include InterTest
|
44
|
+
|
37
45
|
tests TestController
|
38
46
|
|
39
47
|
def setup
|
40
48
|
super
|
41
49
|
ENV['INTERCOM_APP_ID'] = 'my_app_id'
|
42
50
|
end
|
51
|
+
|
52
|
+
def teardown
|
53
|
+
ENV.delete('INTERCOM_APP_ID')
|
54
|
+
end
|
43
55
|
|
44
56
|
def test_no_user_present
|
45
57
|
get :without_user, :body => "<body>Hello world</body>"
|
@@ -65,6 +77,13 @@ class AutoIncludeFilterTest < ActionController::TestCase
|
|
65
77
|
assert_includes @response.body, "Ben McRedmond"
|
66
78
|
end
|
67
79
|
|
80
|
+
def test_user_instance_variable_present_with_body_tag_and_custom_data
|
81
|
+
get :with_user_instance_variable_and_custom_data, :body => "<body>Hello world</body>"
|
82
|
+
assert_includes @response.body, "<script>"
|
83
|
+
assert_includes @response.body, "custom_data"
|
84
|
+
assert_includes @response.body, "testing_stuff"
|
85
|
+
end
|
86
|
+
|
68
87
|
def test_current_user_method_present_with_body_tag
|
69
88
|
get :with_current_user_method, :body => "<body>Hello world</body>"
|
70
89
|
|
@@ -2,6 +2,8 @@ require 'test_setup'
|
|
2
2
|
|
3
3
|
class ConfigTest < MiniTest::Unit::TestCase
|
4
4
|
|
5
|
+
include InterTest
|
6
|
+
|
5
7
|
def test_setting_app_id
|
6
8
|
IntercomRails.config.app_id = "1234"
|
7
9
|
assert_equal IntercomRails.config.app_id, "1234"
|
@@ -27,4 +29,37 @@ class ConfigTest < MiniTest::Unit::TestCase
|
|
27
29
|
assert_equal IntercomRails.config.app_id, "4567"
|
28
30
|
end
|
29
31
|
|
32
|
+
def test_custom_data_rejects_non_proc_or_symbol_attributes
|
33
|
+
exception = assert_raises ArgumentError do
|
34
|
+
IntercomRails.config.custom_data = {
|
35
|
+
'foo' => Proc.new {},
|
36
|
+
'bar' => 'heyheyhey!'
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
assert_equal "all custom_data attributes should be either a Proc or a symbol", exception.message
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_setting_custom_data
|
44
|
+
custom_data_config = {
|
45
|
+
'foo' => Proc.new {},
|
46
|
+
'bar' => :method_name
|
47
|
+
}
|
48
|
+
|
49
|
+
IntercomRails.config.custom_data = custom_data_config
|
50
|
+
assert_equal custom_data_config, IntercomRails.config.custom_data
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_reset_clears_existing_config
|
54
|
+
IntercomRails.config.custom_data = {'muffin' => :muffin}
|
55
|
+
IntercomRails.config.reset!
|
56
|
+
assert_equal nil, IntercomRails.config.custom_data
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_reset_clears_inbox_config_too
|
60
|
+
IntercomRails.config.inbox.style = :custom
|
61
|
+
IntercomRails.config.reset!
|
62
|
+
assert_equal nil, IntercomRails.config.inbox.style
|
63
|
+
end
|
64
|
+
|
30
65
|
end
|
@@ -2,6 +2,7 @@ require 'import_test_setup'
|
|
2
2
|
|
3
3
|
class ImportUnitTest < MiniTest::Unit::TestCase
|
4
4
|
|
5
|
+
include InterTest
|
5
6
|
include ImportTest
|
6
7
|
|
7
8
|
def test_run_with_wrong_rails_env
|
@@ -44,29 +45,6 @@ class ImportUnitTest < MiniTest::Unit::TestCase
|
|
44
45
|
assert_equal exception.message, "Please add an Intercom API Key to config/initializers/intercom.rb"
|
45
46
|
end
|
46
47
|
|
47
|
-
def test_user_for_wire_returns_nil_if_no_user_id_or_email
|
48
|
-
user = Object.new
|
49
|
-
user.instance_eval do
|
50
|
-
def name
|
51
|
-
"Ben"
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
hsh = IntercomRails::Import.new.send(:user_for_wire, user)
|
56
|
-
assert_equal hsh, nil
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_user_for_wire_returns_hash_if_user_id_or_email
|
60
|
-
hsh = IntercomRails::Import.new.send(:user_for_wire, User.first)
|
61
|
-
assert_equal({:user_id => 1, :email => "ben@intercom.io", :name => "Ben McRedmond"}, hsh)
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_send_users_in_batches_prepares_users_for_wire
|
65
|
-
expected_batch = {:users => User.all.map { |u| IntercomRails::Import.new.send(:user_for_wire, u) }}.to_json
|
66
|
-
IntercomRails::Import.any_instance.should_receive(:send_users).with(expected_batch).and_return({'failed' => []})
|
67
|
-
IntercomRails::Import.run
|
68
|
-
end
|
69
|
-
|
70
48
|
def test_status_output
|
71
49
|
@import = IntercomRails::Import.new(:status_enabled => true)
|
72
50
|
@import.stub(:send_users).and_return('failed' => [1])
|
@@ -3,8 +3,14 @@ require 'test_setup'
|
|
3
3
|
|
4
4
|
class ScriptTagTest < MiniTest::Unit::TestCase
|
5
5
|
|
6
|
+
include InterTest
|
6
7
|
include IntercomRails
|
7
8
|
|
9
|
+
def setup
|
10
|
+
super
|
11
|
+
IntercomRails.config.app_id = 'script_tag_test'
|
12
|
+
end
|
13
|
+
|
8
14
|
def test_output_is_html_safe?
|
9
15
|
assert_equal true, ScriptTag.generate({}).html_safe?
|
10
16
|
end
|
@@ -59,4 +65,27 @@ class ScriptTagTest < MiniTest::Unit::TestCase
|
|
59
65
|
assert_equal Digest::SHA1.hexdigest('1234' + 'ben@intercom.io'), script_tag.intercom_settings[:user_hash]
|
60
66
|
end
|
61
67
|
|
68
|
+
def test_inbox_default_style
|
69
|
+
IntercomRails.config.inbox.style = :default
|
70
|
+
script_tag = ScriptTag.new
|
71
|
+
expected_widget_settings= {'activator' => '#IntercomDefaultWidget'}
|
72
|
+
assert_equal expected_widget_settings, script_tag.intercom_settings['widget']
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_inbox_custom_style
|
76
|
+
IntercomRails.config.inbox.style = :custom
|
77
|
+
script_tag = ScriptTag.new
|
78
|
+
expected_widget_settings = {'activator' => '#Intercom'}
|
79
|
+
assert_equal expected_widget_settings, script_tag.intercom_settings['widget']
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_inbox_custom_style_with_counter
|
83
|
+
IntercomRails.config.inbox.style = :custom
|
84
|
+
IntercomRails.config.inbox.counter = true
|
85
|
+
script_tag = ScriptTag.new
|
86
|
+
expected_widget_settings = {'activator' => '#Intercom', 'use_counter' => true}
|
87
|
+
assert_equal expected_widget_settings, script_tag.intercom_settings['widget']
|
88
|
+
end
|
89
|
+
|
90
|
+
|
62
91
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'test_setup'
|
2
|
+
|
3
|
+
class UserProxyTest < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
include InterTest
|
6
|
+
include IntercomRails
|
7
|
+
|
8
|
+
DUMMY_USER = dummy_user(:email => 'ciaran@intercom.io', :name => 'Ciaran Lee')
|
9
|
+
|
10
|
+
def test_raises_error_when_no_user_found
|
11
|
+
assert_raises(IntercomRails::NoUserFoundError) {
|
12
|
+
UserProxy.from_current_user_in_object(Object.new)
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_finds_current_user
|
17
|
+
object_with_current_user_method = Object.new
|
18
|
+
object_with_current_user_method.instance_eval do
|
19
|
+
def current_user
|
20
|
+
DUMMY_USER
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
@user_proxy = UserProxy.from_current_user_in_object(object_with_current_user_method)
|
25
|
+
assert_user_found
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_finds_user_instance_variable
|
29
|
+
object_with_instance_variable = Object.new
|
30
|
+
object_with_instance_variable.instance_eval do
|
31
|
+
@user = DUMMY_USER
|
32
|
+
end
|
33
|
+
|
34
|
+
@user_proxy = UserProxy.from_current_user_in_object(object_with_instance_variable)
|
35
|
+
assert_user_found
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_finds_config_user
|
39
|
+
object_from_config = Object.new
|
40
|
+
object_from_config.instance_eval do
|
41
|
+
def something_esoteric
|
42
|
+
DUMMY_USER
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
IntercomRails.config.current_user = Proc.new { something_esoteric }
|
47
|
+
@user_proxy = UserProxy.from_current_user_in_object(object_from_config)
|
48
|
+
assert_user_found
|
49
|
+
end
|
50
|
+
|
51
|
+
def assert_user_found
|
52
|
+
assert_equal DUMMY_USER, @user_proxy.user
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_includes_custom_data
|
56
|
+
plan_dummy_user = DUMMY_USER.dup
|
57
|
+
plan_dummy_user.instance_eval do
|
58
|
+
def plan
|
59
|
+
'pro'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
IntercomRails.config.custom_data = {
|
64
|
+
'plan' => :plan
|
65
|
+
}
|
66
|
+
|
67
|
+
@user_proxy = UserProxy.new(plan_dummy_user)
|
68
|
+
expected_custom_data = {'plan' => 'pro'}
|
69
|
+
assert_equal expected_custom_data, @user_proxy.to_hash[:custom_data]
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_valid_returns_true_if_user_id_or_email
|
73
|
+
assert_equal true, UserProxy.new(DUMMY_USER).valid?
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_includes_custom_data_from_intercom_custom_data
|
77
|
+
object_with_intercom_custom_data = Object.new
|
78
|
+
object_with_intercom_custom_data.instance_eval do
|
79
|
+
def intercom_custom_data
|
80
|
+
{:ponies => :rainbows}
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
@user_proxy = UserProxy.new(DUMMY_USER, object_with_intercom_custom_data)
|
85
|
+
expected_custom_data = {:ponies => :rainbows}
|
86
|
+
assert_equal expected_custom_data, @user_proxy.to_hash[:custom_data]
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
data/test/test_setup.rb
CHANGED
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.0
|
4
|
+
version: 0.1.0
|
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: 2012-11-
|
14
|
+
date: 2012-11-23 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
@@ -139,13 +139,14 @@ files:
|
|
139
139
|
- lib/data/cacert.pem
|
140
140
|
- lib/intercom-rails/auto_include_filter.rb
|
141
141
|
- lib/intercom-rails/config.rb
|
142
|
-
- lib/intercom-rails/
|
142
|
+
- lib/intercom-rails/custom_data_helper.rb
|
143
143
|
- lib/intercom-rails/exceptions.rb
|
144
144
|
- lib/intercom-rails/import.rb
|
145
145
|
- lib/intercom-rails/intercom.rake
|
146
146
|
- lib/intercom-rails/railtie.rb
|
147
147
|
- lib/intercom-rails/script_tag.rb
|
148
148
|
- lib/intercom-rails/script_tag_helper.rb
|
149
|
+
- lib/intercom-rails/user_proxy.rb
|
149
150
|
- lib/intercom-rails/version.rb
|
150
151
|
- lib/intercom-rails.rb
|
151
152
|
- lib/rails/generators/intercom/config/config_generator.rb
|
@@ -157,11 +158,11 @@ files:
|
|
157
158
|
- test/import_test_setup.rb
|
158
159
|
- test/intercom-rails/auto_include_filter_test.rb
|
159
160
|
- test/intercom-rails/config_test.rb
|
160
|
-
- test/intercom-rails/current_user_test.rb
|
161
161
|
- test/intercom-rails/import_network_test.rb
|
162
162
|
- test/intercom-rails/import_unit_test.rb
|
163
163
|
- test/intercom-rails/script_tag_helper_test.rb
|
164
164
|
- test/intercom-rails/script_tag_test.rb
|
165
|
+
- test/intercom-rails/user_proxy_test.rb
|
165
166
|
- test/test_setup.rb
|
166
167
|
homepage: http://www.intercom.io
|
167
168
|
licenses: []
|
@@ -177,7 +178,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
177
178
|
version: '0'
|
178
179
|
segments:
|
179
180
|
- 0
|
180
|
-
hash:
|
181
|
+
hash: -888650267996425080
|
181
182
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
183
|
none: false
|
183
184
|
requirements:
|
@@ -186,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
186
187
|
version: '0'
|
187
188
|
segments:
|
188
189
|
- 0
|
189
|
-
hash:
|
190
|
+
hash: -888650267996425080
|
190
191
|
requirements: []
|
191
192
|
rubyforge_project: intercom-rails
|
192
193
|
rubygems_version: 1.8.23
|
@@ -198,9 +199,9 @@ test_files:
|
|
198
199
|
- test/import_test_setup.rb
|
199
200
|
- test/intercom-rails/auto_include_filter_test.rb
|
200
201
|
- test/intercom-rails/config_test.rb
|
201
|
-
- test/intercom-rails/current_user_test.rb
|
202
202
|
- test/intercom-rails/import_network_test.rb
|
203
203
|
- test/intercom-rails/import_unit_test.rb
|
204
204
|
- test/intercom-rails/script_tag_helper_test.rb
|
205
205
|
- test/intercom-rails/script_tag_test.rb
|
206
|
+
- test/intercom-rails/user_proxy_test.rb
|
206
207
|
- test/test_setup.rb
|
@@ -1,53 +0,0 @@
|
|
1
|
-
module IntercomRails
|
2
|
-
|
3
|
-
class CurrentUser
|
4
|
-
|
5
|
-
POTENTIAL_USER_OBJECTS = [
|
6
|
-
Proc.new { instance_eval &IntercomRails.config.current_user if IntercomRails.config.current_user.present? },
|
7
|
-
Proc.new { current_user },
|
8
|
-
Proc.new { @user }
|
9
|
-
]
|
10
|
-
|
11
|
-
def self.locate_and_prepare_for_intercom(*args)
|
12
|
-
new(*args).to_hash
|
13
|
-
end
|
14
|
-
|
15
|
-
attr_reader :search_object, :user
|
16
|
-
|
17
|
-
def initialize(search_object)
|
18
|
-
@search_object = search_object
|
19
|
-
@user = find_user
|
20
|
-
end
|
21
|
-
|
22
|
-
def find_user
|
23
|
-
POTENTIAL_USER_OBJECTS.each do |potential_user|
|
24
|
-
begin
|
25
|
-
user = search_object.instance_eval &potential_user
|
26
|
-
return user if user.present? &&
|
27
|
-
(user.email.present? || user.id.present?)
|
28
|
-
rescue NameError
|
29
|
-
next
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
raise CurrentUserNotFoundError
|
34
|
-
end
|
35
|
-
|
36
|
-
def to_hash
|
37
|
-
hsh = {}
|
38
|
-
hsh[:user_id] = user.id if attribute_present?(:id)
|
39
|
-
[:email, :name, :created_at].each do |attribute|
|
40
|
-
hsh[attribute] = user.send(attribute) if attribute_present?(attribute)
|
41
|
-
end
|
42
|
-
|
43
|
-
hsh
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
def attribute_present?(attribute)
|
48
|
-
user.respond_to?(attribute) && user.send(attribute).present?
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'test_setup'
|
2
|
-
|
3
|
-
class CurrentUserTest < MiniTest::Unit::TestCase
|
4
|
-
|
5
|
-
include IntercomRails
|
6
|
-
|
7
|
-
DUMMY_USER = dummy_user(:email => 'ciaran@intercom.io', :name => 'Ciaran Lee')
|
8
|
-
|
9
|
-
def test_raises_error_when_no_user_found
|
10
|
-
assert_raises(IntercomRails::CurrentUserNotFoundError) {
|
11
|
-
CurrentUser.new(Object.new)
|
12
|
-
}
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_finds_current_user
|
16
|
-
object_with_current_user_method = Object.new
|
17
|
-
object_with_current_user_method.instance_eval do
|
18
|
-
def current_user
|
19
|
-
DUMMY_USER
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
@current_user = CurrentUser.new(object_with_current_user_method)
|
24
|
-
assert_user_found
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_finds_user_instance_variable
|
28
|
-
object_with_instance_variable = Object.new
|
29
|
-
object_with_instance_variable.instance_eval do
|
30
|
-
@user = DUMMY_USER
|
31
|
-
end
|
32
|
-
|
33
|
-
@current_user = CurrentUser.new(object_with_instance_variable)
|
34
|
-
assert_user_found
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_finds_config_user
|
38
|
-
object_from_config = Object.new
|
39
|
-
object_from_config.instance_eval do
|
40
|
-
def something_esoteric
|
41
|
-
DUMMY_USER
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
IntercomRails.config.current_user = Proc.new { something_esoteric }
|
46
|
-
@current_user = CurrentUser.new(object_from_config)
|
47
|
-
assert_user_found
|
48
|
-
end
|
49
|
-
|
50
|
-
def assert_user_found
|
51
|
-
assert_equal DUMMY_USER, @current_user.user
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|