stex / setting_accessors

Global key-value-store and database-persisted attribute accessors

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add a "touch" option for ActiveRecord integration

martingregoire opened this issue · comments

Enhancement request: what about an option "touch", similar to Rails "touch" option in belongs_to associations, added for ActiveRecord integration?

From http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to:

:touch
If true, the associated object will be touched (the updated_at/on attributes set to current time) when this record is either saved or destroyed. If you specify a symbol, that attribute will be updated with the current time in addition to the updated_at/on attribute.

Concept:

class MyModel < ActiveRecord::Base
  setting_accessor :a_string
  setting_accessor :another_string, :touch => true
end

MyModel.first.update_attributes({:a_string => 'oh hi there'})
#=> will change the setting and the settings `updated_at`

MyModel.first.update_attributes({:another_string => 'saywut?'})
#=> will change the setting and the settings `updated_at`, and also MyModel.first's `updated_at`

I think this would be a better solution than:

class MyModel < ActiveRecord::Base
  setting_accessor :a_string
  before_save :dirty_updated_at, :if => -> { a_string_changed? }

  private
    def dirty_updated_at
      touch
    end
end

This will be fixed with #10, every normal update will automatically trigger a timestamp update to mimic the behavior on normal attribute changes.