Module: ActiverecordReadonly

Defined in:
lib/activerecord_readonly.rb,
lib/activerecord_readonly/model.rb,
lib/activerecord_readonly/current.rb,
lib/activerecord_readonly/railtie.rb,
lib/activerecord_readonly/version.rb

Defined Under Namespace

Modules: Model Classes: Current, Error, Railtie

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.currentObject



24
25
26
# File 'lib/activerecord_readonly.rb', line 24

def current
  RequestStore[:activerecord_readonly]
end

.except_class_namesObject



32
33
34
# File 'lib/activerecord_readonly.rb', line 32

def except_class_names
  current&.excepts || []
end

.only_class_namesObject



36
37
38
# File 'lib/activerecord_readonly.rb', line 36

def only_class_names
  current&.only || []
end

.readonly?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/activerecord_readonly.rb', line 28

def readonly?
  current&.be_readonly || false
end

.reset_readonly!Object



18
19
20
21
22
# File 'lib/activerecord_readonly.rb', line 18

def reset_readonly!
  RequestStore[:activerecord_readonly] = ActiverecordReadonly::Current.new(false,
                                                                           [],
                                                                           [])
end

.switch!(be_readonly = false, excepts: [], only: []) ⇒ Object



12
13
14
15
16
# File 'lib/activerecord_readonly.rb', line 12

def switch!(be_readonly = false, excepts: [], only: [])
  RequestStore[:activerecord_readonly] = ActiverecordReadonly::Current.new(be_readonly,
                                                                           excepts.map(&:to_s),
                                                                           only.map(&:to_s))
end

.with(be_readonly) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/activerecord_readonly.rb', line 40

def with(be_readonly)
  old = readonly?

  switch!(be_readonly)
  yield
  switch!(old)
end