Module: StateOfTheNation::ClassMethods

Defined in:
lib/state_of_the_nation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#finish_keyObject

Returns the value of attribute finish_key.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def finish_key
  @finish_key
end

#ignore_emptyObject

Returns the value of attribute ignore_empty.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def ignore_empty
  @ignore_empty
end

#parent_associationObject

Returns the value of attribute parent_association.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def parent_association
  @parent_association
end

#prevent_multiple_activeObject

Returns the value of attribute prevent_multiple_active.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def prevent_multiple_active
  @prevent_multiple_active
end

#start_keyObject

Returns the value of attribute start_key.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def start_key
  @start_key
end

Instance Method Details

#considered_active(ignore_empty: false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/state_of_the_nation.rb', line 18

def considered_active(ignore_empty: false)
  @ignore_empty = ignore_empty

  def from(start_key)
    @start_key = start_key
    self
  end

  def until(finish_key)
    @finish_key = finish_key

    define_method "active?" do |time = Time.now.utc|
      (finish.blank? || round_if_should(finish) > round_if_should(time)) && round_if_should(start) <= round_if_should(time)
    end

    scope :active, lambda { |time = Time.now.utc|
      where(QueryString.query_for(:active_scope, self), round_if_should(time), round_if_should(time))
    }
  end

  private def round_if_should(time)
    return time if !should_round_timestamps?
    time.respond_to?(:round) ? time.round : time
  end

  private def should_round_timestamps?
    # MySQL datetime fields do not support millisecond resolution while
    # PostgreSQL's do. To prevent issues with near identical timestamps not
    # comparing as expected in .active? methods we'll choose the resolution
    # appropriate for the database adapter backing the model.
    case self.connection.adapter_name
    when /PostgreSQL/
      false
    else
      true
    end
  end

  self
end

#from(start_key) ⇒ Object



21
22
23
24
# File 'lib/state_of_the_nation.rb', line 21

def from(start_key)
  @start_key = start_key
  self
end

#has_active(association_plural) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/state_of_the_nation.rb', line 59

def has_active(association_plural)
  @association_plural = association_plural
  add_child_methods(plural: @association_plural, single: false, with_identity_cache: false)

  def with_identity_cache
    add_child_methods(plural: @association_plural, single: false, with_identity_cache: true)
    self
  end

  self
end

#has_uniquely_active(association_singular) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/state_of_the_nation.rb', line 71

def has_uniquely_active(association_singular)
  @association_plural = association_singular.to_s.pluralize
  add_child_methods(plural: @association_plural, single: true, with_identity_cache: false)

  def with_identity_cache
    add_child_methods(plural: @association_plural, single: true, with_identity_cache: true)
    self
  end

  self
end

#until(finish_key) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/state_of_the_nation.rb', line 26

def until(finish_key)
  @finish_key = finish_key

  define_method "active?" do |time = Time.now.utc|
    (finish.blank? || round_if_should(finish) > round_if_should(time)) && round_if_should(start) <= round_if_should(time)
  end

  scope :active, lambda { |time = Time.now.utc|
    where(QueryString.query_for(:active_scope, self), round_if_should(time), round_if_should(time))
  }
end

#with_identity_cacheObject



63
64
65
66
# File 'lib/state_of_the_nation.rb', line 63

def with_identity_cache
  add_child_methods(plural: @association_plural, single: false, with_identity_cache: true)
  self
end