Class: ModelsAuditor::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/models_auditor/config.rb

Constant Summary collapse

CONFIG_OPTIONS =
%i(
  audit_enabled
  connection_namespace
  audit_records_table_name
  audit_requests_table_name
  audit_migrations_dir
  audit_request_changes_only
  logger
  records_per_page
  fake_total_count
  audit_controller_base
  respond_to_json_enabled
  respond_to_html_enabled
  json_response_data_key
  json_response_meta_key
)

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



20
21
22
# File 'lib/models_auditor/config.rb', line 20

def initialize
  @indexed_relations = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/models_auditor/config.rb', line 85

def method_missing(method_sym, *args)
  method_name = method_sym.to_s
  option_name = method_name.tr('=', '')
  super if CONFIG_OPTIONS.exclude?(option_name.to_sym)
  if method_name =~ /^.*=$/
    raise ArgumentError.new('Incorrect number of arguments') if args.size != 1
    instance_variable_set("@#{option_name}", args[0]) unless ModelsAuditor.configured?
  else
    var_name = "@#{option_name}"
    instance_variable_defined?(var_name) ?
      instance_variable_get(var_name) :
      default[option_name.to_sym].try(:[], :val)
  end
end

Instance Method Details

#defaultObject



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/models_auditor/config.rb', line 24

def default
  @default ||= {
    audit_enabled:              {
      config: '%w(staging production).include?(Rails.env)',
      val:    true
    },
    connection_namespace:       {
      config: "'audit'",
      val:    'audit'
    },
    audit_records_table_name:   {
      config: "'audit_records'",
      val:    'audit_records'
    },
    audit_requests_table_name:  {
      config: "'audit_requests'",
      val:    'audit_requests'
    },
    audit_migrations_dir:       {
      config: "'audit_migrate'",
      val:    'audit_migrate'
    },
    logger:                     {
      config: "Logger.new(Rails.root.join('log', 'models_auditor.log'))",
      val:    Logger.new(Rails.root.join('log', 'models_auditor.log'))
    },
    audit_request_changes_only: {
      config: 'true',
      val:    true
    },
    records_per_page:           {
      config: '10',
      val:    10
    },
    fake_total_count:           {
      config: 'true',
      val:    true
    },
    audit_controller_base:      {
      config: "'ModelsAuditor::AuditBaseController'",
      val:    'ModelsAuditor::AuditBaseController'
    },
    respond_to_json_enabled:    {
      config: 'true',
      val:    true
    },
    respond_to_html_enabled:    {
      config: 'false',
      val:    false
    },
    json_response_data_key:     {
      config: "'entries'",
      val:    'entries'
    },
    json_response_meta_key:     {
      config: "'meta'",
      val:    'meta'
    },
  }
end