beaconable 0.3.1 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bec79a862ecc4920422fa44d559651bf347e777851475e52477c42ef33873839
4
- data.tar.gz: a0d5adad3dd5446597f72f74a8d04bb327317f907037312dc15b93f1d7159092
3
+ metadata.gz: 8bdeff81b271551aee7e8f733efc822fbbecd6fc1770f8f4e1a991bd2555efda
4
+ data.tar.gz: 3145a97b3a02ae7d11f12c636c2c94924a7d86c44adc103433547c91ed6c15e8
5
5
  SHA512:
6
- metadata.gz: 7c048d0131bcc789c886077b1e6dc3d1a4a198760da76ae0f881de26f3016638553c167507d1232e4be125aa445d4521d3cb3eeb97cbc9cdbd07634fa8c1530b
7
- data.tar.gz: 93c58a920aa1d8b3ed2d80cc34460ccb8ee576489589cfb5f79289fa6de2f4a648ea6ea86f304834c9d41dec0201dcc51b8d66cd378776cf89b8758a28b21a99
6
+ metadata.gz: 372d885268948c09f5edeccccec24ebf36e7a3cb4200415ddfc930c00185e6b6a3427ae6959554ad1a45fca846ec69d97694b4f2c314355f0e6c381171bb0283
7
+ data.tar.gz: c210f9ba73d61fe5389927166069cd9e238fa9fd630c1ebaa59d10404d9933c4d60791942d524157827a576bbf045600fb8967612ae356d1874004878a8c47b8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## 0.3.4 (2022-03-14)
2
+
3
+ ### Improvements
4
+
5
+ - Add `#beacon_metadata` & `#beacon_metadata=` method to included classes
6
+
7
+ ## 0.3.3 (2020-11-02)
8
+
9
+ - Fixes a bug that causes an error when a beaconable was touch by an association without changes
10
+
11
+ ### Improvements
12
+ - Add `#skip_beacon` method to included clases
13
+
14
+ ## 0.3.2 (2020-10-07)
15
+
16
+ ### Improvements
17
+ - Add `#destroyed_entry?` method
18
+
1
19
  ## 0.3.1 (2020-09-16)
2
20
 
3
21
  - Fixes #fire_beacon not to use dirty attributes to check changes
data/Gemfile.lock CHANGED
@@ -1,29 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- beaconable (0.3.1)
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.0.2)
11
- activesupport (= 6.0.2)
12
- activerecord (6.0.2)
13
- activemodel (= 6.0.2)
14
- activesupport (= 6.0.2)
15
- activesupport (6.0.2)
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 (>= 0.7, < 2)
18
- minitest (~> 5.1)
19
- tzinfo (~> 1.1)
20
- zeitwerk (~> 2.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.6)
26
- i18n (1.8.2)
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)
@@ -36,7 +36,7 @@ GEM
36
36
  parser (2.6.3.0)
37
37
  ast (~> 2.4.0)
38
38
  rainbow (3.0.0)
39
- rake (10.5.0)
39
+ rake (13.0.1)
40
40
  rubocop (0.74.0)
41
41
  jaro_winkler (~> 1.5.1)
42
42
  parallel (~> 1.10)
@@ -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
- thread_safe (0.3.6)
50
- tzinfo (1.2.7)
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.2.2)
52
+ zeitwerk (2.5.4)
54
53
 
55
54
  PLATFORMS
56
55
  ruby
@@ -61,7 +60,7 @@ DEPENDENCIES
61
60
  byebug (~> 10.0)
62
61
  minitest (~> 5.0)
63
62
  minitest-reporters (~> 1.3)
64
- rake (~> 10.0)
63
+ rake (~> 13.0)
65
64
  rubocop (~> 0.74.0)
66
65
  sqlite3 (~> 1.3)
67
66
 
data/README.md CHANGED
@@ -28,6 +28,7 @@ When you include Beaconable in your model it will fire your Beacon everytime aft
28
28
  - field_changed?(:field_name) : It allows you to check if a specific field was modified.
29
29
  - any_field_changed?(:field_name, :other_field_name) : It allows you to check if any of multiple fields was modified.
30
30
  - new_entry? : Returns true if the item saved is new
31
+ - destroyed_entry? : Returns true if the item has been destroyed
31
32
 
32
33
  You can also used the following chained methods
33
34
  - field_change(:field_name).from('first_alternative', 'n_alternative').to('first_alternative_for_to', 'second_alternative_for_to', 'n_alternative_for_toq')
@@ -57,6 +58,66 @@ class UserBeacon < Beaconable::BaseBeacon
57
58
  end
58
59
  ```
59
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
+
60
121
  ## Development
61
122
 
62
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.
data/beaconable.gemspec CHANGED
@@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
35
35
  spec.add_development_dependency 'bundler', '~> 1.16'
36
36
  spec.add_development_dependency 'minitest', '~> 5.0'
37
37
  spec.add_development_dependency 'minitest-reporters', '~> 1.3'
38
- spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'rake', '~> 13.0'
39
39
  spec.add_development_dependency 'rubocop', '~> 0.74.0'
40
40
  spec.add_development_dependency 'sqlite3', '~> 1.3'
41
41
  spec.add_development_dependency 'byebug', '~> 10.0'
@@ -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)
@@ -28,6 +29,10 @@ module Beaconable
28
29
 
29
30
  private
30
31
 
32
+ def destroyed_entry?
33
+ object.destroyed?
34
+ end
35
+
31
36
  def field_changed?(field)
32
37
  object.send(field) != object_was.send(field)
33
38
  end
@@ -42,6 +47,5 @@ module Beaconable
42
47
  def new_entry?
43
48
  object_was.created_at.nil?
44
49
  end
45
-
46
50
  end
47
51
  end
@@ -1,3 +1,3 @@
1
1
  module Beaconable
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.4"
3
3
  end
data/lib/beaconable.rb CHANGED
@@ -8,8 +8,13 @@ require 'active_record'
8
8
  module Beaconable
9
9
  extend ActiveSupport::Concern
10
10
  included do
11
- before_save :save_for_beacon
12
- after_commit :fire_beacon
11
+ attr_accessor :beacon_metadata
12
+ attr_accessor :skip_beacon
13
+
14
+ before_save :save_for_beacon, unless: :skip_beacon
15
+ before_destroy :save_for_beacon, unless: :skip_beacon
16
+ after_touch :save_for_beacon, unless: :skip_beacon
17
+ after_commit :fire_beacon, unless: :skip_beacon
13
18
  end
14
19
 
15
20
  private
@@ -21,5 +26,6 @@ module Beaconable
21
26
  def fire_beacon
22
27
  "#{self.class.name}Beacon".constantize.new(self, @object_was).call
23
28
  @object_was = nil
29
+ self.beacon_metadata = nil
24
30
  end
25
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.1
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: 2020-09-16 00:00:00.000000000 Z
11
+ date: 2022-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: '13.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: '13.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rubocop
85
85
  requirement: !ruby/object:Gem::Requirement