Skip to content

Commit

Permalink
generate alert constants
Browse files Browse the repository at this point in the history
Signed-off-by: Khash Sajadi <[email protected]>
  • Loading branch information
khash committed Jun 6, 2019
1 parent ce1b35e commit 4536c68
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ account_payload = AccountPayload.new(1, user.first)
Noticent.notify(:new_user, account_payload)
```

While it is possible to define and use alert names as symbols, Noticent also creates a constant with the name of the alert under the `Noticent` namespace to help with the use of alert names.
By using the constants you can make sure alert names are free of typos.

For example, if you have an alert called `some_event` then after configuration there will be a constant called `Noticent::ALERT_SOME_EVENT` available to use with the value `:some_event`.

### Using Each Noticent Component

#### Payload
Expand Down
14 changes: 14 additions & 0 deletions lib/noticent/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def self.configure(options = {}, &block)
@config = Noticent::Config::Builder.new(options, &block).build
@config.validate!

# construct dynamics
@config.create_dynamics

@config
end

Expand Down Expand Up @@ -142,6 +145,17 @@ def view_dir
File.join(base_dir, 'views')
end

def create_dynamics
return if alerts.nil?

alerts.keys.each do |alert|
const_name = "ALERT_#{alert.to_s.upcase}"
next if Noticent.const_defined?(const_name)

Noticent.const_set(const_name, alert)
end
end

def validate!
# check all scopes
scopes&.values&.each(&:validate!)
Expand Down
19 changes: 19 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,23 @@ def post_channel_registration; end
expect(Noticent.configuration.opt_in_provider.opted_in?(recipient_id: 1, scope: :post, entity_id: 2, alert_name: :foo, channel_name: :slack)).not_to be_truthy
end

it 'create alert constants' do
Noticent.configure do
channel :email

scope :post do
alert(:foo) { notify :users }
alert(:some_event) { notify :users }
end

scope :comment do
alert(:new_signup) { notify :users }
end
end

expect { Noticent::ALERT_FOO }.not_to raise_error
expect { Noticent::ALERT_SOME_EVENT}.not_to raise_error
expect { Noticent::ALERT_NEW_SIGNUP }.not_to raise_error
end

end

0 comments on commit 4536c68

Please sign in to comment.