Module: SettingAccessors::Integration
- Defined in:
- lib/setting_accessors/integration.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #_update_record ⇒ Object
- #as_json(options = {}) ⇒ Object
-
#changed_attributes ⇒ Hash
Adds changed settings to ActiveModel’s list of changed attributes.
-
#reload ⇒ Object
Previously read setting values have to be refreshed if a record is reloaded.
- #settings ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/setting_accessors/integration.rb', line 5 def self.included(base) # After the main record was saved, we can save its settings. # This is necessary as the record might have been a new record # without an ID yet base.after_save do settings.send(:persist!) # From AR 5.1 on, #_update actually checks whether the changed "attributes" are actually # table columns or not. If no actual changed columns were found, the record is not changed and # only the after_* callbacks are executed. # This means that the settings are persisted, but the record's +updated_at+ column is not updated. # # This workaround triggers a #touch on the record in case no actual column change already # triggered a timestamp update. # # TODO: This might lead to #after_commit being triggered twice, once by #update_* and once by #touch touch if @_setting_accessors_touch_assignable @_setting_accessors_touch_assignable = false end base.extend ClassMethods end |
Instance Method Details
#_update_record ⇒ Object
83 84 85 86 87 88 |
# File 'lib/setting_accessors/integration.rb', line 83 def _update_record(*) super.tap do |affected_rows| # Workaround to trigger a #touch if necessary, see +after_save+ callback further up @_setting_accessors_touch_assignable = affected_rows.zero? end end |
#as_json(options = {}) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/setting_accessors/integration.rb', line 91 def as_json( = {}) super.tap do |json| SettingAccessors::Internal.json_setting_names(self.class, **).each do |setting_name| json[setting_name.to_s] = send(setting_name) end end end |
#changed_attributes ⇒ Hash
Adds changed settings to ActiveModel’s list of changed attributes. This is necessary for #changed? to work correctly without actually overriding the method itself.
TODO: Check if it makes more sense to hook into AR5’s AttributeMutationTracker instead
72 73 74 |
# File 'lib/setting_accessors/integration.rb', line 72 def changed_attributes super.merge(settings.changed_settings) end |
#reload ⇒ Object
Previously read setting values have to be refreshed if a record is reloaded. Without this, #reload’ing a record would not update its setting values to the latest database version if they were previously read.
Example to demonstrate the problem with this override:
user = User.create(:a_boolean => true)
user_alias = User.find(user.id)
user.a_boolean = !user_alias.a_boolean
user.save
user_alias.reload
user_alias.a_boolean
#=> true
59 60 61 |
# File 'lib/setting_accessors/integration.rb', line 59 def reload(*) super.tap { @settings = nil } end |
#settings ⇒ Object
99 100 101 |
# File 'lib/setting_accessors/integration.rb', line 99 def settings @settings ||= SettingAccessors::SettingSet.new(self) end |