userlist-rails 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/.rubocop.yml +1 -1
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +105 -9
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tasks/userlist.rake +36 -0
- data/lib/userlist/rails/company.rb +42 -0
- data/lib/userlist/rails/config.rb +33 -0
- data/lib/userlist/rails/importer.rb +36 -0
- data/lib/userlist/rails/logger.rb +34 -0
- data/lib/userlist/rails/push.rb +39 -0
- data/lib/userlist/rails/railtie.rb +49 -0
- data/lib/userlist/rails/user.rb +48 -0
- data/lib/userlist/rails/version.rb +1 -1
- data/lib/userlist/rails.rb +23 -1
- data/userlist-rails.gemspec +4 -3
- metadata +33 -8
- data/MIT-LICENSE +0 -20
- data/lib/tasks/userlist/rails_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ef228cf5cb636af878d4c252b272ad9cdb4bd6ab462ddcb9ad0cfacc71a191fb
|
4
|
+
data.tar.gz: e9577e9c3db7171665106a8b10977adeff7a05a90e6a892521667891701e9ec3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b197cf9e6078baa517cd15a6cb8ee156164f37e154378b915bf024767adcdc155ffdb4323be007de9617e6f5194ce92b2e38d314c49513056650c4e2b2d08fe
|
7
|
+
data.tar.gz: 3f2f7a0c82ae87ec3c66f8595bdd9a994d5c33ea2e806764bc29770248b93931d820ac10caeceafde4469b234e342d5c6d4465ab99f47467ae5c66c30bfb5a01
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.rubocop.yml
CHANGED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at benedikt@benediktdeicke.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
CHANGED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Userlist, Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
# Userlist::Rails
|
2
|
-
Short description and motivation.
|
3
2
|
|
4
|
-
|
5
|
-
How to use my plugin.
|
3
|
+
This gem helps with integrating [Userlist.io](http://userlist.io) into Ruby on Rails applications.
|
6
4
|
|
7
5
|
## Installation
|
6
|
+
|
8
7
|
Add this line to your application's Gemfile:
|
9
8
|
|
10
9
|
```ruby
|
@@ -12,17 +11,114 @@ gem 'userlist-rails'
|
|
12
11
|
```
|
13
12
|
|
14
13
|
And then execute:
|
15
|
-
|
16
|
-
$ bundle
|
17
|
-
```
|
14
|
+
|
15
|
+
$ bundle
|
18
16
|
|
19
17
|
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install userlist-rails
|
18
|
+
|
19
|
+
$ gem install userlist-rails
|
20
|
+
|
21
|
+
## Configuration
|
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.io/settings/push) in your Userlist.io account.
|
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-ruby](http://github.com/userlistio/userlist-ruby) gem for additional configuration options.
|
26
|
+
|
27
|
+
Configuration via environment variables:
|
28
|
+
|
29
|
+
```shell
|
30
|
+
USERLIST_PUSH_KEY=401e5c498be718c0a38b7da7f1ce5b409c56132a49246c435ee296e07bf2be39
|
31
|
+
```
|
32
|
+
|
33
|
+
Configuration via an initializer:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
# config/initializer/userlist.rb
|
37
|
+
Userlist.configure do |config|
|
38
|
+
config.push_key = '401e5c498be718c0a38b7da7f1ce5b409c56132a49246c435ee296e07bf2be39'
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
### Tracking Users
|
45
|
+
|
46
|
+
#### Sending user data automatically
|
47
|
+
|
48
|
+
By default, this gem will automatically detect your `User` model and create and update the corresponding user inside of Userlist. To customize the `identifier`, `email`, or `properties` transmitted for a user, you can overwrite the according methods in your `User` model.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class User < ApplicationRecord
|
52
|
+
def userlist_properties
|
53
|
+
{ first_name: first_name, last_name: last_name }
|
54
|
+
end
|
55
|
+
|
56
|
+
def userlist_identifier
|
57
|
+
"user-#{id}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def userlist_email
|
61
|
+
email
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
#### Sending user data manually
|
67
|
+
|
68
|
+
To manually send user data into Userlist, use the `Userlist::Push.user` method.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
Userlist::Push.event(identifier: user.id, email: user.email, properties: { first_name: user.first_name, last_name: user.last_name })
|
72
|
+
```
|
73
|
+
|
74
|
+
### Tracking Events
|
75
|
+
|
76
|
+
To track custom events use the `Userlist::Push.event` method.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
Userlist::Push.event(name: 'project_created', user: current_user, properties: { project_name: project.name })
|
22
80
|
```
|
23
81
|
|
82
|
+
It is possible to make the `user` property optional by setting it for the entire request using an `around_action` callback in your `ApplicationController`.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
class ApplicationController < ActionController::Base
|
86
|
+
around_action :setup_userlist_current_user
|
87
|
+
|
88
|
+
def setup_userlist_current_user(&block)
|
89
|
+
Userlist::Rails.with_current_user(current_user, &block)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
This simplifies the tracking call for the current request.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
Userlist::Push.event(name: 'project_created', properties: { project_name: project.name })
|
98
|
+
```
|
99
|
+
|
100
|
+
### Batch importing
|
101
|
+
|
102
|
+
You can import (and update) all your existing users and companies into Userlist by running the import rake task:
|
103
|
+
|
104
|
+
```shell
|
105
|
+
rake userlist:import
|
106
|
+
```
|
107
|
+
|
108
|
+
## Development
|
109
|
+
|
110
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
111
|
+
|
112
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
113
|
+
|
24
114
|
## Contributing
|
25
|
-
|
115
|
+
|
116
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/userlistio/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.
|
26
117
|
|
27
118
|
## License
|
119
|
+
|
28
120
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
121
|
+
|
122
|
+
## Code of Conduct
|
123
|
+
|
124
|
+
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/userlistio/userlist-rails/blob/master/CODE_OF_CONDUCT.md).
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "userlist/rails"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# rubocop:disable Metrics/BlockLength
|
2
|
+
|
3
|
+
require 'userlist/rails/importer'
|
4
|
+
|
5
|
+
namespace :userlist do
|
6
|
+
namespace :import do
|
7
|
+
desc 'Import users into Userlist.io'
|
8
|
+
task users: :environment do
|
9
|
+
if user_model = Rails.application.config.userlist.user_model
|
10
|
+
importer = Userlist::Rails::Importer.new
|
11
|
+
importer.import(user_model) do |user|
|
12
|
+
push.user(user.send(:userlist_payload))
|
13
|
+
end
|
14
|
+
else
|
15
|
+
puts 'No user model defined. Skipping import.'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Import companies into Userlist.io'
|
20
|
+
task companies: :environment do
|
21
|
+
if company_model = Rails.application.config.userlist.company_model
|
22
|
+
importer = Userlist::Rails::Importer.new
|
23
|
+
importer.import(company_model) do |company|
|
24
|
+
push.company(company.send(:userlist_payload))
|
25
|
+
end
|
26
|
+
else
|
27
|
+
puts 'No company model defined. Skipping import.'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Import users and companies into Userlist.io'
|
33
|
+
task import: ['userlist:import:users', 'userlist:import:companies']
|
34
|
+
end
|
35
|
+
|
36
|
+
# rubocop:enable Metrics/BlockLength
|
@@ -0,0 +1,42 @@
|
|
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
|
+
block = lambda do
|
11
|
+
Userlist::Push.company(userlist_payload)
|
12
|
+
end
|
13
|
+
|
14
|
+
if respond_to?(:after_commit)
|
15
|
+
after_commit(&block)
|
16
|
+
elsif respond_to?(:after_save)
|
17
|
+
after_save(&block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def userlist_identifier
|
22
|
+
"#{self.class.name}-#{id}".parameterize
|
23
|
+
end
|
24
|
+
|
25
|
+
def userlist_properties
|
26
|
+
{}
|
27
|
+
end
|
28
|
+
|
29
|
+
def userlist_payload
|
30
|
+
{
|
31
|
+
identifier: userlist_identifier,
|
32
|
+
name: userlist_name,
|
33
|
+
properties: userlist_properties
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def userlist_name
|
38
|
+
return name if respond_to?(:name)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'userlist/config'
|
2
|
+
|
3
|
+
module Userlist
|
4
|
+
module Rails
|
5
|
+
module Config
|
6
|
+
DEFAULT_CONFIGURATION = {
|
7
|
+
user_model: nil,
|
8
|
+
company_model: nil,
|
9
|
+
auto_discover: true
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
def default_config
|
13
|
+
super.merge(DEFAULT_CONFIGURATION)
|
14
|
+
end
|
15
|
+
|
16
|
+
def auto_discover
|
17
|
+
[true, 'true'].include?(super)
|
18
|
+
end
|
19
|
+
|
20
|
+
def user_model
|
21
|
+
model = super
|
22
|
+
model && model.to_s.constantize
|
23
|
+
end
|
24
|
+
|
25
|
+
def company_model
|
26
|
+
model = super
|
27
|
+
model && model.to_s.constantize
|
28
|
+
end
|
29
|
+
|
30
|
+
Userlist::Config.send(:prepend, self)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Userlist
|
2
|
+
module Rails
|
3
|
+
class Importer
|
4
|
+
def initialize(config = {})
|
5
|
+
config[:push_strategy] ||= :direct
|
6
|
+
@config = Userlist.config.merge(config)
|
7
|
+
end
|
8
|
+
|
9
|
+
def import(model, &block)
|
10
|
+
find_each(model) do |record|
|
11
|
+
print "Pushing #{model.name} #{record.id}..."
|
12
|
+
instance_exec(record, &block)
|
13
|
+
print " ✔︎\n"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :config
|
20
|
+
|
21
|
+
def push
|
22
|
+
@push ||= Userlist::Push.new(config)
|
23
|
+
end
|
24
|
+
|
25
|
+
def find_each(model, &block)
|
26
|
+
if model.respond_to?(:find_each)
|
27
|
+
model.find_each(&block)
|
28
|
+
elsif model.respond_to?(:each)
|
29
|
+
model.each(&block)
|
30
|
+
else
|
31
|
+
raise "Cannot iterate over #{model} because it doesn't implement .find_each, nor .each"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Userlist
|
2
|
+
module Rails
|
3
|
+
class Logger
|
4
|
+
include ::Logger::Severity
|
5
|
+
|
6
|
+
def initialize(logger, config = {})
|
7
|
+
@logger = logger
|
8
|
+
@config = Userlist.config.merge(config)
|
9
|
+
end
|
10
|
+
|
11
|
+
def add(severity, message = nil, progname = nil)
|
12
|
+
return true if (severity || UNKNOWN) < level
|
13
|
+
|
14
|
+
logger.add(severity, "[userlist-rails] #{message}", progname)
|
15
|
+
end
|
16
|
+
|
17
|
+
constants.each do |severity|
|
18
|
+
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
19
|
+
def #{severity.downcase}(*args) # def debug(*args)
|
20
|
+
add(#{severity}, *args) # add(DEBUG, *args)
|
21
|
+
end # end
|
22
|
+
RUBY
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
attr_reader :logger, :config
|
28
|
+
|
29
|
+
def level
|
30
|
+
@level ||= self.class.const_get(config.log_level.to_s.upcase)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Userlist
|
2
|
+
module Rails
|
3
|
+
module Push
|
4
|
+
def event(payload = {})
|
5
|
+
payload[:user] ||= Userlist::Rails.current_user
|
6
|
+
|
7
|
+
super(transform_payload(payload))
|
8
|
+
end
|
9
|
+
alias track event
|
10
|
+
|
11
|
+
def user(payload = {})
|
12
|
+
super(transform_payload(payload))
|
13
|
+
end
|
14
|
+
alias identify user
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def transform_payload(payload)
|
19
|
+
payload = transform_value(payload)
|
20
|
+
|
21
|
+
return payload unless payload.is_a?(Hash)
|
22
|
+
|
23
|
+
payload.transform_values do |value|
|
24
|
+
result = transform_value(value)
|
25
|
+
result = transform_payload(result) if result.is_a?(Hash)
|
26
|
+
result
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def transform_value(value)
|
31
|
+
if value.respond_to?(:userlist_payload)
|
32
|
+
value.send(:userlist_payload)
|
33
|
+
else
|
34
|
+
value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -1,6 +1,55 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
require 'userlist'
|
4
|
+
require 'userlist/config'
|
5
|
+
require 'userlist/rails/logger'
|
6
|
+
require 'userlist/rails/push'
|
7
|
+
require 'userlist/rails/user'
|
8
|
+
require 'userlist/rails/company'
|
9
|
+
|
1
10
|
module Userlist
|
2
11
|
module Rails
|
3
12
|
class Railtie < ::Rails::Railtie
|
13
|
+
rake_tasks do
|
14
|
+
load 'tasks/userlist.rake'
|
15
|
+
end
|
16
|
+
|
17
|
+
initializer 'userlist.config' do
|
18
|
+
config.userlist = Userlist.config
|
19
|
+
end
|
20
|
+
|
21
|
+
initializer 'userlist.logger' do
|
22
|
+
config.after_initialize do
|
23
|
+
Userlist.logger = Userlist::Rails::Logger.new(::Rails.logger, config.userlist)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
initializer 'userlist.extensions' do
|
28
|
+
Userlist::Push.send(:prepend, Userlist::Rails::Push)
|
29
|
+
end
|
30
|
+
|
31
|
+
initializer 'userlist.models' do
|
32
|
+
config.to_prepare do
|
33
|
+
userlist = ::Rails.application.config.userlist
|
34
|
+
|
35
|
+
if userlist.auto_discover
|
36
|
+
Userlist.logger.info('Automatically discovering models')
|
37
|
+
|
38
|
+
userlist.user_model ||= Userlist::Rails.detect_model('User')
|
39
|
+
userlist.company_model ||= Userlist::Rails.detect_model('Account', 'Company')
|
40
|
+
end
|
41
|
+
|
42
|
+
if user_model = userlist.user_model
|
43
|
+
Userlist.logger.info("Preparing user model #{user_model}")
|
44
|
+
user_model.send(:include, Userlist::Rails::User)
|
45
|
+
end
|
46
|
+
|
47
|
+
if company_model = userlist.company_model
|
48
|
+
Userlist.logger.info("Preparing company model #{company_model}")
|
49
|
+
company_model.send(:include, Userlist::Rails::Company)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
4
53
|
end
|
5
54
|
end
|
6
55
|
end
|
@@ -0,0 +1,48 @@
|
|
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
|
+
block = lambda do
|
11
|
+
Userlist::Push.user(userlist_payload)
|
12
|
+
end
|
13
|
+
|
14
|
+
if respond_to?(:after_commit)
|
15
|
+
after_commit(&block)
|
16
|
+
elsif respond_to?(:after_save)
|
17
|
+
after_save(&block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def userlist_identifier
|
22
|
+
"#{self.class.name}-#{id}".parameterize
|
23
|
+
end
|
24
|
+
|
25
|
+
def userlist_properties
|
26
|
+
{}
|
27
|
+
end
|
28
|
+
|
29
|
+
def userlist_email
|
30
|
+
return email if respond_to?(:email)
|
31
|
+
end
|
32
|
+
|
33
|
+
def userlist_company
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def userlist_payload
|
38
|
+
{
|
39
|
+
identifier: userlist_identifier,
|
40
|
+
email: userlist_email,
|
41
|
+
company: userlist_company,
|
42
|
+
properties: userlist_properties,
|
43
|
+
signed_up_at: created_at
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/userlist/rails.rb
CHANGED
@@ -1,7 +1,29 @@
|
|
1
|
+
require 'userlist/rails/config'
|
1
2
|
require 'userlist/rails/railtie'
|
2
3
|
|
3
4
|
module Userlist
|
4
5
|
module Rails
|
5
|
-
|
6
|
+
def self.with_current_user(user)
|
7
|
+
Thread.current[:userlist_current_user] = user
|
8
|
+
yield
|
9
|
+
ensure
|
10
|
+
Thread.current[:userlist_current_user] = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.current_user
|
14
|
+
Thread.current[:userlist_current_user]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.detect_model(*names)
|
18
|
+
names.each do |name|
|
19
|
+
begin
|
20
|
+
return name.constantize
|
21
|
+
rescue NameError
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
nil
|
27
|
+
end
|
6
28
|
end
|
7
29
|
end
|
data/userlist-rails.gemspec
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
lib = File.expand_path('
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
|
4
4
|
require 'userlist/rails/version'
|
@@ -19,8 +19,9 @@ 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.add_dependency '
|
23
|
-
spec.add_dependency '
|
22
|
+
spec.add_dependency 'activesupport', '>= 3.0'
|
23
|
+
spec.add_dependency 'railties', '>= 3.0'
|
24
|
+
spec.add_dependency 'userlist', '~> 0.2'
|
24
25
|
|
25
26
|
spec.add_development_dependency 'bundler', '~> 1.15'
|
26
27
|
spec.add_development_dependency 'rake', '~> 10.0'
|
metadata
CHANGED
@@ -1,17 +1,31 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: userlist-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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: 2018-
|
11
|
+
date: 2018-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: railties
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - ">="
|
@@ -30,14 +44,14 @@ dependencies:
|
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
47
|
+
version: '0.2'
|
34
48
|
type: :runtime
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
54
|
+
version: '0.2'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,16 +130,27 @@ extensions: []
|
|
116
130
|
extra_rdoc_files: []
|
117
131
|
files:
|
118
132
|
- ".gitignore"
|
133
|
+
- ".rspec"
|
119
134
|
- ".rubocop.yml"
|
135
|
+
- ".travis.yml"
|
136
|
+
- CODE_OF_CONDUCT.md
|
120
137
|
- Gemfile
|
121
138
|
- Guardfile
|
122
|
-
-
|
139
|
+
- LICENSE.txt
|
123
140
|
- README.md
|
124
141
|
- Rakefile
|
142
|
+
- bin/console
|
143
|
+
- bin/setup
|
125
144
|
- bin/test
|
126
|
-
- lib/tasks/userlist
|
145
|
+
- lib/tasks/userlist.rake
|
127
146
|
- lib/userlist/rails.rb
|
147
|
+
- lib/userlist/rails/company.rb
|
148
|
+
- lib/userlist/rails/config.rb
|
149
|
+
- lib/userlist/rails/importer.rb
|
150
|
+
- lib/userlist/rails/logger.rb
|
151
|
+
- lib/userlist/rails/push.rb
|
128
152
|
- lib/userlist/rails/railtie.rb
|
153
|
+
- lib/userlist/rails/user.rb
|
129
154
|
- lib/userlist/rails/version.rb
|
130
155
|
- userlist-rails.gemspec
|
131
156
|
homepage: https://github.com/userlistio/userlist-rails
|
@@ -148,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
173
|
version: '0'
|
149
174
|
requirements: []
|
150
175
|
rubyforge_project:
|
151
|
-
rubygems_version: 2.
|
176
|
+
rubygems_version: 2.7.3
|
152
177
|
signing_key:
|
153
178
|
specification_version: 4
|
154
179
|
summary: Rails integration for Userlist.io
|
data/MIT-LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright 2018 Benedikt Deicke
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|