beaconable 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +16 -17
- data/README.md +60 -0
- data/lib/beaconable/base_beacon.rb +2 -2
- data/lib/beaconable/version.rb +1 -1
- data/lib/beaconable.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8bdeff81b271551aee7e8f733efc822fbbecd6fc1770f8f4e1a991bd2555efda
|
4
|
+
data.tar.gz: 3145a97b3a02ae7d11f12c636c2c94924a7d86c44adc103433547c91ed6c15e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 372d885268948c09f5edeccccec24ebf36e7a3cb4200415ddfc930c00185e6b6a3427ae6959554ad1a45fca846ec69d97694b4f2c314355f0e6c381171bb0283
|
7
|
+
data.tar.gz: c210f9ba73d61fe5389927166069cd9e238fa9fd630c1ebaa59d10404d9933c4d60791942d524157827a576bbf045600fb8967612ae356d1874004878a8c47b8
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
beaconable (0.3.
|
4
|
+
beaconable (0.3.4)
|
5
5
|
activerecord (>= 4.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (6.
|
11
|
-
activesupport (= 6.
|
12
|
-
activerecord (6.
|
13
|
-
activemodel (= 6.
|
14
|
-
activesupport (= 6.
|
15
|
-
activesupport (6.
|
10
|
+
activemodel (6.1.4.6)
|
11
|
+
activesupport (= 6.1.4.6)
|
12
|
+
activerecord (6.1.4.6)
|
13
|
+
activemodel (= 6.1.4.6)
|
14
|
+
activesupport (= 6.1.4.6)
|
15
|
+
activesupport (6.1.4.6)
|
16
16
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
-
i18n (>=
|
18
|
-
minitest (
|
19
|
-
tzinfo (~>
|
20
|
-
zeitwerk (~> 2.
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
zeitwerk (~> 2.3)
|
21
21
|
ansi (1.5.0)
|
22
22
|
ast (2.4.0)
|
23
23
|
builder (3.2.3)
|
24
24
|
byebug (10.0.2)
|
25
|
-
concurrent-ruby (1.1.
|
26
|
-
i18n (1.
|
25
|
+
concurrent-ruby (1.1.9)
|
26
|
+
i18n (1.10.0)
|
27
27
|
concurrent-ruby (~> 1.0)
|
28
28
|
jaro_winkler (1.5.3)
|
29
29
|
minitest (5.11.3)
|
@@ -46,11 +46,10 @@ GEM
|
|
46
46
|
unicode-display_width (>= 1.4.0, < 1.7)
|
47
47
|
ruby-progressbar (1.10.1)
|
48
48
|
sqlite3 (1.4.1)
|
49
|
-
|
50
|
-
|
51
|
-
thread_safe (~> 0.1)
|
49
|
+
tzinfo (2.0.4)
|
50
|
+
concurrent-ruby (~> 1.0)
|
52
51
|
unicode-display_width (1.6.0)
|
53
|
-
zeitwerk (2.
|
52
|
+
zeitwerk (2.5.4)
|
54
53
|
|
55
54
|
PLATFORMS
|
56
55
|
ruby
|
data/README.md
CHANGED
@@ -58,6 +58,66 @@ class UserBeacon < Beaconable::BaseBeacon
|
|
58
58
|
end
|
59
59
|
```
|
60
60
|
|
61
|
+
### Avoid firing a beacon
|
62
|
+
|
63
|
+
You can skip beacon calls if you pass true to the method `#skip_beacon`. I.E:
|
64
|
+
```
|
65
|
+
...
|
66
|
+
user.update(user_params)
|
67
|
+
user.skip_beacon = true
|
68
|
+
user.save # The user beacon won't be fired.
|
69
|
+
```
|
70
|
+
|
71
|
+
### Beacon metadata
|
72
|
+
|
73
|
+
You can pass `beacon_metadata` to the `object` that will be available on the **Beacon**.
|
74
|
+
|
75
|
+
Some uses might be:
|
76
|
+
|
77
|
+
- to determine whether a certain action should be performed or not. For example when creating users in batch actions or in through the console you might want to skip just the welcome email but still perform all the other side effects associated with the user creation.
|
78
|
+
- to pass information that is generated / available in memory, will not be persisted in the model but is relevant in the side effects. For example, if you want to implement your own event logging system you could pass the current user id from the controller action to the beacon where you are going to create the Event.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
User.create(
|
82
|
+
email: "new_user@email.com",
|
83
|
+
beacon_metadata: {
|
84
|
+
skip_welcome_user_job: true,
|
85
|
+
triggered_by: "admin@myapp.com"
|
86
|
+
}
|
87
|
+
)
|
88
|
+
|
89
|
+
# app/beacons/user_beacon.rb
|
90
|
+
class UserBeacon < Beaconable::BaseBeacon
|
91
|
+
alias user object
|
92
|
+
alias user_was object_was
|
93
|
+
|
94
|
+
def call
|
95
|
+
WelcomeUserJob.perform_later(self.id) if should_perform_welcome_user_job?
|
96
|
+
UpdateExternalServiceJob.perform_later(self.id) if field_changed? :email
|
97
|
+
Event.create do |event|
|
98
|
+
event.content = UserSerializer.new(user).event_content
|
99
|
+
event.ocurred_at = user.updated_at
|
100
|
+
if beacon_metadata.present?
|
101
|
+
event.triggered_by = beacon_metadata.dig(:triggered_by)
|
102
|
+
event.source = beacon_metadata.dig(:source)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def should_perform_welcome_user_job?
|
110
|
+
new_entry? && !skip_welcome_user_job?
|
111
|
+
end
|
112
|
+
|
113
|
+
def skip_welcome_user_job?
|
114
|
+
beacon_metadata[:skip_welcome_user_job] if beacon_metadata.present?
|
115
|
+
end
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
**Important**: once the beacon has been _fired_ the `beacon_metadata` will be cleared.
|
120
|
+
|
61
121
|
## Development
|
62
122
|
|
63
123
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
module Beaconable
|
4
4
|
class BaseBeacon
|
5
|
-
attr_reader :object, :object_was
|
5
|
+
attr_reader :object, :object_was, :beacon_metadata
|
6
6
|
|
7
7
|
def initialize(object, object_was)
|
8
8
|
@object = object
|
9
9
|
@object_was = object_was
|
10
|
+
@beacon_metadata = object.beacon_metadata
|
10
11
|
end
|
11
12
|
|
12
13
|
def field_changed(field)
|
@@ -46,6 +47,5 @@ module Beaconable
|
|
46
47
|
def new_entry?
|
47
48
|
object_was.created_at.nil?
|
48
49
|
end
|
49
|
-
|
50
50
|
end
|
51
51
|
end
|
data/lib/beaconable/version.rb
CHANGED
data/lib/beaconable.rb
CHANGED
@@ -8,6 +8,7 @@ require 'active_record'
|
|
8
8
|
module Beaconable
|
9
9
|
extend ActiveSupport::Concern
|
10
10
|
included do
|
11
|
+
attr_accessor :beacon_metadata
|
11
12
|
attr_accessor :skip_beacon
|
12
13
|
|
13
14
|
before_save :save_for_beacon, unless: :skip_beacon
|
@@ -25,5 +26,6 @@ module Beaconable
|
|
25
26
|
def fire_beacon
|
26
27
|
"#{self.class.name}Beacon".constantize.new(self, @object_was).call
|
27
28
|
@object_was = nil
|
29
|
+
self.beacon_metadata = nil
|
28
30
|
end
|
29
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: beaconable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerardo Raiden
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|