userlist-rails 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +22 -0
- data/.rubocop.yml +4 -2
- data/CODE_OF_CONDUCT.md +1 -1
- data/Gemfile +1 -1
- data/README.md +51 -11
- data/lib/userlist/rails.rb +55 -5
- data/lib/userlist/rails/config.rb +11 -3
- data/lib/userlist/rails/extensions/company.rb +30 -0
- data/lib/userlist/rails/extensions/event.rb +10 -3
- data/lib/userlist/rails/extensions/relationship.rb +30 -0
- data/lib/userlist/rails/extensions/user.rb +30 -0
- data/lib/userlist/rails/helpers.rb +2 -4
- data/lib/userlist/rails/railtie.rb +22 -10
- data/lib/{tasks → userlist/rails/tasks}/userlist.rake +5 -5
- data/lib/userlist/rails/transform.rb +42 -0
- data/lib/userlist/rails/transforms/company.rb +41 -0
- data/lib/userlist/rails/transforms/relationship.rb +33 -0
- data/lib/userlist/rails/transforms/user.rb +41 -0
- data/lib/userlist/rails/version.rb +1 -1
- data/userlist-rails.gemspec +6 -5
- metadata +40 -22
- data/.travis.yml +0 -7
- data/lib/userlist/rails/company.rb +0 -45
- data/lib/userlist/rails/extensions/resource.rb +0 -35
- data/lib/userlist/rails/user.rb +0 -51
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fe4b336d98e1d3015aa44aa9a261c37bf160747573d6507f44f533a14e6e4dd
|
4
|
+
data.tar.gz: 77261e5a6972ce2b0f0d0ea43e141c3d4c33011790c01a8fd32749743c35b342
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/.rubocop.yml
CHANGED
data/CODE_OF_CONDUCT.md
CHANGED
@@ -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
|
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: '
|
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
|
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
|
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.
|
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
|
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=
|
30
|
+
USERLIST_PUSH_KEY=VvB7pjDrv0V2hoaOCeZ5rIiUEPbEhSUN
|
31
|
+
USERLIST_PUSH_ID=6vPkJl44cm82y4aLBIzaOhuEHJd0Bm7b
|
31
32
|
```
|
32
33
|
|
33
34
|
Configuration via an initializer:
|
@@ -35,13 +36,23 @@ Configuration via an initializer:
|
|
35
36
|
```ruby
|
36
37
|
# config/initializer/userlist.rb
|
37
38
|
Userlist.configure do |config|
|
38
|
-
config.push_key = '
|
39
|
+
config.push_key = 'VvB7pjDrv0V2hoaOCeZ5rIiUEPbEhSUN'
|
40
|
+
config.push_id = '6vPkJl44cm82y4aLBIzaOhuEHJd0Bm7b'
|
39
41
|
end
|
40
42
|
```
|
41
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
|
+
|
42
53
|
### Disabling in development and test environments
|
43
54
|
|
44
|
-
As sending test and development data into data into Userlist isn't very
|
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`.
|
45
56
|
|
46
57
|
```ruby
|
47
58
|
# config/initializer/userlist.rb
|
@@ -50,9 +61,10 @@ Userlist.configure do |config|
|
|
50
61
|
end
|
51
62
|
```
|
52
63
|
|
53
|
-
|
54
64
|
## Usage
|
55
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
|
+
|
56
68
|
### Tracking Users
|
57
69
|
|
58
70
|
#### Sending user data automatically
|
@@ -79,6 +91,12 @@ end
|
|
79
91
|
|
80
92
|
To manually send user data into Userlist, use the `Userlist::Push.users.push` method.
|
81
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.
|
99
|
+
|
82
100
|
```ruby
|
83
101
|
Userlist::Push.users.push(identifier: user.id, email: user.email, properties: { first_name: user.first_name, last_name: user.last_name })
|
84
102
|
```
|
@@ -86,7 +104,7 @@ Userlist::Push.users.push(identifier: user.id, email: user.email, properties: {
|
|
86
104
|
It's also possible to delete a user from Userlist, using the `Userlist::Push.users.delete` method.
|
87
105
|
|
88
106
|
```ruby
|
89
|
-
Userlist::Push.users.delete(user
|
107
|
+
Userlist::Push.users.delete(user)
|
90
108
|
```
|
91
109
|
|
92
110
|
### Tracking Events
|
@@ -115,6 +133,20 @@ This simplifies the tracking call for the current request.
|
|
115
133
|
Userlist::Push.events.push(name: 'project_created', properties: { project_name: project.name })
|
116
134
|
```
|
117
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) %>
|
148
|
+
```
|
149
|
+
|
118
150
|
### Batch importing
|
119
151
|
|
120
152
|
You can import (and update) all your existing users and companies into Userlist by running the import rake task:
|
@@ -131,7 +163,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
131
163
|
|
132
164
|
## Contributing
|
133
165
|
|
134
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
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.
|
135
167
|
|
136
168
|
## License
|
137
169
|
|
@@ -139,4 +171,12 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
139
171
|
|
140
172
|
## Code of Conduct
|
141
173
|
|
142
|
-
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/
|
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/).
|
data/lib/userlist/rails.rb
CHANGED
@@ -1,11 +1,6 @@
|
|
1
1
|
require 'userlist/rails/config'
|
2
2
|
require 'userlist/rails/railtie'
|
3
3
|
|
4
|
-
require 'userlist/rails/extensions/resource'
|
5
|
-
require 'userlist/rails/extensions/event'
|
6
|
-
|
7
|
-
require 'userlist/rails/helpers'
|
8
|
-
|
9
4
|
module Userlist
|
10
5
|
module Rails
|
11
6
|
def self.with_current_user(user)
|
@@ -19,6 +14,17 @@ module Userlist
|
|
19
14
|
Thread.current[:userlist_current_user]
|
20
15
|
end
|
21
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
|
+
|
22
28
|
def self.detect_model(*names)
|
23
29
|
names.each do |name|
|
24
30
|
begin
|
@@ -30,5 +36,49 @@ module Userlist
|
|
30
36
|
|
31
37
|
nil
|
32
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
|
33
83
|
end
|
34
84
|
end
|
@@ -1,13 +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,
|
13
|
+
relationship_model: nil,
|
9
14
|
auto_discover: true,
|
10
|
-
script_url: 'https://js.userlist.com/v1'
|
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
|
11
19
|
}.freeze
|
12
20
|
|
13
21
|
def default_config
|
@@ -19,11 +27,11 @@ module Userlist
|
|
19
27
|
end
|
20
28
|
|
21
29
|
def user_model
|
22
|
-
super&.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
|
-
super&.to_s&.constantize
|
34
|
+
(model = super) && model.is_a?(Class) ? model : model&.to_s&.constantize
|
27
35
|
end
|
28
36
|
|
29
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
|
@@ -2,10 +2,17 @@ module Userlist
|
|
2
2
|
module Rails
|
3
3
|
module Extensions
|
4
4
|
module Event
|
5
|
-
|
6
|
-
payload
|
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)
|
7
9
|
|
8
|
-
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.included(base)
|
15
|
+
base.extend(ClassMethods)
|
9
16
|
end
|
10
17
|
end
|
11
18
|
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
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Userlist
|
2
2
|
module Rails
|
3
3
|
module Helpers
|
4
|
-
def userlist_script_tag(*args)
|
4
|
+
def userlist_script_tag(*args) # rubocop:disable Metrics/CyclomaticComplexity
|
5
5
|
config = Userlist.config
|
6
6
|
logger = Userlist.logger
|
7
7
|
|
@@ -14,10 +14,8 @@ module Userlist
|
|
14
14
|
options[:async] = true
|
15
15
|
|
16
16
|
if user
|
17
|
-
identifier = user.userlist_identifier
|
18
|
-
|
19
17
|
options[:data] ||= {}
|
20
|
-
options[:data][:userlist] = Userlist::Token.generate(
|
18
|
+
options[:data][:userlist] = Userlist::Token.generate(user, config)
|
21
19
|
end
|
22
20
|
|
23
21
|
script_tag = javascript_tag('window.userlist=window.userlist||function(){(userlist.q=userlist.q||[]).push(arguments)};')
|
@@ -3,10 +3,10 @@ require 'rails/railtie'
|
|
3
3
|
require 'userlist'
|
4
4
|
require 'userlist/config'
|
5
5
|
require 'userlist/rails/logger'
|
6
|
-
require 'userlist/rails/user'
|
7
|
-
require 'userlist/rails/company'
|
8
6
|
|
9
|
-
require 'userlist/rails/extensions/
|
7
|
+
require 'userlist/rails/extensions/user'
|
8
|
+
require 'userlist/rails/extensions/company'
|
9
|
+
require 'userlist/rails/extensions/relationship'
|
10
10
|
require 'userlist/rails/extensions/event'
|
11
11
|
|
12
12
|
require 'userlist/rails/helpers'
|
@@ -15,13 +15,20 @@ module Userlist
|
|
15
15
|
module Rails
|
16
16
|
class Railtie < ::Rails::Railtie
|
17
17
|
rake_tasks do
|
18
|
-
load 'tasks/userlist.rake'
|
18
|
+
load 'userlist/rails/tasks/userlist.rake'
|
19
19
|
end
|
20
20
|
|
21
21
|
initializer 'userlist.config' do
|
22
22
|
config.userlist = Userlist.config
|
23
23
|
end
|
24
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
|
+
|
25
32
|
initializer 'userlist.logger' do
|
26
33
|
config.after_initialize do
|
27
34
|
Userlist.logger = Userlist::Rails::Logger.new(::Rails.logger, config.userlist)
|
@@ -35,8 +42,7 @@ module Userlist
|
|
35
42
|
end
|
36
43
|
|
37
44
|
initializer 'userlist.extensions' do
|
38
|
-
Userlist::
|
39
|
-
Userlist::Push::Event.prepend(Userlist::Rails::Extensions::Event)
|
45
|
+
Userlist::Rails.setup_extensions
|
40
46
|
end
|
41
47
|
|
42
48
|
initializer 'userlist.models' do
|
@@ -46,18 +52,24 @@ module Userlist
|
|
46
52
|
if userlist.auto_discover
|
47
53
|
Userlist.logger.info('Automatically discovering models')
|
48
54
|
|
49
|
-
userlist.user_model
|
50
|
-
userlist.company_model
|
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)
|
51
58
|
end
|
52
59
|
|
53
60
|
if user_model = userlist.user_model
|
54
61
|
Userlist.logger.info("Preparing user model #{user_model}")
|
55
|
-
|
62
|
+
Userlist::Rails.setup_callbacks(user_model, :users)
|
56
63
|
end
|
57
64
|
|
58
65
|
if company_model = userlist.company_model
|
59
66
|
Userlist.logger.info("Preparing company model #{company_model}")
|
60
|
-
|
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)
|
61
73
|
end
|
62
74
|
end
|
63
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
|
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.
|
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
|
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.
|
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
|
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
|
data/userlist-rails.gemspec
CHANGED
@@ -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.
|
10
|
+
spec.email = ['benedikt@userlist.com']
|
11
11
|
spec.homepage = 'https://github.com/userlistio/userlist-rails'
|
12
|
-
spec.summary = 'Rails integration for Userlist
|
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,16 +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.required_ruby_version = '>= 2.
|
22
|
+
spec.required_ruby_version = '>= 2.4'
|
23
23
|
|
24
24
|
spec.add_dependency 'activesupport', '>= 5.0'
|
25
25
|
spec.add_dependency 'railties', '>= 5.0'
|
26
|
-
spec.add_dependency 'userlist', '~> 0.
|
26
|
+
spec.add_dependency 'userlist', '~> 0.5'
|
27
27
|
|
28
28
|
spec.add_development_dependency 'actionpack', '>= 5.0'
|
29
|
+
spec.add_development_dependency 'activerecord', '>= 5.0'
|
29
30
|
spec.add_development_dependency 'bundler', '>= 1.15'
|
30
31
|
spec.add_development_dependency 'rake', '~> 12.3', '>= 12.3.3'
|
31
32
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
32
33
|
spec.add_development_dependency 'rspec-rails', '~> 3.0'
|
33
|
-
spec.add_development_dependency '
|
34
|
+
spec.add_development_dependency 'sqlite3', '>= 1.4'
|
34
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.
|
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:
|
11
|
+
date: 2021-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '0.
|
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.
|
54
|
+
version: '0.5'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: actionpack
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
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'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: bundler
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,30 +143,30 @@ dependencies:
|
|
129
143
|
- !ruby/object:Gem::Version
|
130
144
|
version: '3.0'
|
131
145
|
- !ruby/object:Gem::Dependency
|
132
|
-
name:
|
146
|
+
name: sqlite3
|
133
147
|
requirement: !ruby/object:Gem::Requirement
|
134
148
|
requirements:
|
135
|
-
- - "
|
149
|
+
- - ">="
|
136
150
|
- !ruby/object:Gem::Version
|
137
|
-
version: '1.
|
151
|
+
version: '1.4'
|
138
152
|
type: :development
|
139
153
|
prerelease: false
|
140
154
|
version_requirements: !ruby/object:Gem::Requirement
|
141
155
|
requirements:
|
142
|
-
- - "
|
156
|
+
- - ">="
|
143
157
|
- !ruby/object:Gem::Version
|
144
|
-
version: '1.
|
145
|
-
description:
|
158
|
+
version: '1.4'
|
159
|
+
description:
|
146
160
|
email:
|
147
|
-
- benedikt@userlist.
|
161
|
+
- benedikt@userlist.com
|
148
162
|
executables: []
|
149
163
|
extensions: []
|
150
164
|
extra_rdoc_files: []
|
151
165
|
files:
|
166
|
+
- ".github/workflows/test.yml"
|
152
167
|
- ".gitignore"
|
153
168
|
- ".rspec"
|
154
169
|
- ".rubocop.yml"
|
155
|
-
- ".travis.yml"
|
156
170
|
- CODE_OF_CONDUCT.md
|
157
171
|
- Gemfile
|
158
172
|
- Guardfile
|
@@ -162,24 +176,28 @@ files:
|
|
162
176
|
- bin/console
|
163
177
|
- bin/setup
|
164
178
|
- bin/test
|
165
|
-
- lib/tasks/userlist.rake
|
166
179
|
- lib/userlist/rails.rb
|
167
|
-
- lib/userlist/rails/company.rb
|
168
180
|
- lib/userlist/rails/config.rb
|
181
|
+
- lib/userlist/rails/extensions/company.rb
|
169
182
|
- lib/userlist/rails/extensions/event.rb
|
170
|
-
- lib/userlist/rails/extensions/
|
183
|
+
- lib/userlist/rails/extensions/relationship.rb
|
184
|
+
- lib/userlist/rails/extensions/user.rb
|
171
185
|
- lib/userlist/rails/helpers.rb
|
172
186
|
- lib/userlist/rails/importer.rb
|
173
187
|
- lib/userlist/rails/logger.rb
|
174
188
|
- lib/userlist/rails/railtie.rb
|
175
|
-
- lib/userlist/rails/
|
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
|
176
194
|
- lib/userlist/rails/version.rb
|
177
195
|
- userlist-rails.gemspec
|
178
196
|
homepage: https://github.com/userlistio/userlist-rails
|
179
197
|
licenses:
|
180
198
|
- MIT
|
181
199
|
metadata: {}
|
182
|
-
post_install_message:
|
200
|
+
post_install_message:
|
183
201
|
rdoc_options: []
|
184
202
|
require_paths:
|
185
203
|
- lib
|
@@ -187,15 +205,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
187
205
|
requirements:
|
188
206
|
- - ">="
|
189
207
|
- !ruby/object:Gem::Version
|
190
|
-
version: '2.
|
208
|
+
version: '2.4'
|
191
209
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
192
210
|
requirements:
|
193
211
|
- - ">="
|
194
212
|
- !ruby/object:Gem::Version
|
195
213
|
version: '0'
|
196
214
|
requirements: []
|
197
|
-
rubygems_version: 3.
|
198
|
-
signing_key:
|
215
|
+
rubygems_version: 3.1.4
|
216
|
+
signing_key:
|
199
217
|
specification_version: 4
|
200
|
-
summary: Rails integration for Userlist
|
218
|
+
summary: Rails integration for Userlist
|
201
219
|
test_files: []
|
data/.travis.yml
DELETED
@@ -1,45 +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
|
-
if method = [:after_commit, :after_save].find { |m| respond_to?(m) }
|
11
|
-
public_send(method, :userlist_push, on: [:create, :update])
|
12
|
-
public_send(method, :userlist_delete, on: [:destroy])
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def userlist_identifier
|
17
|
-
"#{self.class.name}-#{id}".parameterize
|
18
|
-
end
|
19
|
-
|
20
|
-
def userlist_properties
|
21
|
-
{}
|
22
|
-
end
|
23
|
-
|
24
|
-
def userlist_payload
|
25
|
-
{
|
26
|
-
identifier: userlist_identifier,
|
27
|
-
name: userlist_name,
|
28
|
-
properties: userlist_properties
|
29
|
-
}
|
30
|
-
end
|
31
|
-
|
32
|
-
def userlist_name
|
33
|
-
return name if respond_to?(:name)
|
34
|
-
end
|
35
|
-
|
36
|
-
def userlist_push
|
37
|
-
Userlist::Push.companies.push(userlist_payload)
|
38
|
-
end
|
39
|
-
|
40
|
-
def userlist_delete
|
41
|
-
Userlist::Push.companies.delete(userlist_identifier)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/hash/transform_values' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0')
|
2
|
-
|
3
|
-
module Userlist
|
4
|
-
module Rails
|
5
|
-
module Extensions
|
6
|
-
module Resource
|
7
|
-
def initialize(payload = {})
|
8
|
-
super(transform_payload(payload))
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def transform_payload(payload)
|
14
|
-
payload = transform_value(payload)
|
15
|
-
|
16
|
-
return payload unless payload.is_a?(Hash)
|
17
|
-
|
18
|
-
payload.transform_values do |value|
|
19
|
-
result = transform_value(value)
|
20
|
-
result = transform_payload(result) if result.is_a?(Hash)
|
21
|
-
result
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def transform_value(value)
|
26
|
-
if value.respond_to?(:userlist_payload)
|
27
|
-
value.send(:userlist_payload)
|
28
|
-
else
|
29
|
-
value
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/lib/userlist/rails/user.rb
DELETED
@@ -1,51 +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
|
-
if method = [:after_commit, :after_save].find { |m| respond_to?(m) }
|
11
|
-
public_send(method, :userlist_push, on: [:create, :update])
|
12
|
-
public_send(method, :userlist_delete, on: [:destroy])
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def userlist_identifier
|
17
|
-
"#{self.class.name}-#{id}".parameterize
|
18
|
-
end
|
19
|
-
|
20
|
-
def userlist_properties
|
21
|
-
{}
|
22
|
-
end
|
23
|
-
|
24
|
-
def userlist_email
|
25
|
-
return email if respond_to?(:email)
|
26
|
-
end
|
27
|
-
|
28
|
-
def userlist_company
|
29
|
-
nil
|
30
|
-
end
|
31
|
-
|
32
|
-
def userlist_payload
|
33
|
-
{
|
34
|
-
identifier: userlist_identifier,
|
35
|
-
email: userlist_email,
|
36
|
-
company: userlist_company,
|
37
|
-
properties: userlist_properties,
|
38
|
-
signed_up_at: created_at
|
39
|
-
}
|
40
|
-
end
|
41
|
-
|
42
|
-
def userlist_push
|
43
|
-
Userlist::Push.users.push(userlist_payload)
|
44
|
-
end
|
45
|
-
|
46
|
-
def userlist_delete
|
47
|
-
Userlist::Push.users.delete(userlist_identifier)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|