userlist-rails 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,15 @@ 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
35
+ end
36
+
37
+ def relationship_model
38
+ (model = super) && model.is_a?(Class) ? model : model&.to_s&.constantize
27
39
  end
28
40
 
29
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
@@ -2,10 +2,17 @@ module Userlist
2
2
  module Rails
3
3
  module Extensions
4
4
  module Event
5
- def initialize(payload)
6
- payload[:user] ||= Userlist::Rails.current_user
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
- super
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(identifier)
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/resource'
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::Push::Resource.prepend(Userlist::Rails::Extensions::Resource)
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,26 @@ module Userlist
46
52
  if userlist.auto_discover
47
53
  Userlist.logger.info('Automatically discovering models')
48
54
 
49
- userlist.user_model ||= Userlist::Rails.detect_model('User')
50
- 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', 'Team', 'Organization')
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
- user_model.send(:include, Userlist::Rails::User)
62
+ Userlist::Rails.check_deprecations(user_model)
63
+ Userlist::Rails.setup_callbacks(user_model, :users)
56
64
  end
57
65
 
58
66
  if company_model = userlist.company_model
59
67
  Userlist.logger.info("Preparing company model #{company_model}")
60
- company_model.send(:include, Userlist::Rails::Company)
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)
61
75
  end
62
76
  end
63
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.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
+ model.try("userlist_#{name}") || public_send("default_#{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?(:userlist_push?) || model.userlist_push?)
31
+ end
32
+
33
+ def delete?
34
+ (!model.respond_to?(:userlist_delete?) || model.userlist_delete?)
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :model, :config
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,55 @@
1
+ require 'userlist/rails/transform'
2
+ require 'userlist/rails/transforms/has_relationships'
3
+
4
+ module Userlist
5
+ module Rails
6
+ module Transforms
7
+ class Company < Userlist::Rails::Transform
8
+ include HasRelationships
9
+
10
+ def self.attributes
11
+ @attributes ||= [
12
+ :identifier,
13
+ :properties,
14
+ :relationships,
15
+ :name,
16
+ :signed_up_at
17
+ ]
18
+ end
19
+
20
+ def default_identifier
21
+ "#{model.class.name}-#{model.id}".parameterize
22
+ end
23
+
24
+ def default_properties
25
+ {}
26
+ end
27
+
28
+ def default_name
29
+ model.try(:name)
30
+ end
31
+
32
+ def default_signed_up_at
33
+ model.try(:created_at)
34
+ end
35
+
36
+ private
37
+
38
+ def build_relationship(record)
39
+ {
40
+ user: record,
41
+ company: model
42
+ }
43
+ end
44
+
45
+ def relationship_from
46
+ config.company_model
47
+ end
48
+
49
+ def relationship_to
50
+ config.user_model
51
+ end
52
+ end
53
+ end
54
+ end
55
+ 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
@@ -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 default_properties
16
+ {}
17
+ end
18
+
19
+ def default_user
20
+ user_method = Userlist::Rails.find_reflection(config.relationship_model, config.user_model)&.name
21
+
22
+ user_method && model.try(user_method)
23
+ end
24
+
25
+ def default_company
26
+ company_method = Userlist::Rails.find_reflection(config.relationship_model, config.company_model)&.name
27
+
28
+ company_method && model.try(company_method)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end