Module: EffectiveGem::ClassMethods

Defined in:
lib/effective_resources/effective_gem.rb

Instance Method Summary collapse

Instance Method Details

#class_name(name, key) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/effective_resources/effective_gem.rb', line 55

def class_name(name, key)
  raise('expected name to be a string') unless name.kind_of?(String)
  raise('expected key to be a symbol') unless key.kind_of?(Symbol)

  namespace = name.split('::').first.underscore.to_sym if name.include?('::')
  resolve_class_name(key, namespace: namespace)
end

#config(namespace = nil) ⇒ Object



29
30
31
32
# File 'lib/effective_resources/effective_gem.rb', line 29

def config(namespace = nil)
  namespace ||= Tenant.current if defined?(Tenant)
  (@config[namespace] if namespace) || @config[:effective]
end

#deliver_methodObject



76
77
78
# File 'lib/effective_resources/effective_gem.rb', line 76

def deliver_method
  config[:deliver_method].presence || EffectiveResources.deliver_method
end

#klass(key, fallback: nil) ⇒ Object



63
64
65
66
67
68
# File 'lib/effective_resources/effective_gem.rb', line 63

def klass(key, fallback: nil)
  raise('expected key to be a symbol') unless key.kind_of?(Symbol)

  namespace = Tenant.current if defined?(Tenant)
  resolve_class_name(key, namespace: namespace, fallback: fallback).constantize
end

#mailer_adminObject



92
93
94
# File 'lib/effective_resources/effective_gem.rb', line 92

def mailer_admin
  config[:mailer_admin].presence || EffectiveResources.mailer_admin
end

#mailer_fromsObject



88
89
90
# File 'lib/effective_resources/effective_gem.rb', line 88

def mailer_froms
  config[:mailer_froms].presence || EffectiveResources.mailer_froms
end

#mailer_layoutObject



80
81
82
# File 'lib/effective_resources/effective_gem.rb', line 80

def mailer_layout
  config[:mailer_layout].presence || EffectiveResources.mailer_layout
end

#mailer_senderObject



84
85
86
# File 'lib/effective_resources/effective_gem.rb', line 84

def mailer_sender
  config[:mailer_sender].presence || EffectiveResources.mailer_sender
end

#mailer_subjectObject



96
97
98
# File 'lib/effective_resources/effective_gem.rb', line 96

def mailer_subject
  config[:mailer_subject].presence || EffectiveResources.mailer_subject
end

#parent_mailer_classObject

Mailer Settings These methods are intended to flow through to the default EffectiveResources settings



72
73
74
# File 'lib/effective_resources/effective_gem.rb', line 72

def parent_mailer_class
  config[:parent_mailer].presence&.constantize || EffectiveResources.parent_mailer_class
end

#send_email(email, *args) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/effective_resources/effective_gem.rb', line 100

def send_email(email, *args)
  raise('gem does not respond to mailer_class') unless respond_to?(:mailer_class)
  raise('expected args to be an Array') unless args.kind_of?(Array)

  begin
    mailer_class.send(email, *args).send(deliver_method)
  rescue => e
    associated = args.first

    if associated.kind_of?(ActiveRecord::Base)
      EffectiveLogger.error(e.message, associated: associated, details: { email: email }) if defined?(EffectiveLogger)
      EffectiveResources.send_error(e, email: email, associated_id: associated.id, associated_type: associated.class.name)
    else
      args_to_s = args.to_s.gsub('<', '').gsub('>', '')
      EffectiveLogger.error(e.message, details: { email: email, args: args_to_s }) if defined?(EffectiveLogger)
      EffectiveResources.send_error(e, email: email, details: args_to_s)
    end

    raise(e) unless Rails.env.production? || Rails.env.staging?
  end
end

#setup(namespace = nil) {|config(namespace)| ... } ⇒ Object

Yields:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/effective_resources/effective_gem.rb', line 34

def setup(namespace = nil, &block)
  @config ||= ActiveSupport::OrderedOptions.new

  namespace ||= Tenant.current if defined?(Tenant)
  namespace ||= :effective

  @config[namespace] ||= ActiveSupport::InheritableOptions.new(@config[:effective])

  yield(config(namespace))

  if(unsupported = (config(namespace).keys - config_keys)).present?
    if unsupported.include?(:authorization_method)
      raise("config.authorization_method has been removed. This gem will call EffectiveResources.authorization_method instead. Please double check the config.authorization_method setting in config/initializers/effective_resources.rb and remove it from this file.")
    end

    raise("unsupported config keys: #{unsupported}\n supported keys: #{config_keys}")
  end

  true
end