Class: OptOut::Adapters::ActiveRecordAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- OptOut::Adapters::ActiveRecordAdapter
- Defined in:
- lib/opt_out/adapters/active_record_adapter.rb
Overview
Adapter that stores persists data through ActiveRecord. It requires the following table:
:list_id string
:user_id string
composite index on (list_id, user_id)
Options
:table_name - name of storage table. Defaults to 'opt_outs'
Instance Method Summary collapse
- #reset ⇒ Object
- #subscribe(list_id, user_id) ⇒ Object
-
#unsubscribe(list_id, user_id) ⇒ Object
TODO: would prefer opt_outs table to not have a primary key ‘id`, but that’s not working right now.
- #unsubscribed?(list_id, user_id) ⇒ Boolean
- #unsubscribers(list_id) ⇒ Object
Methods inherited from AbstractAdapter
Constructor Details
This class inherits a constructor from OptOut::Adapters::AbstractAdapter
Instance Method Details
#reset ⇒ Object
40 41 42 |
# File 'lib/opt_out/adapters/active_record_adapter.rb', line 40 def reset store.delete_all end |
#subscribe(list_id, user_id) ⇒ Object
16 17 18 19 20 |
# File 'lib/opt_out/adapters/active_record_adapter.rb', line 16 def subscribe(list_id, user_id) return if [list_id, user_id].any? {|s| s.nil? || s == ''} store.where(:list_id => list_id.to_s, :user_id => user_id.to_s).delete_all nil end |
#unsubscribe(list_id, user_id) ⇒ Object
TODO: would prefer opt_outs table to not have a primary key ‘id`, but that’s not working right now
24 25 26 27 28 29 30 |
# File 'lib/opt_out/adapters/active_record_adapter.rb', line 24 def unsubscribe(list_id, user_id) store.create(:list_id => list_id.to_s, :user_id => user_id.to_s) rescue ActiveRecord::RecordNotUnique # already unsubscribed ensure return nil end |
#unsubscribed?(list_id, user_id) ⇒ Boolean
32 33 34 |
# File 'lib/opt_out/adapters/active_record_adapter.rb', line 32 def unsubscribed?(list_id, user_id) store.exists?(:list_id => list_id.to_s, :user_id => user_id.to_s) end |
#unsubscribers(list_id) ⇒ Object
36 37 38 |
# File 'lib/opt_out/adapters/active_record_adapter.rb', line 36 def unsubscribers(list_id) store.where(:list_id => list_id.to_s).map(&:user_id).to_a end |