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?('::')
  config(namespace)[(key.to_s.singularize + '_class_name').to_sym] || "Effective::#{key.to_s.singularize.classify}"
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



69
70
71
# File 'lib/effective_resources/effective_gem.rb', line 69

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

#mailer_adminObject



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

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

#mailer_fromsObject



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

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

#mailer_layoutObject



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

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

#mailer_senderObject



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

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

#mailer_subjectObject



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

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



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

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

#send_email(email, *args) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/effective_resources/effective_gem.rb', line 93

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)
      ExceptionNotifier.notify_exception(e, data: { email: email, associated_id: associated.id, associated_type: associated.class.name }) if defined?(ExceptionNotifier)
    else
      args_to_s = args.to_s.gsub('<', '').gsub('>', '')
      EffectiveLogger.error(e.message, details: { email: email, args: args_to_s }) if defined?(EffectiveLogger)
      ExceptionNotifier.notify_exception(e, data: { email: email, args: args_to_s }) if defined?(ExceptionNotifier)
    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::OrderedOptions.new

  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