Class: Noticent::ActiveRecordOptInProvider
- Inherits:
-
Object
- Object
- Noticent::ActiveRecordOptInProvider
- Defined in:
- lib/noticent/active_record_opt_in_provider.rb
Overview
should be used only for testing
Instance Method Summary collapse
- #add_alert(scope:, alert_name:, recipient_ids:, channel:) ⇒ Object
- #opt_in(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) ⇒ Object
- #opt_out(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) ⇒ Object
- #opted_in?(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) ⇒ Boolean
- #remove_alert(scope:, alert_name:) ⇒ Object
- #remove_entity(scope:, entity_id:) ⇒ Object
- #remove_recipient(recipient_id:) ⇒ Object
Instance Method Details
#add_alert(scope:, alert_name:, recipient_ids:, channel:) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/noticent/active_record_opt_in_provider.rb', line 18 def add_alert(scope:, alert_name:, recipient_ids:, channel:) ActiveRecord::Base.transaction do now = Time.now.utc.to_s(:db) # fetch all permutations of recipient and entity id permutations = Noticent::OptIn.distinct .where('recipient_id IN (?)', recipient_ids) .pluck(:entity_id, :recipient_id) return if permutations.empty? values = permutations.map { |e, r| "('#{scope}','#{alert_name}', #{e}, #{r}, '#{channel}', '#{now}', '#{now}')" }.join(',') ActiveRecord::Base.connection.execute("INSERT INTO opt_ins (scope, alert_name, entity_id, recipient_id, channel_name, created_at, updated_at) VALUES #{values}") end end |
#opt_in(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) ⇒ Object
6 7 8 |
# File 'lib/noticent/active_record_opt_in_provider.rb', line 6 def opt_in(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) Noticent::OptIn.create!(recipient_id: recipient_id, scope: scope, entity_id: entity_id, alert_name: alert_name, channel_name: channel_name) end |
#opt_out(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) ⇒ Object
10 11 12 |
# File 'lib/noticent/active_record_opt_in_provider.rb', line 10 def opt_out(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) Noticent::OptIn.where(recipient_id: recipient_id, scope: scope, entity_id: entity_id, alert_name: alert_name, channel_name: channel_name).destroy_all end |
#opted_in?(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) ⇒ Boolean
14 15 16 |
# File 'lib/noticent/active_record_opt_in_provider.rb', line 14 def opted_in?(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) Noticent::OptIn.where(recipient_id: recipient_id, scope: scope, entity_id: entity_id, alert_name: alert_name, channel_name: channel_name).count != 0 end |
#remove_alert(scope:, alert_name:) ⇒ Object
33 34 35 |
# File 'lib/noticent/active_record_opt_in_provider.rb', line 33 def remove_alert(scope:, alert_name:) Noticent::OptIn.where('scope = ? AND alert_name = ?', scope, alert_name).destroy_all end |