Class: Subly::Model

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/subly/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activeObject



14
15
16
17
# File 'lib/subly/model.rb', line 14

def self.active
  now = Time.zone.now
  scoped(:conditions => ['starts_at <= ? AND (ends_at > ? OR ends_at IS NULL)', now, now])
end

.by_name(name) ⇒ Object



34
35
36
# File 'lib/subly/model.rb', line 34

def self.by_name(name)
  scoped(:conditions => {:name => name})
end

.expiredObject



19
20
21
22
# File 'lib/subly/model.rb', line 19

def self.expired
  now = Time.zone.now
  scoped(:conditions => ['starts_at <= ? AND ends_at <= ?', now, now])
end

.for_subscriber(sub) ⇒ Object



29
30
31
32
# File 'lib/subly/model.rb', line 29

def self.for_subscriber(sub)
  raise ArgumentError('wrong number of arguments (0 for 1)') if sub.blank?
  scoped(:conditions => ['subscriber_type = ? AND subscriber_id = ?',sub.class.to_s, sub.id])
end

.unexpiredObject



24
25
26
27
# File 'lib/subly/model.rb', line 24

def self.unexpired
  now = Time.zone.now
  scoped(:conditions => ['ends_at > ? OR ends_at IS NULL', now])
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/subly/model.rb', line 46

def active?
  now = Time.now
  !!(starts_at <= now && (ends_at.nil? || ends_at > now))
end

#durationObject



42
43
44
# File 'lib/subly/model.rb', line 42

def duration
  @duration
end

#duration=(duration) ⇒ Object



38
39
40
# File 'lib/subly/model.rb', line 38

def duration=(duration)
  @duration = duration
end

#expire_nowObject



61
62
63
# File 'lib/subly/model.rb', line 61

def expire_now
  self.update_attribute(:ends_at, Time.now)
end

#expired?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/subly/model.rb', line 51

def expired?
  now = Time.now
  !(ends_at.nil? || ends_at > now)
end

#in_the_future?Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/subly/model.rb', line 56

def in_the_future?
  now = Time.now
  !!(starts_at > now && (ends_at.nil? || ends_at > now))
end