Module: TenantCheck

Defined in:
lib/tenant_check.rb,
lib/tenant_check/rack.rb,
lib/tenant_check/version.rb,
lib/tenant_check/notification.rb,
lib/tenant_check/stack_trace_fliter.rb,
lib/tenant_check/active_record/extensions.rb

Defined Under Namespace

Modules: ActiveRecord, StackTraceFilter Classes: Notification, Rack

Constant Summary collapse

VERSION =
'0.1.3'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enableObject

Returns the value of attribute enable.



13
14
15
# File 'lib/tenant_check.rb', line 13

def enable
  @enable
end

.safe_caller_patternsObject



59
60
61
# File 'lib/tenant_check.rb', line 59

def safe_caller_patterns
  @safe_caller_patterns ||= default_safe_caller_patterns
end

.tenant_associationsObject



44
45
46
# File 'lib/tenant_check.rb', line 44

def tenant_associations
  @tenant_associations ||= {}
end

.tenant_class_nameObject

Returns the value of attribute tenant_class_name.



41
42
43
# File 'lib/tenant_check.rb', line 41

def tenant_class_name
  @tenant_class_name
end

Class Method Details

.add_association(table, foreign_key) ⇒ Object



48
49
50
51
# File 'lib/tenant_check.rb', line 48

def add_association(table, foreign_key)
  tenant_associations[table] ||= Set.new
  tenant_associations[table].add(foreign_key)
end

.add_notification(notification) ⇒ Object



100
101
102
# File 'lib/tenant_check.rb', line 100

def add_notification(notification)
  notifications.add(notification) if started?
end

.default_safe_caller_patternsObject



53
54
55
56
57
# File 'lib/tenant_check.rb', line 53

def default_safe_caller_patterns
  [
    /^.*devise.*`serialize_from_(session|cookie)'.*$/,
  ]
end

.enable_and_started?Boolean

Returns:

  • (Boolean)


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

def enable_and_started?
  enable && started? && !temporally_disabled?
end

.endObject



68
69
70
71
# File 'lib/tenant_check.rb', line 68

def end
  Thread.current[:tenant_check_start] = nil
  Thread.current[:tenant_check_notifications] = nil
end

.ignoredObject



89
90
91
92
93
94
# File 'lib/tenant_check.rb', line 89

def ignored
  prev, self.temporally_disabled = temporally_disabled?, true # rubocop:disable Style/ParallelAssignment
  yield
ensure
  self.temporally_disabled = prev
end

.loggerObject



114
115
116
# File 'lib/tenant_check.rb', line 114

def logger
  @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

.notification?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/tenant_check.rb', line 104

def notification?
  !notifications.empty?
end

.notificationsObject



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

def notifications
  Thread.current[:tenant_check_notifications]
end

.output_notificationsObject



108
109
110
111
112
# File 'lib/tenant_check.rb', line 108

def output_notifications
  notifications.each do |notification|
    logger.warn(notification.message)
  end
end

.startObject



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

def start
  Thread.current[:tenant_check_start] = true
  Thread.current[:tenant_check_notifications] = Set.new
end

.started?Boolean

Returns:

  • (Boolean)


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

def started?
  Thread.current[:tenant_check_start]
end

.temporally_disabled=(value) ⇒ Object



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

def temporally_disabled=(value)
  Thread.current[:tenant_check_temporally_disabled] = value
end

.temporally_disabled?Boolean

Returns:

  • (Boolean)


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

def temporally_disabled?
  Thread.current[:tenant_check_temporally_disabled]
end

.tenant_class=(klass) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tenant_check.rb', line 27

def tenant_class=(klass)
  if ::TenantCheck.tenant_class_name != nil && ::TenantCheck.tenant_class_name != klass.name
    raise 'TenantCheck.tenant_class= must be called only once'
  end
  ::TenantCheck.tenant_class_name = klass.name

  klass.reflections.each do |_, reflection|
    case reflection
    when ::ActiveRecord::Reflection::HasManyReflection, ::ActiveRecord::Reflection::HasOneReflection
      ::TenantCheck.add_association(reflection.klass.arel_table.name, reflection.foreign_key)
    end
  end
end