Module: Sraas::HasSraas::ClassMethods

Defined in:
lib/sraas/has_sraas.rb

Instance Method Summary collapse

Instance Method Details

#has_sraas(name: nil, fingerprint: nil, deck: nil, timezone: nil) ⇒ Object

name: string – the name of the deck to use with Sraas

symbol -- the method to call on this object that returns a deck name
nil    -- use the default in configuration or errors...

fingerprint: string – allows locking to one specific deck version timezone: string – the timezone to use with Sraas

symbol -- the method to call on this object that returns name of timezone field
nil    -- use the default in configuration or errors...


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sraas/has_sraas.rb', line 11

def has_sraas(name: nil, fingerprint: nil, deck: nil, timezone: nil)
  if deck # v1.0 Syntax
    raise "Since GEM 0.3.0, semantics changed for has_sraas. You need to say has_sraas name: ..."
  end
  has_one :sraas, as: :sraas_consumerable, class_name: 'Sraas::Consumer'

  after_create :configure_sraas_consumer!
  before_update :update_sraas_consumer!

  if name.is_a?(Symbol)
    # We allow specifying a symbol as a method name/attribute that will return the deck name
    # make sure model supports it:
    raise "[Undefined Method Error] Class '#{self.name}' doesn't have a method #{name}" unless self.method_defined?(name) || self.has_attribute?(name)
  end
  self.sraas_default_deck_name = name || Sraas.configuration.default_deck_name
  self.sraas_default_deck_fingerprint = fingerprint || Sraas.configuration.default_deck_fingerprint

  if timezone.is_a?(Symbol)
    # We allow specifying a symbol as a method name/attribute that will return the name of timezone field
    # make sure model supports it:
    raise "[Undefined Method Error] Class '#{self.name}' doesn't have a method '#{timezone}'" unless self.method_defined?(timezone) || self.has_attribute?(timezone)
  end
  self.sraas_default_timezone = timezone || Sraas.configuration.default_timezone
  Time.find_zone!(self.sraas_default_timezone) unless self.sraas_default_timezone.is_a?(Symbol)
end