Class: MailyHerald::List

Inherits:
ActiveRecord::Base
  • Object
show all
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

Instance Method Summary collapse

Methods included from Autonaming

included

Instance Attribute Details

#context_nameString

Name of the Context used by List.

Returns:

  • (String)

    the current value of context_name



11
12
13
# File 'app/models/maily_herald/list.rb', line 11

def context_name
  @context_name
end

#nameString

Returns the current value of name.

Returns:

  • (String)

    the current value of name



11
12
13
# File 'app/models/maily_herald/list.rb', line 11

def name
  @name
end

#titleString

Returns the current value of title.

Returns:

  • (String)

    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_countObject

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

#contextObject

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.

Returns:

  • (Boolean)

See Also:



112
113
114
# File 'app/models/maily_herald/list.rb', line 112

def locked?
  MailyHerald.list_locked?(self.name)
end

#logsObject

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_outsObject

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.

Returns:

  • (Boolean)


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_subscribersObject

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.

Parameters:

  • entity (Object)

    Entity object. Need to be in the Context scope.



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.

Returns:

  • (Boolean)


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

#subscribersObject

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.

Parameters:

  • entity (Object)

    Entity object.



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.

Returns:

  • (Boolean)


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