Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: doximity/rails-settings
base: 3605e3af06
...
head fork: doximity/rails-settings
compare: 873a2bd46d
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 25, 2012
@jeyb jeyb The updated_at time on User should reflect the last settings
change.

We rely on updated_at time to cache User objects, performing a touch
on the User model is necessary on updates to Settings. This will ensure
our cache is always fresh.
2daff1b
@jeyb jeyb Merge pull request #1 from doximity/touch
The updated_at time on User should reflect the last settings change.
873a2bd
View
11 lib/rails-settings/settings.rb
@@ -41,6 +41,17 @@ def self.method_missing(method, *args)
end
end
end
+
+ after_save do |settings|
+ case settings.target_type
+ when 'User'
+ User.find_by_id(settings.target_id).try(:touch)
+ when 'Account'
+ Account.find_by_id(settings.target_id).try(:user).try(:touch)
+ end
+
+ true
+ end
#destroy the specified settings record
def self.destroy(var_name)
View
19 test/settings_test.rb
@@ -113,6 +113,25 @@ def test_delete_settings_after_destroying_target
user2.destroy
assert_equal before_count - 1, Settings.count
end
+
+ def test_save_on_settings_targetting_user
+ user = User.create :name => 'Mr. Foo'
+ updated_at = user.updated_at.to_i
+
+ sleep 1
+ user.settings.example = 42
+ assert user.reload.updated_at.to_i > updated_at
+ end
+
+ def test_save_on_settings_targetting_account
+ account = Account.create :login => 'name@email.com'
+ user = User.create :name => 'Mr. Foo', :account => account
+ updated_at = user.updated_at.to_i
+
+ sleep 1
+ user.settings.example = 42
+ assert user.reload.updated_at.to_i > updated_at
+ end
def test_all
assert_equal({ "test2" => "bar", "test" => "foo" }, Settings.all)
View
15 test/test_helper.rb
@@ -12,13 +12,18 @@
class User < ActiveRecord::Base
has_settings
+ belongs_to :account
+end
+
+class Account < ActiveRecord::Base
+ has_one :user
end
def setup_db
ActiveRecord::Schema.define(:version => 1) do
create_table :settings do |t|
t.string :var, :null => false
- t.text :value, :null => true
+ t.text :value, :null => true
t.integer :target_id, :null => true
t.string :target_type, :limit => 30, :null => true
t.timestamps
@@ -27,8 +32,14 @@ def setup_db
create_table :users do |t|
t.string :name
+ t.belongs_to :account
+ t.timestamps
+ end
+
+ create_table :accounts do |t|
+ t.string :login
end
end
end
-puts "Testing with ActiveRecord #{ActiveRecord::VERSION::STRING}"

No commit comments for this range

Something went wrong with that request. Please try again.