noticed 1.2.15 → 1.2.16

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: 69785568f3cfb7b464a3dfbb7ce8bd0813acbdcd3f96059122c2a9b24ade12d2
4
- data.tar.gz: 3c9464383b48f2df3605b7aa1062519e867f2ebfae7dde21ccc25ac69afab258
3
+ metadata.gz: 3b37e7d17da790342493c2ffc575cc5d013ac156b7ac600fb0022c79bcb034de
4
+ data.tar.gz: 87fc46cbf6829e174b0dc6df1751187965e645f7461848469e48a2ea7bcb6a46
5
5
  SHA512:
6
- metadata.gz: 539e60472bad43dd44239ecbcbd79ad9dff86cb8105d7923657e62ffa6b1fe9a3ef6ced17b823597e907645ec5837985aaee7cc7da8b2f89f7d85fa1b6b881bd
7
- data.tar.gz: 34bb2249fafab5002d569ddd267a3944e43e03b51a793ed162c3166900699b658efdb8ace15a3a6dca29000aa68787046732d2d1df88b31468fc096ffd18641f
6
+ metadata.gz: b9b4db6825f88670f27d768f40724fbd22c2f8e92fc6ce5f3bc32650cc9e77475d3c6eecb51fd7f59b10d70e735f648c92ecdae6f6d4b8ff61ff4b5c39e501d3
7
+ data.tar.gz: f474ad70283d4faa9b33bd51346acb64b29a4076b9c9c0ffcff58e60f75d331bc54b162a8e3d77310153d39caf898ac2924b234389b1089b3262b35fa7b48ac3
data/README.md CHANGED
@@ -86,6 +86,7 @@ class CommentNotification < Noticed::Base
86
86
  end
87
87
 
88
88
  # URL helpers are accessible in notifications
89
+ # Don't forget to set your default_url_options so Rails knows how to generate urls
89
90
  def url
90
91
  post_path(params[:post])
91
92
  end
@@ -113,6 +114,12 @@ You can define helper methods inside your Notification object to make it easier
113
114
 
114
115
  Rails url helpers are included in notification classes by default so you have full access to them just like you would in your controllers and views.
115
116
 
117
+ Don't forget, you'll need to configure `default_url_options` in order for Rails to know what host and port to use when generating URLs.
118
+
119
+ ```ruby
120
+ Rails.application.routes.default_url_options[:host] = 'localhost:3000'
121
+ ```
122
+
116
123
  **Callbacks**
117
124
 
118
125
  Like ActiveRecord, notifications have several different types of callbacks.
@@ -342,19 +349,24 @@ Delivery methods have access to the following methods and attributes:
342
349
 
343
350
  #### Validating options passed to Custom Delivery methods
344
351
 
345
- You can validate the options passed to the custom delivery method and raise validation errors. This is helpful for debugging to make sure valid and required options were passed in.
352
+ The presence of the delivery method options is automatically validated if using the `option(s)` method.
346
353
 
347
- To do this, simply override the `self.validate!(options)` method from the `Noticed::DeliveryMethods::Base` class in your Custom Delivery method.
354
+ If you want to validate that the passed options contain valid values, or to add any custom validations, override the `self.validate!(delivery_method_options)` method from the `Noticed::DeliveryMethods::Base` class.
348
355
 
349
356
  ```ruby
350
357
  class DeliveryMethods::Discord < Noticed::DeliveryMethods::Base
358
+ option :username # Requires the username option to be passed
359
+
351
360
  def deliver
352
361
  # Logic for sending a Discord notification
353
362
  end
354
363
 
355
- def self.validate!(options)
356
- unless options.key?(:sent_by)
357
- raise Noticed::ValidationError, 'the `sent_by` option is missing'
364
+ def self.validate!(delivery_method_options)
365
+ super # Don't forget to call super, otherwise option presence won't be validated
366
+
367
+   # Custom validations
368
+ if delivery_method_options[:username].blank?
369
+ raise Noticed::ValidationError, 'the `username` option must be present'
358
370
  end
359
371
  end
360
372
  end
@@ -370,7 +382,7 @@ To fix the error, the argument has to be passed correctly. For example:
370
382
 
371
383
  ```ruby
372
384
  class CommentNotification < Noticed::Base
373
- deliver_by :discord, class: 'DeliveryMethods::Discord', sent_by: User.admin.first
385
+ deliver_by :discord, class: 'DeliveryMethods::Discord', username: User.admin.username
374
386
  end
375
387
  ```
376
388
 
@@ -394,7 +406,7 @@ Rails 6.1+ can serialize Class and Module objects as arguments to ActiveJob. The
394
406
  deliver_by DeliveryMethods::Discord
395
407
  ```
396
408
 
397
- For Rails 6.0 and earlier, you must pass strings of the class names in the `deliver_by` options.
409
+ For Rails 6.0, you must pass strings of the class names in the `deliver_by` options.
398
410
 
399
411
  ```ruby
400
412
  deliver_by :discord, class: "DeliveryMethods::Discord"
@@ -15,7 +15,7 @@ module Noticed
15
15
  argument :attributes, type: :array, default: [], banner: "field:type field:type"
16
16
 
17
17
  def generate_notification
18
- generate :model, name, "recipient:references{polymorphic}", "type", params_column, "read_at:datetime", *attributes
18
+ generate :model, name, "recipient:references{polymorphic}", "type", params_column, "read_at:datetime:index", *attributes
19
19
  end
20
20
 
21
21
  def add_noticed_model
@@ -43,7 +43,7 @@ module Noticed
43
43
 
44
44
  def params_column
45
45
  case ActiveRecord::Base.configurations.configs_for(spec_name: "primary").config["adapter"]
46
- when "mysql"
46
+ when "mysql2"
47
47
  "params:json"
48
48
  when "postgresql"
49
49
  "params:jsonb"
@@ -32,7 +32,7 @@ module Noticed
32
32
  def params(*names)
33
33
  param_names.concat Array.wrap(names)
34
34
  end
35
- alias param params
35
+ alias_method :param, :params
36
36
  end
37
37
 
38
38
  def initialize(params = {})
@@ -17,18 +17,18 @@ module Noticed
17
17
 
18
18
  def channel
19
19
  @channel ||= begin
20
- value = options[:channel]
21
- case value
22
- when String
23
- value.constantize
24
- when Symbol
25
- notification.send(value)
26
- when Class
27
- value
28
- else
29
- Noticed::NotificationChannel
30
- end
31
- end
20
+ value = options[:channel]
21
+ case value
22
+ when String
23
+ value.constantize
24
+ when Symbol
25
+ notification.send(value)
26
+ when Class
27
+ value
28
+ else
29
+ Noticed::NotificationChannel
30
+ end
31
+ end
32
32
  end
33
33
  end
34
34
  end
@@ -4,8 +4,31 @@ module Noticed
4
4
  extend ActiveModel::Callbacks
5
5
  define_model_callbacks :deliver
6
6
 
7
+ class_attribute :option_names, instance_writer: false, default: []
8
+
7
9
  attr_reader :notification, :options, :params, :recipient, :record
8
10
 
11
+ class << self
12
+ # Copy option names from parent
13
+ def inherited(base) #:nodoc:
14
+ base.option_names = option_names.dup
15
+ super
16
+ end
17
+
18
+ def options(*names)
19
+ option_names.concat Array.wrap(names)
20
+ end
21
+ alias_method :option, :options
22
+
23
+ def validate!(delivery_method_options)
24
+ option_names.each do |option_name|
25
+ unless delivery_method_options.key? option_name
26
+ raise ValidationError, "option `#{option_name}` must be set for #{name}"
27
+ end
28
+ end
29
+ end
30
+ end
31
+
9
32
  def perform(args)
10
33
  @notification = args[:notification_class].constantize.new(args[:params])
11
34
  @options = args[:options]
@@ -26,11 +49,6 @@ module Noticed
26
49
  raise NotImplementedError, "Delivery methods must implement a deliver method"
27
50
  end
28
51
 
29
- def self.validate!(options)
30
- # Override this method in your custom DeliveryMethod class to validate the options
31
- # and raise error, if invalid.
32
- end
33
-
34
52
  private
35
53
 
36
54
  # Helper method for making POST requests from delivery methods
@@ -1,16 +1,12 @@
1
1
  module Noticed
2
2
  module DeliveryMethods
3
3
  class Email < Base
4
+ option :mailer
5
+
4
6
  def deliver
5
7
  mailer.with(format).send(method.to_sym).deliver_later
6
8
  end
7
9
 
8
- def self.validate!(options)
9
- unless options.key?(:mailer)
10
- raise ValidationError, "email delivery method requires a 'mailer' to be specified"
11
- end
12
- end
13
-
14
10
  private
15
11
 
16
12
  def mailer
@@ -32,10 +32,10 @@ module Noticed
32
32
  # Rehydrate the database notification into the Notification object for rendering
33
33
  def to_notification
34
34
  @_notification ||= begin
35
- instance = type.constantize.with(params)
36
- instance.record = self
37
- instance
38
- end
35
+ instance = type.constantize.with(params)
36
+ instance.record = self
37
+ instance
38
+ end
39
39
  end
40
40
 
41
41
  def mark_as_read!
@@ -10,7 +10,7 @@ module Noticed
10
10
  def translate(key, **options)
11
11
  I18n.translate(scope_translation_key(key), **options)
12
12
  end
13
- alias t translate
13
+ alias_method :t, :translate
14
14
 
15
15
  def scope_translation_key(key)
16
16
  if key.to_s.start_with?(".")
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "1.2.15"
2
+ VERSION = "1.2.16"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noticed
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.15
4
+ version: 1.2.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-02 00:00:00.000000000 Z
11
+ date: 2020-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -119,7 +119,7 @@ homepage: https://github.com/excid3/noticed
119
119
  licenses:
120
120
  - MIT
121
121
  metadata: {}
122
- post_install_message:
122
+ post_install_message:
123
123
  rdoc_options: []
124
124
  require_paths:
125
125
  - lib
@@ -134,8 +134,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  - !ruby/object:Gem::Version
135
135
  version: '0'
136
136
  requirements: []
137
- rubygems_version: 3.1.2
138
- signing_key:
137
+ rubygems_version: 3.1.4
138
+ signing_key:
139
139
  specification_version: 4
140
140
  summary: Notifications for Ruby on Rails applications
141
141
  test_files: []