Class: MailyHerald::Subscription

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/maily_herald/subscription.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_from(entity) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/maily_herald/subscription.rb', line 28

def self.get_from(entity)
  if entity.has_attribute?(:maily_subscription_id) && entity.maily_subscription_id
    subscription = MailyHerald::Subscription.new

    entity.attributes.each do |k, v|
      if match = k.match(/^maily_subscription_(\w+)$/)
        subscription.send("#{match[1]}=", v)
      end
    end

    subscription.readonly!
    subscription
  end
end

Instance Method Details

#activate!Object



51
52
53
# File 'app/models/maily_herald/subscription.rb', line 51

def activate!
  update_attribute(:active, true)
end

#active?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/maily_herald/subscription.rb', line 43

def active?
  read_attribute(:id) && read_attribute(:active)
end

#deactivate!Object



47
48
49
# File 'app/models/maily_herald/subscription.rb', line 47

def deactivate!
  update_attribute(:active, false)
end

#logsObject



84
85
86
# File 'app/models/maily_herald/subscription.rb', line 84

def logs
  self.list.logs.for_entity(self.entity)
end

#to_liquidObject



63
64
65
66
67
# File 'app/models/maily_herald/subscription.rb', line 63

def to_liquid
  {
    "token_url" => token_url
  }
end

#toggle!Object



55
56
57
# File 'app/models/maily_herald/subscription.rb', line 55

def toggle!
  active? ? deactivate! : activate!
end

#token_urlObject



59
60
61
# File 'app/models/maily_herald/subscription.rb', line 59

def token_url
  MailyHerald::Engine.routes.url_helpers.maily_unsubscribe_url(self)
end

#update_schedulesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/maily_herald/subscription.rb', line 69

def update_schedules
  AdHocMailing.where(list_id: self.list).each do |m|
    m.set_schedule_for self.entity
  end
  OneTimeMailing.where(list_id: self.list).each do |m|
    m.set_schedule_for self.entity
  end
  PeriodicalMailing.where(list_id: self.list).each do |m|
    m.set_schedule_for self.entity
  end
  Sequence.where(list_id: self.list).each do |s|
    s.set_schedule_for self.entity
  end
end