Class: MailyHerald::List
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MailyHerald::List
- Includes:
- Autonaming
- Defined in:
- app/models/maily_herald/list.rb
Overview
Represents subscriptions list.
Entities can be subscribed to lists by creating Subscription object for them.
List have Context assigned. Only entities from Context scope can be subscribed to list.
Instance Attribute Summary collapse
-
#context_name ⇒ String
Name of the Context used by List.
-
#name ⇒ String
The current value of name.
-
#title ⇒ String
The current value of title.
Instance Method Summary collapse
-
#active_subscription_count ⇒ Object
Returns number of List subscribers.
-
#context ⇒ Object
Returns Context object associated with List.
-
#locked? ⇒ Boolean
Check if List is locked.
-
#logs ⇒ Object
Returns Log objects collection related to this List.
-
#opt_outs ⇒ Object
Returns entities within the context’s scope with inactive subscription.
-
#opted_out?(entity) ⇒ Boolean
Checks whether entity has been removed from List.
-
#potential_subscribers ⇒ Object
Returns entities within the context’s scope without subscription.
-
#subscribe!(entity) ⇒ Object
Subscribes entity to List.
-
#subscribed?(entity) ⇒ Boolean
Checks whether entity is subscribed to List.
-
#subscribers ⇒ Object
Returns entities within the context’s scope with active subscription.
-
#subscription_for(entity) ⇒ Object
Returns subscription for given entity.
-
#unsubscribe!(entity) ⇒ Object
Unsubscribes entity from List.
-
#unsubscribed?(entity) ⇒ Boolean
Checks whether entity is not subscribed to List.
Methods included from Autonaming
Instance Attribute Details
#context_name ⇒ String
Name of the Context used by List.
11 12 13 |
# File 'app/models/maily_herald/list.rb', line 11 def context_name @context_name end |
#name ⇒ String
Returns the current value of name.
11 12 13 |
# File 'app/models/maily_herald/list.rb', line 11 def name @name end |
#title ⇒ String
Returns the current value of title.
11 12 13 |
# File 'app/models/maily_herald/list.rb', line 11 def title @title end |
Instance Method Details
#active_subscription_count ⇒ Object
Returns number of List subscribers.
84 85 86 |
# File 'app/models/maily_herald/list.rb', line 84 def active_subscription_count subscribers.count(:id) end |
#context ⇒ Object
Returns Context object associated with List.
34 35 36 |
# File 'app/models/maily_herald/list.rb', line 34 def context @context ||= MailyHerald.context self.context_name end |
#locked? ⇒ Boolean
Check if List is locked.
112 113 114 |
# File 'app/models/maily_herald/list.rb', line 112 def locked? MailyHerald.list_locked?(self.name) end |
#logs ⇒ Object
Returns MailyHerald::Log objects collection related to this List.
105 106 107 108 |
# File 'app/models/maily_herald/list.rb', line 105 def logs #Log.for_mailings(self.dispatches.select("id")) Log.for_mailings(Dispatch.where("sequence_id IN (?) OR list_id = (?)", Sequence.where(list_id: self.id).select("id"), self.id).select("id")) end |
#opt_outs ⇒ Object
Returns entities within the context’s scope with inactive subscription.
94 95 96 |
# File 'app/models/maily_herald/list.rb', line 94 def opt_outs context.scope_with_subscription(self).where("#{Subscription.table_name}.active = (?)", false).where("#{Subscription.table_name}.list_id = (?)", self.id) end |
#opted_out?(entity) ⇒ Boolean
Checks whether entity has been removed from List.
True only if user was intentionally unsubscribed.
73 74 75 76 |
# File 'app/models/maily_herald/list.rb', line 73 def opted_out? entity s = subscription_for(entity) s ? !s.active? : false end |
#potential_subscribers ⇒ Object
Returns entities within the context’s scope without subscription.
99 100 101 102 |
# File 'app/models/maily_herald/list.rb', line 99 def potential_subscribers sq = context.scope_with_subscription(self, :outer).where("#{Subscription.table_name}.list_id = (?)", self.id).pluck("#{context.model.table_name}.id") context.scope.where(context.model.arel_table[:id].not_in(sq)) end |
#subscribe!(entity) ⇒ Object
Subscribes entity to List.
41 42 43 44 45 |
# File 'app/models/maily_herald/list.rb', line 41 def subscribe! entity s = subscription_for(entity) s ? s.activate! : s = create_subscription_for(entity, true) s end |
#subscribed?(entity) ⇒ Boolean
Checks whether entity is subscribed to List.
57 58 59 60 |
# File 'app/models/maily_herald/list.rb', line 57 def subscribed? entity s = Subscription.get_from(entity) || subscription_for(entity) s.try(:active?) end |
#subscribers ⇒ Object
Returns entities within the context’s scope with active subscription.
89 90 91 |
# File 'app/models/maily_herald/list.rb', line 89 def subscribers context.scope_with_subscription(self).where("#{Subscription.table_name}.active = (?)", true).where("#{Subscription.table_name}.list_id = (?)", self.id) end |
#subscription_for(entity) ⇒ Object
Returns subscription for given entity.
79 80 81 |
# File 'app/models/maily_herald/list.rb', line 79 def subscription_for entity self.subscriptions.for_entity(entity).first end |
#unsubscribe!(entity) ⇒ Object
Unsubscribes entity from List.
50 51 52 53 54 |
# File 'app/models/maily_herald/list.rb', line 50 def unsubscribe! entity s = subscription_for(entity) s ? s.deactivate! : s = create_subscription_for(entity, false) s end |
#unsubscribed?(entity) ⇒ Boolean
Checks whether entity is not subscribed to List.
True if user has inactive subscription or never been subscribed.
65 66 67 68 |
# File 'app/models/maily_herald/list.rb', line 65 def unsubscribed? entity s = Subscription.get_from(entity) || subscription_for(entity) s ? !s.active? : true end |