Skip to content

Commit

Permalink
Add example validation for :record association. #372
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Jan 20, 2024
1 parent d0bea36 commit 4ee966d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ rails db:migrate

Noticed operates with a few constructs: Notifiers, delivery methods, and Notification records.

To start, generate a Notifier:
To start, generate a Notifier:

```sh
rails generate noticed:notifier NewCommentNotifier
Expand Down Expand Up @@ -126,7 +126,10 @@ class CarSaleNotifier < Noticed::Event
deliver_by :email { |c| c.mailer = "BranchMailer" }

# `record` is the Car record, `Branch` is the dealership
required_params :record, :branch
required_params :branch

# To validate the `:record` param, add a validation since it is an association on the Noticed::Event
validates :record, presence: true
end
```

Expand Down Expand Up @@ -175,13 +178,13 @@ From the above Notifier...
```ruby
class NewCommentNotifier < Noticed::Event
# ...

notification_methods do
def message
t(".message")
end
end

# ...
end
```
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/notifiers/record_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class RecordNotifier < Noticed::Event
validates :record, presence: true
end
13 changes: 13 additions & 0 deletions test/notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ class NotifierTest < ActiveSupport::TestCase
assert_equal({user: user}, notification.params)
end

test "assigns record association from params" do
user = users(:one)
notifier = RecordNotifier.with(record: user)
assert_equal user, notifier.record
assert_empty notifier.params
end

test "can add validations for record association" do
notifier = RecordNotifier.with({})
refute notifier.valid?
assert_equal ["can't be blank"], notifier.errors[:record]
end

test "deliver creates an event" do
assert_difference "Noticed::Event.count" do
ReceiptNotifier.deliver(User.first)
Expand Down

0 comments on commit 4ee966d

Please sign in to comment.