userlist-rails 0.2.0 → 0.5.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
  SHA256:
3
- metadata.gz: ef228cf5cb636af878d4c252b272ad9cdb4bd6ab462ddcb9ad0cfacc71a191fb
4
- data.tar.gz: e9577e9c3db7171665106a8b10977adeff7a05a90e6a892521667891701e9ec3
3
+ metadata.gz: 9fe4b336d98e1d3015aa44aa9a261c37bf160747573d6507f44f533a14e6e4dd
4
+ data.tar.gz: 77261e5a6972ce2b0f0d0ea43e141c3d4c33011790c01a8fd32749743c35b342
5
5
  SHA512:
6
- metadata.gz: 5b197cf9e6078baa517cd15a6cb8ee156164f37e154378b915bf024767adcdc155ffdb4323be007de9617e6f5194ce92b2e38d314c49513056650c4e2b2d08fe
7
- data.tar.gz: 3f2f7a0c82ae87ec3c66f8595bdd9a994d5c33ea2e806764bc29770248b93931d820ac10caeceafde4469b234e342d5c6d4465ab99f47467ae5c66c30bfb5a01
6
+ metadata.gz: a3843cb90d6f4868179c43972e3b0877f8d1cd2e02637b03799f9778571d1115c44ee6c7dc320407d50e0cea7305d1b019d6bb63526b42f8dd109d180b015834
7
+ data.tar.gz: 5147105865f3bfccc0750823f69889e76c34d7cd2f18b149fc6cd4ffc8bb7e80a3103cccd88dc14a2f3520bf9747b1cfbceaee1cf313ae37ecec3cf2a5fcc406
@@ -0,0 +1,22 @@
1
+ name: Tests
2
+ on: [push]
3
+ jobs:
4
+ build:
5
+ strategy:
6
+ matrix:
7
+ ruby:
8
+ - 2.4
9
+ - 2.4
10
+ - 2.5
11
+ - 2.6
12
+ - 2.7
13
+ - 3.0
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby }}
20
+ bundler-cache: true
21
+ - name: RSpec
22
+ run: bundle exec rake
@@ -1,10 +1,12 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.2
2
+ TargetRubyVersion: 2.4
3
+ NewCops: enable
4
+ SuggestExtensions: false
3
5
  Exclude:
4
6
  - 'bin/*'
5
7
  - 'Guardfile'
6
8
 
7
- Metrics/LineLength:
9
+ Layout/LineLength:
8
10
  Enabled: false
9
11
 
10
12
  Metrics/ModuleLength:
@@ -32,13 +34,10 @@ Layout/ElseAlignment:
32
34
  Lint/AssignmentInCondition:
33
35
  Enabled: false
34
36
 
35
- Lint/EndAlignment:
36
- EnforcedStyleAlignWith: start_of_line
37
-
38
37
  Layout/AccessModifierIndentation:
39
38
  EnforcedStyle: outdent
40
39
 
41
- Layout/AlignParameters:
40
+ Layout/ParameterAlignment:
42
41
  EnforcedStyle: with_fixed_indentation
43
42
 
44
43
  Style/FrozenStringLiteralComment:
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
55
55
  ## Enforcement
56
56
 
57
57
  Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at benedikt@benediktdeicke.com. All
58
+ reported by contacting the project team at support@userlist.com. All
59
59
  complaints will be reviewed and investigated and will result in a response that
60
60
  is deemed necessary and appropriate to the circumstances. The project team is
61
61
  obligated to maintain confidentiality with regard to the reporter of an incident.
data/Gemfile CHANGED
@@ -4,7 +4,7 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  gemspec
6
6
 
7
- gem 'userlist', github: 'userlistio/userlist-ruby', branch: 'master'
7
+ gem 'userlist', github: 'userlist/userlist-ruby', branch: 'master'
8
8
 
9
9
  gem 'guard-rspec', '~> 4.7'
10
10
  gem 'guard-rubocop', '~> 1.3'
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Userlist::Rails
1
+ # Userlist for Ruby on Rails [![Build Status](https://github.com/userlist/userlist-rails/workflows/Tests/badge.svg)](https://github.com/userlist/userlist-rails)
2
2
 
3
- This gem helps with integrating [Userlist.io](http://userlist.io) into Ruby on Rails applications.
3
+ This gem helps with integrating [Userlist](http://userlist.com) into Ruby on Rails applications.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,14 +20,15 @@ Or install it yourself as:
20
20
 
21
21
  ## Configuration
22
22
 
23
- The only required configuration is the Push API key. You can get your Push API key via the [Push API settings](https://app.userlist.io/settings/push) in your Userlist.io account.
23
+ The only required configuration is the Push API key. You can get your Push API key via the [Push API settings](https://app.userlist.com/settings/push) in your Userlist account.
24
24
 
25
- Configuration values can either be set via an initializer or as environment variables. The environment take precedence over configuration values from the initializer. Please refer to the [userlist-ruby](http://github.com/userlistio/userlist-ruby) gem for additional configuration options.
25
+ Configuration values can either be set via an initializer or as environment variables. The environment take precedence over configuration values from the initializer. Please refer to the [userlist](http://github.com/userlist/userlist-ruby) gem for additional configuration options.
26
26
 
27
27
  Configuration via environment variables:
28
28
 
29
29
  ```shell
30
- USERLIST_PUSH_KEY=401e5c498be718c0a38b7da7f1ce5b409c56132a49246c435ee296e07bf2be39
30
+ USERLIST_PUSH_KEY=VvB7pjDrv0V2hoaOCeZ5rIiUEPbEhSUN
31
+ USERLIST_PUSH_ID=6vPkJl44cm82y4aLBIzaOhuEHJd0Bm7b
31
32
  ```
32
33
 
33
34
  Configuration via an initializer:
@@ -35,17 +36,40 @@ Configuration via an initializer:
35
36
  ```ruby
36
37
  # config/initializer/userlist.rb
37
38
  Userlist.configure do |config|
38
- config.push_key = '401e5c498be718c0a38b7da7f1ce5b409c56132a49246c435ee296e07bf2be39'
39
+ config.push_key = 'VvB7pjDrv0V2hoaOCeZ5rIiUEPbEhSUN'
40
+ config.push_id = '6vPkJl44cm82y4aLBIzaOhuEHJd0Bm7b'
41
+ end
42
+ ```
43
+
44
+ In addition to the configuration options of the [userlist](http://github.com/userlist/userlist-ruby#configuration) gem, the following options are available.
45
+
46
+ | Name | Default value | Description |
47
+ | --------------- | ---------------------------- | -------------------------------------------------------------------------------------------------------------------- |
48
+ | `user_model` | `nil` | The user model to use. Will be automatically set when `auto_discover` is `true` |
49
+ | `company_model` | `nil` | The company model to use. Will be automatically set when `auto_discover` is `true` |
50
+ | `auto_discover` | `true` | The gem will try to automatically identify your `User` and `Company` models. Possible values are `true` and `false`. |
51
+ | `script_url` | `https://js.userlist.com/v1` | The script url to load the Userlist in-app messages script from. |
52
+
53
+ ### Disabling in development and test environments
54
+
55
+ As sending test and development data into data into Userlist isn't very desirable, you can disable transmissions by setting the push strategy to `:null`.
56
+
57
+ ```ruby
58
+ # config/initializer/userlist.rb
59
+ Userlist.configure do |config|
60
+ config.push_strategy = :null unless Rails.env.production?
39
61
  end
40
62
  ```
41
63
 
42
64
  ## Usage
43
65
 
66
+ > ⚠️ **Important:** If you're using [Segment](https://segment.com) in combination with this gem, please make sure that both are using the same user identifier. By default, this gem will send `"user-#{id}"` (a combination of the user's primary key in the database and the `user-` prefix) as identifier. Either customize the `userlist_identifier` method on your User model, or ensure that you use the same identifier in your Segment integration.
67
+
44
68
  ### Tracking Users
45
69
 
46
70
  #### Sending user data automatically
47
71
 
48
- By default, this gem will automatically detect your `User` model and create and update the corresponding user inside of Userlist. To customize the `identifier`, `email`, or `properties` transmitted for a user, you can overwrite the according methods in your `User` model.
72
+ By default, this gem will automatically detect your `User` model and create, update, and delete the corresponding user inside of Userlist. To customize the `identifier`, `email`, or `properties` transmitted for a user, you can overwrite the according methods in your `User` model.
49
73
 
50
74
  ```ruby
51
75
  class User < ApplicationRecord
@@ -65,18 +89,30 @@ end
65
89
 
66
90
  #### Sending user data manually
67
91
 
68
- To manually send user data into Userlist, use the `Userlist::Push.user` method.
92
+ To manually send user data into Userlist, use the `Userlist::Push.users.push` method.
93
+
94
+ ```ruby
95
+ Userlist::Push.users.push(user)
96
+ ```
97
+
98
+ It's also possible to customize the payload sent to Userlist by passing a hash instead of the user object.
69
99
 
70
100
  ```ruby
71
- Userlist::Push.event(identifier: user.id, email: user.email, properties: { first_name: user.first_name, last_name: user.last_name })
101
+ Userlist::Push.users.push(identifier: user.id, email: user.email, properties: { first_name: user.first_name, last_name: user.last_name })
102
+ ```
103
+
104
+ It's also possible to delete a user from Userlist, using the `Userlist::Push.users.delete` method.
105
+
106
+ ```ruby
107
+ Userlist::Push.users.delete(user)
72
108
  ```
73
109
 
74
110
  ### Tracking Events
75
111
 
76
- To track custom events use the `Userlist::Push.event` method.
112
+ To track custom events use the `Userlist::Push.events.push` method.
77
113
 
78
114
  ```ruby
79
- Userlist::Push.event(name: 'project_created', user: current_user, properties: { project_name: project.name })
115
+ Userlist::Push.events.push(name: 'project_created', user: current_user, properties: { project_name: project.name })
80
116
  ```
81
117
 
82
118
  It is possible to make the `user` property optional by setting it for the entire request using an `around_action` callback in your `ApplicationController`.
@@ -94,7 +130,21 @@ end
94
130
  This simplifies the tracking call for the current request.
95
131
 
96
132
  ```ruby
97
- Userlist::Push.event(name: 'project_created', properties: { project_name: project.name })
133
+ Userlist::Push.events.push(name: 'project_created', properties: { project_name: project.name })
134
+ ```
135
+
136
+ ### Enabling in-app messages
137
+
138
+ In order to use in-app messages, please set both the `push_key` and `push_id` configuration variables. Afterwards, include the `userlist_script_tag` helper into your application's layout for signed in users.
139
+
140
+ ```erb
141
+ <%= userlist_script_tag %>
142
+ ```
143
+
144
+ By default, the helper will try to use the `current_user` helper to read the currently signed in user. You can change the default bebahvior by passing a different user. The passed object must respond to the `userlist_identifier` method.
145
+
146
+ ```erb
147
+ <%= userlist_script_tag(user) %>
98
148
  ```
99
149
 
100
150
  ### Batch importing
@@ -113,7 +163,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
113
163
 
114
164
  ## Contributing
115
165
 
116
- Bug reports and pull requests are welcome on GitHub at https://github.com/userlistio/userlist-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
166
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/userlist/userlist-rails>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
117
167
 
118
168
  ## License
119
169
 
@@ -121,4 +171,12 @@ The gem is available as open source under the terms of the [MIT License](https:/
121
171
 
122
172
  ## Code of Conduct
123
173
 
124
- Everyone interacting in the Userlist::Rails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/userlistio/userlist-rails/blob/master/CODE_OF_CONDUCT.md).
174
+ Everyone interacting in the Userlist::Rails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/userlist/userlist-rails/blob/master/CODE_OF_CONDUCT.md).
175
+
176
+ ## What is Userlist?
177
+
178
+ [![Userlist](https://userlist.com/images/external/userlist-logo-github.svg)](https://userlist.com/)
179
+
180
+ [Userlist](https://userlist.com/) allows you to onboard and engage your SaaS users with targeted behavior-based campaigns using email or in-app messages.
181
+
182
+ Userlist was started in 2017 as an alternative to bulky enterprise messaging tools. We believe that running SaaS products should be more enjoyable. Learn more [about us](https://userlist.com/about-us/).
@@ -14,6 +14,17 @@ module Userlist
14
14
  Thread.current[:userlist_current_user]
15
15
  end
16
16
 
17
+ def self.with_current_company(company)
18
+ Thread.current[:userlist_current_company] = company
19
+ yield
20
+ ensure
21
+ Thread.current[:userlist_current_company] = nil
22
+ end
23
+
24
+ def self.current_company
25
+ Thread.current[:userlist_current_company]
26
+ end
27
+
17
28
  def self.detect_model(*names)
18
29
  names.each do |name|
19
30
  begin
@@ -25,5 +36,49 @@ module Userlist
25
36
 
26
37
  nil
27
38
  end
39
+
40
+ def self.detect_relationship(from, to)
41
+ return unless reflection = find_reflection(from, to)
42
+
43
+ reflection.through_reflection.klass if reflection.through_reflection?
44
+ end
45
+
46
+ def self.find_reflection(from, to)
47
+ return unless from && to
48
+
49
+ from.reflect_on_all_associations.find { |r| r.class_name == to.to_s }
50
+ end
51
+
52
+ def self.setup_callbacks(model, scope)
53
+ return if model.instance_variable_get(:@userlist_callbacks_registered)
54
+
55
+ setup_callback(:create, model, scope, :push)
56
+ setup_callback(:update, model, scope, :push)
57
+ setup_callback(:destroy, model, scope, :delete)
58
+
59
+ model.instance_variable_set(:@userlist_callbacks_registered, true)
60
+ end
61
+
62
+ def self.setup_callback(type, model, scope, method)
63
+ return unless callback_method = [:after_commit, :"after_#{type}"].find { |m| model.respond_to?(m) }
64
+
65
+ callback = lambda do
66
+ begin
67
+ relation = Userlist::Push.public_send(scope)
68
+ relation.public_send(method, self)
69
+ rescue Userlist::Error => e
70
+ Userlist.logger.error "Failed to #{method} #{method.to_s.singularize}: #{e.message}"
71
+ end
72
+ end
73
+
74
+ model.public_send(callback_method, callback, on: type)
75
+ end
76
+
77
+ def self.setup_extensions
78
+ Userlist::Push::User.include(Userlist::Rails::Extensions::User)
79
+ Userlist::Push::Company.include(Userlist::Rails::Extensions::Company)
80
+ Userlist::Push::Relationship.include(Userlist::Rails::Extensions::Relationship)
81
+ Userlist::Push::Event.include(Userlist::Rails::Extensions::Event)
82
+ end
28
83
  end
29
84
  end
@@ -1,12 +1,21 @@
1
1
  require 'userlist/config'
2
2
 
3
+ require 'userlist/rails/transforms/user'
4
+ require 'userlist/rails/transforms/company'
5
+ require 'userlist/rails/transforms/relationship'
6
+
3
7
  module Userlist
4
8
  module Rails
5
9
  module Config
6
10
  DEFAULT_CONFIGURATION = {
7
11
  user_model: nil,
8
12
  company_model: nil,
9
- auto_discover: true
13
+ relationship_model: nil,
14
+ auto_discover: true,
15
+ script_url: 'https://js.userlist.com/v1',
16
+ user_transform: Userlist::Rails::Transforms::User,
17
+ company_transform: Userlist::Rails::Transforms::Company,
18
+ relationship_transform: Userlist::Rails::Transforms::Relationship
10
19
  }.freeze
11
20
 
12
21
  def default_config
@@ -18,13 +27,11 @@ module Userlist
18
27
  end
19
28
 
20
29
  def user_model
21
- model = super
22
- model && model.to_s.constantize
30
+ (model = super) && model.is_a?(Class) ? model : model&.to_s&.constantize
23
31
  end
24
32
 
25
33
  def company_model
26
- model = super
27
- model && model.to_s.constantize
34
+ (model = super) && model.is_a?(Class) ? model : model&.to_s&.constantize
28
35
  end
29
36
 
30
37
  Userlist::Config.send(:prepend, self)
@@ -0,0 +1,30 @@
1
+ module Userlist
2
+ module Rails
3
+ module Extensions
4
+ module Company
5
+ module ClassMethods
6
+ def from_payload(payload, config = Userlist.config)
7
+ company_model = config.company_model
8
+ company_transform = config.company_transform
9
+
10
+ payload = company_transform.new(payload, config) if company_model && payload.is_a?(company_model)
11
+
12
+ super
13
+ end
14
+ end
15
+
16
+ def self.included(base)
17
+ base.extend(ClassMethods)
18
+ end
19
+
20
+ def push?
21
+ super && (!payload.respond_to?(:push?) || payload.push?)
22
+ end
23
+
24
+ def delete?
25
+ super && (!payload.respond_to?(:delete?) || payload.delete?)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ module Userlist
2
+ module Rails
3
+ module Extensions
4
+ module Event
5
+ module ClassMethods
6
+ def new(payload, config = Userlist.config)
7
+ payload[:user] = Userlist::Rails.current_user unless payload.key?(:user)
8
+ payload[:company] = Userlist::Rails.current_company unless payload.key?(:company)
9
+
10
+ super
11
+ end
12
+ end
13
+
14
+ def self.included(base)
15
+ base.extend(ClassMethods)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ module Userlist
2
+ module Rails
3
+ module Extensions
4
+ module Relationship
5
+ module ClassMethods
6
+ def from_payload(payload, config = Userlist.config)
7
+ relationship_model = config.relationship_model
8
+ relationship_transform = config.relationship_transform
9
+
10
+ payload = relationship_transform.new(payload, config) if relationship_model && payload.is_a?(relationship_model)
11
+
12
+ super
13
+ end
14
+ end
15
+
16
+ def self.included(base)
17
+ base.extend(ClassMethods)
18
+ end
19
+
20
+ def push?
21
+ super && (!payload.respond_to?(:push?) || payload.push?)
22
+ end
23
+
24
+ def delete?
25
+ super && (!payload.respond_to?(:delete?) || payload.delete?)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ module Userlist
2
+ module Rails
3
+ module Extensions
4
+ module User
5
+ module ClassMethods
6
+ def from_payload(payload, config = Userlist.config)
7
+ user_model = config.user_model
8
+ user_transform = config.user_transform
9
+
10
+ payload = user_transform.new(payload, config) if user_model && payload.is_a?(user_model)
11
+
12
+ super
13
+ end
14
+ end
15
+
16
+ def self.included(base)
17
+ base.extend(ClassMethods)
18
+ end
19
+
20
+ def push?
21
+ super && (!payload.respond_to?(:push?) || payload.push?)
22
+ end
23
+
24
+ def delete?
25
+ super && (!payload.respond_to?(:delete?) || payload.delete?)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ module Userlist
2
+ module Rails
3
+ module Helpers
4
+ def userlist_script_tag(*args) # rubocop:disable Metrics/CyclomaticComplexity
5
+ config = Userlist.config
6
+ logger = Userlist.logger
7
+
8
+ options = args.extract_options!
9
+
10
+ user = args.first
11
+ user ||= current_user if respond_to?(:current_user)
12
+ user ||= Userlist::Rails.current_user
13
+
14
+ options[:async] = true
15
+
16
+ if user
17
+ options[:data] ||= {}
18
+ options[:data][:userlist] = Userlist::Token.generate(user, config)
19
+ end
20
+
21
+ script_tag = javascript_tag('window.userlist=window.userlist||function(){(userlist.q=userlist.q||[]).push(arguments)};')
22
+ include_tag = javascript_include_tag(config.script_url, options)
23
+
24
+ script_tag + include_tag
25
+ rescue Userlist::Error => e
26
+ logger.error(e.message)
27
+ raw("<!-- #{e.class}: #{e.message} -->") unless ::Rails.env.production?
28
+ end
29
+ end
30
+ end
31
+ end
@@ -3,29 +3,46 @@ require 'rails/railtie'
3
3
  require 'userlist'
4
4
  require 'userlist/config'
5
5
  require 'userlist/rails/logger'
6
- require 'userlist/rails/push'
7
- require 'userlist/rails/user'
8
- require 'userlist/rails/company'
6
+
7
+ require 'userlist/rails/extensions/user'
8
+ require 'userlist/rails/extensions/company'
9
+ require 'userlist/rails/extensions/relationship'
10
+ require 'userlist/rails/extensions/event'
11
+
12
+ require 'userlist/rails/helpers'
9
13
 
10
14
  module Userlist
11
15
  module Rails
12
16
  class Railtie < ::Rails::Railtie
13
17
  rake_tasks do
14
- load 'tasks/userlist.rake'
18
+ load 'userlist/rails/tasks/userlist.rake'
15
19
  end
16
20
 
17
21
  initializer 'userlist.config' do
18
22
  config.userlist = Userlist.config
19
23
  end
20
24
 
25
+ initializer 'userlist.strategy' do
26
+ config.after_initialize do
27
+ strategy = config.userlist.push_strategy
28
+ Userlist::Push::Strategies.require_strategy(strategy)
29
+ end
30
+ end
31
+
21
32
  initializer 'userlist.logger' do
22
33
  config.after_initialize do
23
34
  Userlist.logger = Userlist::Rails::Logger.new(::Rails.logger, config.userlist)
24
35
  end
25
36
  end
26
37
 
38
+ initializer 'userlist.helpers' do
39
+ ActiveSupport.on_load :action_view do
40
+ include Userlist::Rails::Helpers
41
+ end
42
+ end
43
+
27
44
  initializer 'userlist.extensions' do
28
- Userlist::Push.send(:prepend, Userlist::Rails::Push)
45
+ Userlist::Rails.setup_extensions
29
46
  end
30
47
 
31
48
  initializer 'userlist.models' do
@@ -35,18 +52,24 @@ module Userlist
35
52
  if userlist.auto_discover
36
53
  Userlist.logger.info('Automatically discovering models')
37
54
 
38
- userlist.user_model ||= Userlist::Rails.detect_model('User')
39
- userlist.company_model ||= Userlist::Rails.detect_model('Account', 'Company')
55
+ userlist.user_model = Userlist::Rails.detect_model('User')
56
+ userlist.company_model = Userlist::Rails.detect_model('Account', 'Company')
57
+ userlist.relationship_model = Userlist::Rails.detect_relationship(userlist.user_model, userlist.company_model)
40
58
  end
41
59
 
42
60
  if user_model = userlist.user_model
43
61
  Userlist.logger.info("Preparing user model #{user_model}")
44
- user_model.send(:include, Userlist::Rails::User)
62
+ Userlist::Rails.setup_callbacks(user_model, :users)
45
63
  end
46
64
 
47
65
  if company_model = userlist.company_model
48
66
  Userlist.logger.info("Preparing company model #{company_model}")
49
- company_model.send(:include, Userlist::Rails::Company)
67
+ Userlist::Rails.setup_callbacks(company_model, :companies)
68
+ end
69
+
70
+ if relationship_model = userlist.relationship_model
71
+ Userlist.logger.info("Preparing relationship model #{relationship_model}")
72
+ Userlist::Rails.setup_callbacks(relationship_model, :relationships)
50
73
  end
51
74
  end
52
75
  end
@@ -4,24 +4,24 @@ require 'userlist/rails/importer'
4
4
 
5
5
  namespace :userlist do
6
6
  namespace :import do
7
- desc 'Import users into Userlist.io'
7
+ desc 'Import users into Userlist'
8
8
  task users: :environment do
9
9
  if user_model = Rails.application.config.userlist.user_model
10
10
  importer = Userlist::Rails::Importer.new
11
11
  importer.import(user_model) do |user|
12
- push.user(user.send(:userlist_payload))
12
+ push.users.create(user)
13
13
  end
14
14
  else
15
15
  puts 'No user model defined. Skipping import.'
16
16
  end
17
17
  end
18
18
 
19
- desc 'Import companies into Userlist.io'
19
+ desc 'Import companies into Userlist'
20
20
  task companies: :environment do
21
21
  if company_model = Rails.application.config.userlist.company_model
22
22
  importer = Userlist::Rails::Importer.new
23
23
  importer.import(company_model) do |company|
24
- push.company(company.send(:userlist_payload))
24
+ push.companies.create(company)
25
25
  end
26
26
  else
27
27
  puts 'No company model defined. Skipping import.'
@@ -29,7 +29,7 @@ namespace :userlist do
29
29
  end
30
30
  end
31
31
 
32
- desc 'Import users and companies into Userlist.io'
32
+ desc 'Import users and companies into Userlist'
33
33
  task import: ['userlist:import:users', 'userlist:import:companies']
34
34
  end
35
35
 
@@ -0,0 +1,42 @@
1
+ module Userlist
2
+ module Rails
3
+ class Transform
4
+ def self.attributes
5
+ @attributes = []
6
+ end
7
+
8
+ def initialize(model, config = Userlist.config)
9
+ @model = model
10
+ @config = config
11
+ end
12
+
13
+ def [](name)
14
+ public_send(name) if key?(name)
15
+ end
16
+
17
+ def key?(name)
18
+ keys.include?(name.to_sym)
19
+ end
20
+
21
+ def keys
22
+ self.class.attributes
23
+ end
24
+
25
+ def hash
26
+ model.hash
27
+ end
28
+
29
+ def push?
30
+ (!model.respond_to?(:push?) || model.push?)
31
+ end
32
+
33
+ def delete?
34
+ (!model.respond_to?(:delete?) || model.delete?)
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :model, :config
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ require 'userlist/rails/transform'
2
+
3
+ module Userlist
4
+ module Rails
5
+ module Transforms
6
+ class Company < Userlist::Rails::Transform
7
+ def self.attributes
8
+ @attributes ||= [
9
+ :identifier,
10
+ :properties,
11
+ :relationships,
12
+ :name,
13
+ :signed_up_at
14
+ ]
15
+ end
16
+
17
+ def identifier
18
+ model.try(:userlist_identifier) || "#{model.class.name}-#{model.id}".parameterize
19
+ end
20
+
21
+ def properties
22
+ model.try(:userlist_properties) || {}
23
+ end
24
+
25
+ def relationships
26
+ relationships_method = Userlist::Rails.find_reflection(config.company_model, config.relationship_model)&.name
27
+
28
+ model.try(:userlist_relationships) || (relationships_method && model.try(relationships_method))
29
+ end
30
+
31
+ def name
32
+ model.try(:userlist_name) || model.try(:name)
33
+ end
34
+
35
+ def signed_up_at
36
+ model.try(:created_at)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,33 @@
1
+ require 'userlist/rails/transform'
2
+
3
+ module Userlist
4
+ module Rails
5
+ module Transforms
6
+ class Relationship < Userlist::Rails::Transform
7
+ def self.attributes
8
+ @attributes ||= [
9
+ :user,
10
+ :company,
11
+ :properties
12
+ ]
13
+ end
14
+
15
+ def properties
16
+ model.try(:userlist_properties) || {}
17
+ end
18
+
19
+ def user
20
+ user_method = Userlist::Rails.find_reflection(config.relationship_model, config.user_model)&.name
21
+
22
+ model.try(:userlist_user) || (user_method && model.try(user_method))
23
+ end
24
+
25
+ def company
26
+ company_method = Userlist::Rails.find_reflection(config.relationship_model, config.company_model)&.name
27
+
28
+ model.try(:userlist_company) || (company_method && model.try(company_method))
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ require 'userlist/rails/transform'
2
+
3
+ module Userlist
4
+ module Rails
5
+ module Transforms
6
+ class User < Userlist::Rails::Transform
7
+ def self.attributes
8
+ @attributes ||= [
9
+ :identifier,
10
+ :properties,
11
+ :relationships,
12
+ :email,
13
+ :signed_up_at
14
+ ]
15
+ end
16
+
17
+ def identifier
18
+ model.try(:userlist_identifier) || "#{model.class.name}-#{model.id}".parameterize
19
+ end
20
+
21
+ def properties
22
+ model.try(:userlist_properties) || {}
23
+ end
24
+
25
+ def relationships
26
+ relationships_method = Userlist::Rails.find_reflection(config.user_model, config.relationship_model)&.name
27
+
28
+ model.try(:userlist_relationships) || (relationships_method && model.try(relationships_method))
29
+ end
30
+
31
+ def email
32
+ model.try(:userlist_email) || model.try(:email)
33
+ end
34
+
35
+ def signed_up_at
36
+ model.try(:created_at)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  module Userlist
2
2
  module Rails
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.5.0'.freeze
4
4
  end
5
5
  end
@@ -7,9 +7,9 @@ Gem::Specification.new do |spec|
7
7
  spec.name = 'userlist-rails'
8
8
  spec.version = Userlist::Rails::VERSION
9
9
  spec.authors = ['Benedikt Deicke']
10
- spec.email = ['benedikt@userlist.io']
10
+ spec.email = ['benedikt@userlist.com']
11
11
  spec.homepage = 'https://github.com/userlistio/userlist-rails'
12
- spec.summary = 'Rails integration for Userlist.io'
12
+ spec.summary = 'Rails integration for Userlist'
13
13
  spec.license = 'MIT'
14
14
 
15
15
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
@@ -19,13 +19,17 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_dependency 'activesupport', '>= 3.0'
23
- spec.add_dependency 'railties', '>= 3.0'
24
- spec.add_dependency 'userlist', '~> 0.2'
22
+ spec.required_ruby_version = '>= 2.4'
25
23
 
26
- spec.add_development_dependency 'bundler', '~> 1.15'
27
- spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_dependency 'activesupport', '>= 5.0'
25
+ spec.add_dependency 'railties', '>= 5.0'
26
+ spec.add_dependency 'userlist', '~> 0.5'
27
+
28
+ spec.add_development_dependency 'actionpack', '>= 5.0'
29
+ spec.add_development_dependency 'activerecord', '>= 5.0'
30
+ spec.add_development_dependency 'bundler', '>= 1.15'
31
+ spec.add_development_dependency 'rake', '~> 12.3', '>= 12.3.3'
28
32
  spec.add_development_dependency 'rspec', '~> 3.0'
29
- spec.add_development_dependency 'sqlite3'
30
- spec.add_development_dependency 'webmock', '~> 1.22'
33
+ spec.add_development_dependency 'rspec-rails', '~> 3.0'
34
+ spec.add_development_dependency 'sqlite3', '>= 1.4'
31
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: userlist-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benedikt Deicke
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-21 00:00:00.000000000 Z
11
+ date: 2021-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,54 +16,82 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.0'
19
+ version: '5.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '3.0'
26
+ version: '5.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: railties
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '3.0'
33
+ version: '5.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '3.0'
40
+ version: '5.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: userlist
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.2'
47
+ version: '0.5'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.2'
54
+ version: '0.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: actionpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: bundler
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
- - - "~>"
87
+ - - ">="
60
88
  - !ruby/object:Gem::Version
61
89
  version: '1.15'
62
90
  type: :development
63
91
  prerelease: false
64
92
  version_requirements: !ruby/object:Gem::Requirement
65
93
  requirements:
66
- - - "~>"
94
+ - - ">="
67
95
  - !ruby/object:Gem::Version
68
96
  version: '1.15'
69
97
  - !ruby/object:Gem::Dependency
@@ -72,14 +100,20 @@ dependencies:
72
100
  requirements:
73
101
  - - "~>"
74
102
  - !ruby/object:Gem::Version
75
- version: '10.0'
103
+ version: '12.3'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 12.3.3
76
107
  type: :development
77
108
  prerelease: false
78
109
  version_requirements: !ruby/object:Gem::Requirement
79
110
  requirements:
80
111
  - - "~>"
81
112
  - !ruby/object:Gem::Version
82
- version: '10.0'
113
+ version: '12.3'
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 12.3.3
83
117
  - !ruby/object:Gem::Dependency
84
118
  name: rspec
85
119
  requirement: !ruby/object:Gem::Requirement
@@ -95,44 +129,44 @@ dependencies:
95
129
  - !ruby/object:Gem::Version
96
130
  version: '3.0'
97
131
  - !ruby/object:Gem::Dependency
98
- name: sqlite3
132
+ name: rspec-rails
99
133
  requirement: !ruby/object:Gem::Requirement
100
134
  requirements:
101
- - - ">="
135
+ - - "~>"
102
136
  - !ruby/object:Gem::Version
103
- version: '0'
137
+ version: '3.0'
104
138
  type: :development
105
139
  prerelease: false
106
140
  version_requirements: !ruby/object:Gem::Requirement
107
141
  requirements:
108
- - - ">="
142
+ - - "~>"
109
143
  - !ruby/object:Gem::Version
110
- version: '0'
144
+ version: '3.0'
111
145
  - !ruby/object:Gem::Dependency
112
- name: webmock
146
+ name: sqlite3
113
147
  requirement: !ruby/object:Gem::Requirement
114
148
  requirements:
115
- - - "~>"
149
+ - - ">="
116
150
  - !ruby/object:Gem::Version
117
- version: '1.22'
151
+ version: '1.4'
118
152
  type: :development
119
153
  prerelease: false
120
154
  version_requirements: !ruby/object:Gem::Requirement
121
155
  requirements:
122
- - - "~>"
156
+ - - ">="
123
157
  - !ruby/object:Gem::Version
124
- version: '1.22'
125
- description:
158
+ version: '1.4'
159
+ description:
126
160
  email:
127
- - benedikt@userlist.io
161
+ - benedikt@userlist.com
128
162
  executables: []
129
163
  extensions: []
130
164
  extra_rdoc_files: []
131
165
  files:
166
+ - ".github/workflows/test.yml"
132
167
  - ".gitignore"
133
168
  - ".rspec"
134
169
  - ".rubocop.yml"
135
- - ".travis.yml"
136
170
  - CODE_OF_CONDUCT.md
137
171
  - Gemfile
138
172
  - Guardfile
@@ -142,22 +176,28 @@ files:
142
176
  - bin/console
143
177
  - bin/setup
144
178
  - bin/test
145
- - lib/tasks/userlist.rake
146
179
  - lib/userlist/rails.rb
147
- - lib/userlist/rails/company.rb
148
180
  - lib/userlist/rails/config.rb
181
+ - lib/userlist/rails/extensions/company.rb
182
+ - lib/userlist/rails/extensions/event.rb
183
+ - lib/userlist/rails/extensions/relationship.rb
184
+ - lib/userlist/rails/extensions/user.rb
185
+ - lib/userlist/rails/helpers.rb
149
186
  - lib/userlist/rails/importer.rb
150
187
  - lib/userlist/rails/logger.rb
151
- - lib/userlist/rails/push.rb
152
188
  - lib/userlist/rails/railtie.rb
153
- - lib/userlist/rails/user.rb
189
+ - lib/userlist/rails/tasks/userlist.rake
190
+ - lib/userlist/rails/transform.rb
191
+ - lib/userlist/rails/transforms/company.rb
192
+ - lib/userlist/rails/transforms/relationship.rb
193
+ - lib/userlist/rails/transforms/user.rb
154
194
  - lib/userlist/rails/version.rb
155
195
  - userlist-rails.gemspec
156
196
  homepage: https://github.com/userlistio/userlist-rails
157
197
  licenses:
158
198
  - MIT
159
199
  metadata: {}
160
- post_install_message:
200
+ post_install_message:
161
201
  rdoc_options: []
162
202
  require_paths:
163
203
  - lib
@@ -165,16 +205,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
165
205
  requirements:
166
206
  - - ">="
167
207
  - !ruby/object:Gem::Version
168
- version: '0'
208
+ version: '2.4'
169
209
  required_rubygems_version: !ruby/object:Gem::Requirement
170
210
  requirements:
171
211
  - - ">="
172
212
  - !ruby/object:Gem::Version
173
213
  version: '0'
174
214
  requirements: []
175
- rubyforge_project:
176
- rubygems_version: 2.7.3
177
- signing_key:
215
+ rubygems_version: 3.1.4
216
+ signing_key:
178
217
  specification_version: 4
179
- summary: Rails integration for Userlist.io
218
+ summary: Rails integration for Userlist
180
219
  test_files: []
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.5.0
7
- before_install: gem install bundler -v 1.17.1
@@ -1,42 +0,0 @@
1
- require 'active_support/concern'
2
- require 'userlist/push'
3
-
4
- module Userlist
5
- module Rails
6
- module Company
7
- extend ActiveSupport::Concern
8
-
9
- included do
10
- block = lambda do
11
- Userlist::Push.company(userlist_payload)
12
- end
13
-
14
- if respond_to?(:after_commit)
15
- after_commit(&block)
16
- elsif respond_to?(:after_save)
17
- after_save(&block)
18
- end
19
- end
20
-
21
- def userlist_identifier
22
- "#{self.class.name}-#{id}".parameterize
23
- end
24
-
25
- def userlist_properties
26
- {}
27
- end
28
-
29
- def userlist_payload
30
- {
31
- identifier: userlist_identifier,
32
- name: userlist_name,
33
- properties: userlist_properties
34
- }
35
- end
36
-
37
- def userlist_name
38
- return name if respond_to?(:name)
39
- end
40
- end
41
- end
42
- end
@@ -1,39 +0,0 @@
1
- module Userlist
2
- module Rails
3
- module Push
4
- def event(payload = {})
5
- payload[:user] ||= Userlist::Rails.current_user
6
-
7
- super(transform_payload(payload))
8
- end
9
- alias track event
10
-
11
- def user(payload = {})
12
- super(transform_payload(payload))
13
- end
14
- alias identify user
15
-
16
- private
17
-
18
- def transform_payload(payload)
19
- payload = transform_value(payload)
20
-
21
- return payload unless payload.is_a?(Hash)
22
-
23
- payload.transform_values do |value|
24
- result = transform_value(value)
25
- result = transform_payload(result) if result.is_a?(Hash)
26
- result
27
- end
28
- end
29
-
30
- def transform_value(value)
31
- if value.respond_to?(:userlist_payload)
32
- value.send(:userlist_payload)
33
- else
34
- value
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,48 +0,0 @@
1
- require 'active_support/concern'
2
- require 'userlist/push'
3
-
4
- module Userlist
5
- module Rails
6
- module User
7
- extend ActiveSupport::Concern
8
-
9
- included do
10
- block = lambda do
11
- Userlist::Push.user(userlist_payload)
12
- end
13
-
14
- if respond_to?(:after_commit)
15
- after_commit(&block)
16
- elsif respond_to?(:after_save)
17
- after_save(&block)
18
- end
19
- end
20
-
21
- def userlist_identifier
22
- "#{self.class.name}-#{id}".parameterize
23
- end
24
-
25
- def userlist_properties
26
- {}
27
- end
28
-
29
- def userlist_email
30
- return email if respond_to?(:email)
31
- end
32
-
33
- def userlist_company
34
- nil
35
- end
36
-
37
- def userlist_payload
38
- {
39
- identifier: userlist_identifier,
40
- email: userlist_email,
41
- company: userlist_company,
42
- properties: userlist_properties,
43
- signed_up_at: created_at
44
- }
45
- end
46
- end
47
- end
48
- end