userlist-rails 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +142 -10
- data/lib/userlist/rails.rb +40 -0
- data/lib/userlist/rails/config.rb +4 -0
- data/lib/userlist/rails/railtie.rb +3 -1
- data/lib/userlist/rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4361f8f73c39543b0250633d22c4d758ac4b43cb073d160bce946ac14390c39
|
4
|
+
data.tar.gz: 59e21c2a6579d36c4b50545f858dbb30c1a63e9e1dc7de69dacfbf87201707cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 776f506a31ac8db648d2e8fb6ccdf846abba9fe4d8c04f4b6b0143ebd5a6711719b6c627a7191016d529ed76c3c8ab880da797823d85b6846e767a89ff473e68
|
7
|
+
data.tar.gz: 1984ab2272f68b9b6dddea36cac787ec1bd278da08630b4c168fd25a3aa6fca1fead8936415f28d80c58532cc866cc9b1ea13512ad4cb4c5a202bcf84c598f11
|
data/README.md
CHANGED
@@ -47,6 +47,7 @@ In addition to the configuration options of the [userlist](http://github.com/use
|
|
47
47
|
| --------------- | ---------------------------- | -------------------------------------------------------------------------------------------------------------------- |
|
48
48
|
| `user_model` | `nil` | The user model to use. Will be automatically set when `auto_discover` is `true` |
|
49
49
|
| `company_model` | `nil` | The company model to use. Will be automatically set when `auto_discover` is `true` |
|
50
|
+
| `relationship_model` | `nil` | The relationship model to use. Will be automatically infered from the user and company models |
|
50
51
|
| `auto_discover` | `true` | The gem will try to automatically identify your `User` and `Company` models. Possible values are `true` and `false`. |
|
51
52
|
| `script_url` | `https://js.userlist.com/v1` | The script url to load the Userlist in-app messages script from. |
|
52
53
|
|
@@ -101,36 +102,167 @@ It's also possible to customize the payload sent to Userlist by passing a hash i
|
|
101
102
|
Userlist::Push.users.push(identifier: user.id, email: user.email, properties: { first_name: user.first_name, last_name: user.last_name })
|
102
103
|
```
|
103
104
|
|
105
|
+
#### Ignoring users
|
106
|
+
|
107
|
+
For cases where you don't want to send specific user to Userlist you can add a `userlist_push?` method. Whenever this method doesn't return a falsey value, this user will not be sent to Userlist. This also applies to any events or relationships this user is involved in.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
class User < ApplicationRecord
|
111
|
+
def userlist_push?
|
112
|
+
!deleted? && !guest?
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
#### Deleting users
|
118
|
+
|
104
119
|
It's also possible to delete a user from Userlist, using the `Userlist::Push.users.delete` method.
|
105
120
|
|
106
121
|
```ruby
|
107
122
|
Userlist::Push.users.delete(user)
|
108
123
|
```
|
109
124
|
|
110
|
-
### Tracking Events
|
111
125
|
|
112
|
-
|
126
|
+
### Tracking Companies
|
127
|
+
|
128
|
+
#### Sending company data automatically
|
129
|
+
|
130
|
+
By default, this gem will automatically detect your company model (like `Account`, `Company`, `Team`, `Organization`) and create, update, and delete the corresponding company inside of Userlist. To customize the `identifier`, `name`, or `properties` transmitted for a company, you can overwrite the according methods in your company model.
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
class Account < ApplicationRecord
|
134
|
+
def userlist_properties
|
135
|
+
{ projects: projects.count }
|
136
|
+
end
|
137
|
+
|
138
|
+
def userlist_identifier
|
139
|
+
"account-#{id}"
|
140
|
+
end
|
141
|
+
|
142
|
+
def userlist_name
|
143
|
+
name
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
|
149
|
+
#### Sending company data manually
|
150
|
+
|
151
|
+
To manually send company data into Userlist, use the `Userlist::Push.companies.push` method.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
Userlist::Push.companies.push(user)
|
155
|
+
```
|
156
|
+
|
157
|
+
It's also possible to customize the payload sent to Userlist by passing a hash instead of the company object.
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
Userlist::Push.companies.push(identifier: company.id, name: company.name, properties: { projects: company.projects.count })
|
161
|
+
```
|
162
|
+
|
163
|
+
|
164
|
+
#### Ignoring companies
|
165
|
+
|
166
|
+
For cases where you don't want to send specific company to Userlist you can add a `userlist_push?` method. Whenever this method doesn't return a falsey value, this company will not be sent to Userlist. This also applies to any events or relationships this company is involved in.
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
class User < ApplicationRecord
|
170
|
+
def userlist_push?
|
171
|
+
!deleted? && !guest?
|
172
|
+
end
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
#### Deleting users
|
177
|
+
|
178
|
+
It's also possible to delete a company from Userlist, using the `Userlist::Push.companies.delete` method.
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
Userlist::Push.companies.delete(company)
|
182
|
+
```
|
183
|
+
|
184
|
+
### Tracking relationships
|
185
|
+
|
186
|
+
Userlist supports n:m relationships between users and companies. This gem will try to figure out the model your application uses to describe these relationships by looking at the associations defined in your user and company models. When sending a user to Userlist, this gem will try to automatically include the user's relationships as well. This includes information about the relationships and companies this user is associated with, but not information about other users associated with any of the companies. This works the other way around as well. When sending a company, it'll try to automatically include the company's relationships, but not any information about the associated users' other companies.
|
113
187
|
|
114
188
|
```ruby
|
115
|
-
|
189
|
+
user = User.create(email: 'foo@example.com')
|
190
|
+
user.companies.create(name: 'Example, Inc.')
|
191
|
+
|
192
|
+
Userlist::Push.users.push(user)
|
193
|
+
|
194
|
+
# Payload sent to Userlist
|
195
|
+
{
|
196
|
+
identifier: 'user-1',
|
197
|
+
email: 'foo@example.com',
|
198
|
+
relationships: [
|
199
|
+
{
|
200
|
+
user: 'user-identifier',
|
201
|
+
company: {
|
202
|
+
identifier: 'company-identifier',
|
203
|
+
name: 'Example, Inc.',
|
204
|
+
}
|
205
|
+
}
|
206
|
+
]
|
207
|
+
}
|
116
208
|
```
|
117
209
|
|
118
|
-
|
210
|
+
Similar to users and events, these relationships may define a `userlist_properties` method to provide addition properties that describe the relationship.
|
119
211
|
|
120
212
|
```ruby
|
121
|
-
class
|
122
|
-
|
213
|
+
class Membership < ApplicationRecord
|
214
|
+
belongs_to :user
|
215
|
+
belongs_to :account
|
123
216
|
|
124
|
-
def
|
125
|
-
|
217
|
+
def userlist_properties
|
218
|
+
{ role: role }
|
126
219
|
end
|
127
220
|
end
|
128
221
|
```
|
129
222
|
|
130
|
-
|
223
|
+
|
224
|
+
#### Customizing relationship lookup
|
225
|
+
|
226
|
+
It's possible to customize the way this gem looks up relationships for users and companies by specifying a `userlist_relationships` method on the user and/or company model.
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
class User < ApplicationRecord
|
230
|
+
def userlist_relationships
|
231
|
+
memberships.where(role: 'owner')
|
232
|
+
end
|
233
|
+
end
|
234
|
+
```
|
235
|
+
|
236
|
+
|
237
|
+
#### Ignoring relationships
|
238
|
+
|
239
|
+
This gem automatically ignore relationship if either the user or the company is ignored. However, in some cases it might be desirable to ignore relationships even when they connect to valid objects. A typical example for this are pending invitations. To support this use case, you can provide a `userlist_push?` method. Whenever this method doesn't return a falsey value, this relationship will not be sent to Userlist.
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
class Membership < ApplicationRecord
|
243
|
+
belongs_to :user
|
244
|
+
belongs_to :account
|
245
|
+
|
246
|
+
def userlist_push?
|
247
|
+
pending?
|
248
|
+
end
|
249
|
+
end
|
250
|
+
```
|
251
|
+
|
252
|
+
#### Deleting relationships
|
253
|
+
|
254
|
+
It's also possible to delete a relationship from Userlist, using the `Userlist::Push.relationship.delete` method.
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
Userlist::Push.relationship.delete(membership)
|
258
|
+
```
|
259
|
+
|
260
|
+
### Tracking Events
|
261
|
+
|
262
|
+
To track custom events use the `Userlist::Push.events.push` method. Events can be related to a user, a company, or both.
|
131
263
|
|
132
264
|
```ruby
|
133
|
-
Userlist::Push.events.push(name: 'project_created', properties: { project_name: project.name })
|
265
|
+
Userlist::Push.events.push(name: 'project_created', user: current_user, company: current_account, properties: { project_name: project.name })
|
134
266
|
```
|
135
267
|
|
136
268
|
### Enabling in-app messages
|
data/lib/userlist/rails.rb
CHANGED
@@ -3,6 +3,14 @@ require 'userlist/rails/railtie'
|
|
3
3
|
|
4
4
|
module Userlist
|
5
5
|
module Rails
|
6
|
+
DEPRECATED_MODEL_METHODS = [
|
7
|
+
:userlist_push,
|
8
|
+
:userlist_delete,
|
9
|
+
:userlist_payload,
|
10
|
+
:userlist_company,
|
11
|
+
:userlist_user
|
12
|
+
].freeze
|
13
|
+
|
6
14
|
def self.with_current_user(user)
|
7
15
|
Thread.current[:userlist_current_user] = user
|
8
16
|
yield
|
@@ -80,5 +88,37 @@ module Userlist
|
|
80
88
|
Userlist::Push::Relationship.include(Userlist::Rails::Extensions::Relationship)
|
81
89
|
Userlist::Push::Event.include(Userlist::Rails::Extensions::Event)
|
82
90
|
end
|
91
|
+
|
92
|
+
def self.check_deprecations(type)
|
93
|
+
deprecated_methods = (type.instance_methods + type.private_instance_methods) & DEPRECATED_MODEL_METHODS
|
94
|
+
|
95
|
+
if deprecated_methods.any?
|
96
|
+
raise <<~MESSAGE
|
97
|
+
Deprecation warning for userlist-rails
|
98
|
+
|
99
|
+
Customizing the way userlist-rails works has changed.
|
100
|
+
|
101
|
+
Using the #{deprecated_methods.to_sentence} method(s) on your #{type.name} model is not supported anymore.
|
102
|
+
|
103
|
+
For details on how to customize the gem's behavior, please see https://github.com/userlist/userlist-rails or reach out to support@userlist.com
|
104
|
+
MESSAGE
|
105
|
+
end
|
106
|
+
|
107
|
+
deprecated_methods = type.private_instance_methods.grep(/userlist_/)
|
108
|
+
|
109
|
+
if deprecated_methods.any?
|
110
|
+
raise <<~MESSAGE
|
111
|
+
Deprecation warning for userlist-rails
|
112
|
+
|
113
|
+
Customizing the way userlist-rails works has changed.
|
114
|
+
|
115
|
+
Using private methods (like #{deprecated_methods.to_sentence}) on your #{type.name} model is not supported anymore. Please use public methods instead.
|
116
|
+
|
117
|
+
For details on how to customize the gem's behavior, please see https://github.com/userlist/userlist-rails or reach out to support@userlist.com
|
118
|
+
MESSAGE
|
119
|
+
end
|
120
|
+
|
121
|
+
true
|
122
|
+
end
|
83
123
|
end
|
84
124
|
end
|
@@ -34,6 +34,10 @@ module Userlist
|
|
34
34
|
(model = super) && model.is_a?(Class) ? model : model&.to_s&.constantize
|
35
35
|
end
|
36
36
|
|
37
|
+
def relationship_model
|
38
|
+
(model = super) && model.is_a?(Class) ? model : model&.to_s&.constantize
|
39
|
+
end
|
40
|
+
|
37
41
|
Userlist::Config.send(:prepend, self)
|
38
42
|
end
|
39
43
|
end
|
@@ -53,17 +53,19 @@ module Userlist
|
|
53
53
|
Userlist.logger.info('Automatically discovering models')
|
54
54
|
|
55
55
|
userlist.user_model = Userlist::Rails.detect_model('User')
|
56
|
-
userlist.company_model = Userlist::Rails.detect_model('Account', 'Company')
|
56
|
+
userlist.company_model = Userlist::Rails.detect_model('Account', 'Company', 'Team', 'Organization')
|
57
57
|
userlist.relationship_model = Userlist::Rails.detect_relationship(userlist.user_model, userlist.company_model)
|
58
58
|
end
|
59
59
|
|
60
60
|
if user_model = userlist.user_model
|
61
61
|
Userlist.logger.info("Preparing user model #{user_model}")
|
62
|
+
Userlist::Rails.check_deprecations(user_model)
|
62
63
|
Userlist::Rails.setup_callbacks(user_model, :users)
|
63
64
|
end
|
64
65
|
|
65
66
|
if company_model = userlist.company_model
|
66
67
|
Userlist.logger.info("Preparing company model #{company_model}")
|
68
|
+
Userlist::Rails.check_deprecations(company_model)
|
67
69
|
Userlist::Rails.setup_callbacks(company_model, :companies)
|
68
70
|
end
|
69
71
|
|