Module: Eventsimple::Readonly
- Defined in:
- lib/eventsimple/readonly.rb
Overview
Closes gaps where writes bypass readonly! (instance helpers like delete, update_column, relation SQL like upsert / delete_all / update_all). Raises ActiveRecord::ReadOnlyRecord unless enable_writes! is active.
Defined Under Namespace
Modules: RelationMethods
Constant Summary collapse
- THREAD_KEY =
:eventsimple_class_writes_depth
Class Method Summary collapse
- .class_writes_enabled? ⇒ Boolean
- .enable_writes! ⇒ Object
- .install_relation_guards!(model_class) ⇒ Object
Instance Method Summary collapse
- #decrement! ⇒ Object
- #delete ⇒ Object
- #enable_writes!(&_block) ⇒ Object
- #increment! ⇒ Object
- #readonly? ⇒ Boolean
- #toggle! ⇒ Object
- #touch ⇒ Object
- #update_attribute ⇒ Object
- #update_attribute! ⇒ Object
- #update_column ⇒ Object
- #update_columns ⇒ Object
Class Method Details
.class_writes_enabled? ⇒ Boolean
54 55 56 |
# File 'lib/eventsimple/readonly.rb', line 54 def class_writes_enabled? Thread.current[THREAD_KEY].to_i.positive? end |
.enable_writes! ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/eventsimple/readonly.rb', line 46 def enable_writes! Thread.current[THREAD_KEY] = Thread.current[THREAD_KEY].to_i + 1 yield ensure d = Thread.current[THREAD_KEY].to_i - 1 Thread.current[THREAD_KEY] = d.positive? ? d : nil end |
.install_relation_guards!(model_class) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/eventsimple/readonly.rb', line 37 def install_relation_guards!(model_class) ActiveRecord::Delegation.delegated_classes.each do |relation_base| delegate_class = model_class.relation_delegate_class(relation_base) next if delegate_class.ancestors.include?(RelationMethods) delegate_class.prepend(RelationMethods) end end |
Instance Method Details
#decrement! ⇒ Object
101 102 103 104 |
# File 'lib/eventsimple/readonly.rb', line 101 def decrement!(...) _raise_readonly_record_error if readonly? super end |
#delete ⇒ Object
71 72 73 74 |
# File 'lib/eventsimple/readonly.rb', line 71 def delete _raise_readonly_record_error if readonly? super end |
#enable_writes!(&_block) ⇒ Object
65 66 67 68 69 |
# File 'lib/eventsimple/readonly.rb', line 65 def enable_writes!(&_block) raise DeprecatedInstanceEnableWrites, <<~MSG.squish Use Eventsimple.enable_writes! { ... } instead of #{self.class.name}#enable_writes! MSG end |
#increment! ⇒ Object
96 97 98 99 |
# File 'lib/eventsimple/readonly.rb', line 96 def increment!(...) _raise_readonly_record_error if readonly? super end |
#readonly? ⇒ Boolean
59 60 61 62 63 |
# File 'lib/eventsimple/readonly.rb', line 59 def readonly? return false if Readonly.class_writes_enabled? super end |
#toggle! ⇒ Object
106 107 108 109 |
# File 'lib/eventsimple/readonly.rb', line 106 def toggle!(...) _raise_readonly_record_error if readonly? super end |
#touch ⇒ Object
111 112 113 114 |
# File 'lib/eventsimple/readonly.rb', line 111 def touch(...) _raise_readonly_record_error if readonly? super end |
#update_attribute ⇒ Object
86 87 88 89 |
# File 'lib/eventsimple/readonly.rb', line 86 def update_attribute(...) _raise_readonly_record_error if readonly? super end |
#update_attribute! ⇒ Object
91 92 93 94 |
# File 'lib/eventsimple/readonly.rb', line 91 def update_attribute!(...) _raise_readonly_record_error if readonly? super end |
#update_column ⇒ Object
76 77 78 79 |
# File 'lib/eventsimple/readonly.rb', line 76 def update_column(...) _raise_readonly_record_error if readonly? super end |
#update_columns ⇒ Object
81 82 83 84 |
# File 'lib/eventsimple/readonly.rb', line 81 def update_columns(...) _raise_readonly_record_error if readonly? super end |