intercom-rails 0.2.2 → 0.2.3

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.
@@ -59,6 +59,10 @@ module IntercomRails
59
59
  raise ArgumentError, "user.model should be a Proc" unless value.kind_of?(Proc)
60
60
  end
61
61
 
62
+ config_accessor :company_association do |value|
63
+ raise ArgumentError, "company_association should be a Proc" unless value.kind_of?(Proc)
64
+ end
65
+
62
66
  config_accessor :custom_data do |value|
63
67
  raise ArgumentError, "user.custom_data should be a hash" unless value.kind_of?(Hash)
64
68
  unless value.reject { |_,v| v.kind_of?(Proc) || v.kind_of?(Symbol) }.count.zero?
@@ -70,7 +70,13 @@ module IntercomRails
70
70
  user_klass.find_in_batches(:batch_size => MAX_BATCH_SIZE) do |users|
71
71
  users_for_wire = users.map do |u|
72
72
  user_proxy = Proxy::User.new(u)
73
- user_proxy.valid? ? user_proxy.to_hash : nil
73
+ next unless user_proxy.valid?
74
+
75
+ for_wire = user_proxy.to_hash
76
+ companies = Proxy::Company.companies_for_user(user_proxy)
77
+ for_wire.merge!(:companies => companies.map(&:to_hash)) if companies.present?
78
+
79
+ for_wire
74
80
  end.compact
75
81
 
76
82
  yield(prepare_batch(users_for_wire), users_for_wire.count) unless users_for_wire.count.zero?
@@ -43,12 +43,12 @@ module IntercomRails
43
43
  self.class.type
44
44
  end
45
45
 
46
- def self.config
47
- IntercomRails.config.send(type)
46
+ def self.config(type_override = nil)
47
+ IntercomRails.config.send(type_override || type)
48
48
  end
49
49
 
50
- def config
51
- self.class.config
50
+ def config(type_override = nil)
51
+ self.class.config(type_override)
52
52
  end
53
53
 
54
54
  private
@@ -4,6 +4,14 @@ module IntercomRails
4
4
 
5
5
  class Company < Proxy
6
6
 
7
+ def self.companies_for_user(user)
8
+ return unless config(:user).company_association.present?
9
+ companies = config(:user).company_association.call(user.user)
10
+ return unless companies.kind_of?(Array)
11
+
12
+ companies.map { |company| new(company) }.select { |company_proxy| company_proxy.valid? }
13
+ end
14
+
7
15
  def self.current_in_context(search_object)
8
16
  begin
9
17
  if config.current.present?
@@ -4,14 +4,21 @@ module IntercomRails
4
4
 
5
5
  class User < Proxy
6
6
 
7
- POTENTIAL_USER_OBJECTS = [
8
- Proc.new { instance_eval &IntercomRails.config.user.current if IntercomRails.config.user.current.present? },
7
+ PREDEFINED_POTENTIAL_USER_OBJECTS = [
9
8
  Proc.new { current_user },
10
9
  Proc.new { @user }
11
10
  ]
12
11
 
12
+ def self.potential_user_objects
13
+ if config.current.present?
14
+ [Proc.new { instance_eval &IntercomRails.config.user.current }]
15
+ else
16
+ PREDEFINED_POTENTIAL_USER_OBJECTS
17
+ end
18
+ end
19
+
13
20
  def self.current_in_context(search_object)
14
- POTENTIAL_USER_OBJECTS.each do |potential_object|
21
+ potential_user_objects.each do |potential_object|
15
22
  begin
16
23
  user_proxy = new(search_object.instance_eval(&potential_object), search_object)
17
24
  return user_proxy if user_proxy.valid?
@@ -1,3 +1,3 @@
1
1
  module IntercomRails
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -45,6 +45,13 @@ IntercomRails.config do |config|
45
45
  # }
46
46
 
47
47
  <%- if @app_id == 'tx2p130c' -%>
48
+ # == User -> Company association
49
+ # A Proc that given a user returns an array of companies
50
+ # that the user belongs to.
51
+ #
52
+ # config.user.company_association = Proc.new { |user| user.companies.to_a }
53
+ # config.user.company_association = Proc.new { |user| [user.company] }
54
+
48
55
  # == Current company name
49
56
  # The method/variable that contains the current company for the current user,
50
57
  # in your controllers. 'Companies' are generic groupings of users, so this
@@ -70,4 +70,28 @@ output
70
70
  $stdout = @old_stdout
71
71
  end
72
72
 
73
+ def test_prepares_companies
74
+ @import = IntercomRails::Import.new
75
+
76
+ u = dummy_user
77
+ u.instance_eval do
78
+ def apps
79
+ [dummy_company]
80
+ end
81
+ end
82
+
83
+ User.stub(:find_in_batches).and_yield([u])
84
+
85
+ IntercomRails.config.user.company_association = Proc.new { |user| user.apps }
86
+
87
+ prepare_for_batch_users = nil
88
+ @import.stub(:prepare_batch) { |users| prepare_for_batch_users = users }
89
+ @import.stub(:send_users).and_return('failed' => [])
90
+
91
+ @import.run
92
+
93
+ assert_equal 1, prepare_for_batch_users[0][:companies].length
94
+ User.rspec_reset
95
+ end
96
+
73
97
  end
@@ -29,4 +29,18 @@ class CompanyTest < MiniTest::Unit::TestCase
29
29
  assert_equal false, Company.new(search_object).valid?
30
30
  end
31
31
 
32
+ def test_companies_for_user
33
+ IntercomRails.config.user.company_association = Proc.new { |user| user.apps }
34
+ test_user = dummy_user
35
+ test_user.instance_eval do
36
+ def apps
37
+ [DUMMY_COMPANY, dummy_company(:name => "Prey", :id => "800")]
38
+ end
39
+ end
40
+
41
+ companies = Company.companies_for_user(IntercomRails::Proxy::User.new(test_user))
42
+ assert_equal 2, companies.length
43
+ assert_equal ["Intercom", "Prey"], companies.map(&:company).map(&:name)
44
+ end
45
+
32
46
  end
@@ -50,6 +50,18 @@ class UserTest < MiniTest::Unit::TestCase
50
50
  assert_user_found
51
51
  end
52
52
 
53
+ def test_finds_config_user_does_not_fallback_to_auto_find_users
54
+ IntercomRails.config.user.current = Proc.new { something_esoteric }
55
+ object_with_instance_variable = Object.new
56
+ object_with_instance_variable.instance_eval do
57
+ @user = DUMMY_USER
58
+ end
59
+
60
+ assert_raises(IntercomRails::NoUserFoundError) {
61
+ User.current_in_context(object_with_instance_variable)
62
+ }
63
+ end
64
+
53
65
  def assert_user_found
54
66
  assert_equal DUMMY_USER, @user_proxy.user
55
67
  end
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.2
4
+ version: 0.2.3
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-30 00:00:00.000000000 Z
14
+ date: 2012-12-03 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -179,12 +179,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
179
179
  - - ! '>='
180
180
  - !ruby/object:Gem::Version
181
181
  version: '0'
182
+ segments:
183
+ - 0
184
+ hash: 3543058584260120985
182
185
  required_rubygems_version: !ruby/object:Gem::Requirement
183
186
  none: false
184
187
  requirements:
185
188
  - - ! '>='
186
189
  - !ruby/object:Gem::Version
187
190
  version: '0'
191
+ segments:
192
+ - 0
193
+ hash: 3543058584260120985
188
194
  requirements: []
189
195
  rubyforge_project: intercom-rails
190
196
  rubygems_version: 1.8.23