userlist-rails 0.2.1 → 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/.github/workflows/test.yml +22 -0
- data/.rubocop.yml +5 -6
- data/CODE_OF_CONDUCT.md +1 -1
- data/Gemfile +1 -1
- data/README.md +201 -23
- data/lib/userlist/rails.rb +95 -0
- data/lib/userlist/rails/config.rb +16 -5
- data/lib/userlist/rails/extensions/company.rb +30 -0
- data/lib/userlist/rails/extensions/event.rb +20 -0
- data/lib/userlist/rails/extensions/relationship.rb +30 -0
- data/lib/userlist/rails/extensions/user.rb +30 -0
- data/lib/userlist/rails/helpers.rb +31 -0
- data/lib/userlist/rails/railtie.rb +34 -9
- 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 +13 -9
- metadata +75 -35
- data/.travis.yml +0 -7
- data/lib/userlist/rails/company.rb +0 -39
- data/lib/userlist/rails/push.rb +0 -39
- data/lib/userlist/rails/user.rb +0 -45
@@ -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
|
-
|
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,15 @@ 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
|
-
|
34
|
+
(model = super) && model.is_a?(Class) ? model : model&.to_s&.constantize
|
35
|
+
end
|
36
|
+
|
37
|
+
def relationship_model
|
38
|
+
(model = super) && model.is_a?(Class) ? model : model&.to_s&.constantize
|
28
39
|
end
|
29
40
|
|
30
41
|
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
|
-
|
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::
|
45
|
+
Userlist::Rails.setup_extensions
|
29
46
|
end
|
30
47
|
|
31
48
|
initializer 'userlist.models' do
|
@@ -35,18 +52,26 @@ module Userlist
|
|
35
52
|
if userlist.auto_discover
|
36
53
|
Userlist.logger.info('Automatically discovering models')
|
37
54
|
|
38
|
-
userlist.user_model
|
39
|
-
userlist.company_model
|
55
|
+
userlist.user_model = Userlist::Rails.detect_model('User')
|
56
|
+
userlist.company_model = Userlist::Rails.detect_model('Account', 'Company', 'Team', 'Organization')
|
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
|
-
|
62
|
+
Userlist::Rails.check_deprecations(user_model)
|
63
|
+
Userlist::Rails.setup_callbacks(user_model, :users)
|
45
64
|
end
|
46
65
|
|
47
66
|
if company_model = userlist.company_model
|
48
67
|
Userlist.logger.info("Preparing company model #{company_model}")
|
49
|
-
|
68
|
+
Userlist::Rails.check_deprecations(company_model)
|
69
|
+
Userlist::Rails.setup_callbacks(company_model, :companies)
|
70
|
+
end
|
71
|
+
|
72
|
+
if relationship_model = userlist.relationship_model
|
73
|
+
Userlist.logger.info("Preparing relationship model #{relationship_model}")
|
74
|
+
Userlist::Rails.setup_callbacks(relationship_model, :relationships)
|
50
75
|
end
|
51
76
|
end
|
52
77
|
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
|