userlist-rails 0.5.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c07d01a265094da4cbd48cf116bb6f883e22a2d3c4f6f70f36866ee02081f28
4
- data.tar.gz: 2f6c5bf68403019e08188ce1cd7aeea7bda6233634633d7025161ffd54dee6f2
3
+ metadata.gz: 1541bb4907f73b3f85ffd2a97ca5d3040cb5128ee4dfbf6a8c5dfc1bf05c5afe
4
+ data.tar.gz: 4e0212563099467fa99ea690fde210dd947ec89995f095e95a7df72b6f84171a
5
5
  SHA512:
6
- metadata.gz: a009f824dbc922c3dca025e0fccc6e162d19fa23f87b15d6bfd65747a155e1307666a62c04b4266778f631c6a79c0167dfc19ce490ecc91f36c5d4c8d811de94
7
- data.tar.gz: 950db541f0a4715430319f116eebd90a1fdca6dbf4cadb2d15d75e384b6fe24796dc3cd43c55eb26dae2fe3f680e968b8eaedf9ee70cb55f5d44267070dd6fc0
6
+ metadata.gz: 83520dc930b013536d4a3278326ee07f37fdae4a303e0ea2d935ca38d168358eb2d9011eeb08902850ac88b4ac0ae6f337c3133826a1eeeaa6ca0d0740ca5c3a
7
+ data.tar.gz: eedce9ed77e9464da3da6c6f00306b252c69bd39ac95cc2cbf43247a852c9a29d763198e4ad69fcc179a10008e079d9889d953a41188131c837550a93132aec5
data/README.md CHANGED
@@ -151,7 +151,7 @@ end
151
151
  To manually send company data into Userlist, use the `Userlist::Push.companies.push` method.
152
152
 
153
153
  ```ruby
154
- Userlist::Push.companies.push(user)
154
+ Userlist::Push.companies.push(company)
155
155
  ```
156
156
 
157
157
  It's also possible to customize the payload sent to Userlist by passing a hash instead of the company object.
@@ -166,14 +166,14 @@ Userlist::Push.companies.push(identifier: company.id, name: company.name, proper
166
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
167
 
168
168
  ```ruby
169
- class User < ApplicationRecord
169
+ class Account < ApplicationRecord
170
170
  def userlist_push?
171
- !deleted? && !guest?
171
+ !deleted? && active_subscription?
172
172
  end
173
173
  end
174
174
  ```
175
175
 
176
- #### Deleting users
176
+ #### Deleting companies
177
177
 
178
178
  It's also possible to delete a company from Userlist, using the `Userlist::Push.companies.delete` method.
179
179
 
@@ -233,6 +233,20 @@ class User < ApplicationRecord
233
233
  end
234
234
  ```
235
235
 
236
+ If you don't have a dedicated model describing the relationship, you can return a hash including both the user and the company model.
237
+
238
+ ```ruby
239
+ class User < ApplicationRecord
240
+ def userlist_relationships
241
+ [
242
+ {
243
+ user: self,
244
+ company: account
245
+ }
246
+ ]
247
+ end
248
+ end
249
+ ```
236
250
 
237
251
  #### Ignoring relationships
238
252
 
@@ -59,6 +59,12 @@ module Userlist
59
59
  from.reflect_on_all_associations.find { |r| r.class_name == to.to_s }
60
60
  end
61
61
 
62
+ def self.find_association_between(from, to)
63
+ return unless association = Userlist::Rails.find_reflection(from, to)
64
+
65
+ association.through_reflection || association
66
+ end
67
+
62
68
  def self.setup_callbacks(model, scope)
63
69
  return if model.instance_variable_get(:@userlist_callbacks_registered)
64
70
 
@@ -11,7 +11,7 @@ module Userlist
11
11
  end
12
12
 
13
13
  def [](name)
14
- public_send(name) if key?(name)
14
+ model.try("userlist_#{name}") || public_send("default_#{name}") if key?(name)
15
15
  end
16
16
 
17
17
  def key?(name)
@@ -1,9 +1,12 @@
1
1
  require 'userlist/rails/transform'
2
+ require 'userlist/rails/transforms/has_relationships'
2
3
 
3
4
  module Userlist
4
5
  module Rails
5
6
  module Transforms
6
7
  class Company < Userlist::Rails::Transform
8
+ include HasRelationships
9
+
7
10
  def self.attributes
8
11
  @attributes ||= [
9
12
  :identifier,
@@ -14,26 +17,37 @@ module Userlist
14
17
  ]
15
18
  end
16
19
 
17
- def identifier
18
- model.try(:userlist_identifier) || "#{model.class.name}-#{model.id}".parameterize
20
+ def default_identifier
21
+ "#{model.class.name}-#{model.id}".parameterize
19
22
  end
20
23
 
21
- def properties
22
- model.try(:userlist_properties) || {}
24
+ def default_properties
25
+ {}
23
26
  end
24
27
 
25
- def relationships
26
- relationships_method = Userlist::Rails.find_reflection(config.company_model, config.relationship_model)&.name
28
+ def default_name
29
+ model.try(:name)
30
+ end
27
31
 
28
- model.try(:userlist_relationships) || (relationships_method && model.try(relationships_method))
32
+ def default_signed_up_at
33
+ model.try(:created_at)
29
34
  end
30
35
 
31
- def name
32
- model.try(:userlist_name) || model.try(:name)
36
+ private
37
+
38
+ def build_relationship(record)
39
+ {
40
+ user: record,
41
+ company: model
42
+ }
33
43
  end
34
44
 
35
- def signed_up_at
36
- model.try(:created_at)
45
+ def relationship_from
46
+ config.company_model
47
+ end
48
+
49
+ def relationship_to
50
+ config.user_model
37
51
  end
38
52
  end
39
53
  end
@@ -0,0 +1,35 @@
1
+ require 'userlist/rails/transform'
2
+
3
+ module Userlist
4
+ module Rails
5
+ module Transforms
6
+ module HasRelationships
7
+ def default_relationships
8
+ return unless association = Userlist::Rails.find_association_between(relationship_from, relationship_to)
9
+
10
+ records = model.try(association.name)
11
+
12
+ if association.klass == config.relationship_model
13
+ records
14
+ elsif association.klass == relationship_to
15
+ Array.wrap(records).map { |record| build_relationship(record) }
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def build_relationship(_record)
22
+ raise NotImplementedError
23
+ end
24
+
25
+ def relationship_to
26
+ raise NotImplementedError
27
+ end
28
+
29
+ def relationship_from
30
+ raise NotImplementedError
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -12,20 +12,20 @@ module Userlist
12
12
  ]
13
13
  end
14
14
 
15
- def properties
16
- model.try(:userlist_properties) || {}
15
+ def default_properties
16
+ {}
17
17
  end
18
18
 
19
- def user
19
+ def default_user
20
20
  user_method = Userlist::Rails.find_reflection(config.relationship_model, config.user_model)&.name
21
21
 
22
- model.try(:userlist_user) || (user_method && model.try(user_method))
22
+ user_method && model.try(user_method)
23
23
  end
24
24
 
25
- def company
25
+ def default_company
26
26
  company_method = Userlist::Rails.find_reflection(config.relationship_model, config.company_model)&.name
27
27
 
28
- model.try(:userlist_company) || (company_method && model.try(company_method))
28
+ company_method && model.try(company_method)
29
29
  end
30
30
  end
31
31
  end
@@ -1,9 +1,12 @@
1
1
  require 'userlist/rails/transform'
2
+ require 'userlist/rails/transforms/has_relationships'
2
3
 
3
4
  module Userlist
4
5
  module Rails
5
6
  module Transforms
6
7
  class User < Userlist::Rails::Transform
8
+ include HasRelationships
9
+
7
10
  def self.attributes
8
11
  @attributes ||= [
9
12
  :identifier,
@@ -14,26 +17,37 @@ module Userlist
14
17
  ]
15
18
  end
16
19
 
17
- def identifier
18
- model.try(:userlist_identifier) || "#{model.class.name}-#{model.id}".parameterize
20
+ def default_identifier
21
+ "#{model.class.name}-#{model.id}".parameterize
19
22
  end
20
23
 
21
- def properties
22
- model.try(:userlist_properties) || {}
24
+ def default_properties
25
+ {}
23
26
  end
24
27
 
25
- def relationships
26
- relationships_method = Userlist::Rails.find_reflection(config.user_model, config.relationship_model)&.name
28
+ def default_email
29
+ model.try(:email)
30
+ end
27
31
 
28
- model.try(:userlist_relationships) || (relationships_method && model.try(relationships_method))
32
+ def default_signed_up_at
33
+ model.try(:created_at)
29
34
  end
30
35
 
31
- def email
32
- model.try(:userlist_email) || model.try(:email)
36
+ private
37
+
38
+ def build_relationship(record)
39
+ {
40
+ user: model,
41
+ company: record
42
+ }
33
43
  end
34
44
 
35
- def signed_up_at
36
- model.try(:created_at)
45
+ def relationship_from
46
+ config.user_model
47
+ end
48
+
49
+ def relationship_to
50
+ config.company_model
37
51
  end
38
52
  end
39
53
  end
@@ -1,5 +1,5 @@
1
1
  module Userlist
2
2
  module Rails
3
- VERSION = '0.5.3'.freeze
3
+ VERSION = '0.6.0'.freeze
4
4
  end
5
5
  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.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benedikt Deicke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-15 00:00:00.000000000 Z
11
+ date: 2021-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -189,6 +189,7 @@ files:
189
189
  - lib/userlist/rails/tasks/userlist.rake
190
190
  - lib/userlist/rails/transform.rb
191
191
  - lib/userlist/rails/transforms/company.rb
192
+ - lib/userlist/rails/transforms/has_relationships.rb
192
193
  - lib/userlist/rails/transforms/relationship.rb
193
194
  - lib/userlist/rails/transforms/user.rb
194
195
  - lib/userlist/rails/version.rb