Skip to content
Browse files

Version to 0.2.4.

New method  to direct write default value into database. #16
  • Loading branch information...
1 parent 47c2de8 commit 855cc8c904a1d6a0a8ee0a41ce7c9f7906077ce5 @huacnlee committed
View
4 Changelogs.md
@@ -1,3 +1,7 @@
+## 0.3.0
+
+* Setting.save_default method to direct write default value in database.
+
## 0.2.3
* Fix bug with when key has cached a nil value, and then set a default value for that key, the default value can't right return.
View
18 README.md
@@ -87,18 +87,28 @@ Setting.all('preferences.')
```
Set defaults for certain settings of your app. This will cause the defined settings to return with the
-Specified value even if they are not in the database. Make a new file in `config/initializers/default_settings.rb`
+Specified value even if they are **not in the database**. Make a new file in `config/initializers/default_settings.rb`
with the following:
```ruby
Setting.defaults[:some_setting] = 'footastic'
+Setting.where(:var => "some_setting").count
+=> 0
+Setting.some_setting
+=> "footastic"
```
-
-Now even if the database is completely empty, you app will have some intelligent defaults:
+
+Init defualt value in database, this has indifferent with `Setting.defaults[:some_setting]`, this will **save the value into database**:
```ruby
-Setting.some_setting # returns 'footastic'
+Setting.save_default(:some_key) = "123"
+Setting.where(:var => "some_key").count
+=> 1
+Setting.some_key
+=> "123"
```
+
+
Settings may be bound to any existing ActiveRecord object. Define this association like this:
Notice! is not do caching in this version.
View
6 lib/rails-settings/cached_settings.rb
@@ -15,5 +15,11 @@ def self.[](var_name)
}
obj || @@defaults[var_name.to_s]
end
+
+ def self.save_default(key,value)
+ if self.send(key) == nil
+ self.send("#{key}=",value)
+ end
+ end
end
end
View
2 rails-settings-cached.gemspec
@@ -1,7 +1,7 @@
# coding: utf-8
Gem::Specification.new do |s|
s.name = %q{rails-settings-cached}
- s.version = "0.2.3"
+ s.version = "0.2.4"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Squeegy", "Georg Ledermann", "100hz", "Jason Lee"]
View
9 spec/rails-settings-cached/setting_spec.rb
@@ -77,6 +77,15 @@
Setting.defaults[:has_cached_nil_key] = "123"
Setting.has_cached_nil_key.should == "123"
end
+
+ it "#save_default" do
+ Setting.test_save_default_key
+ Setting.save_default(:test_save_default_key, "321")
+ Setting.where(:var => "test_save_default_key").count.should == 1
+ Setting.test_save_default_key.should == "321"
+ Setting.save_default(:test_save_default_key, "3211")
+ Setting.test_save_default_key.should == "321"
+ end
end
describe "Implementation by embeds a Model" do

0 comments on commit 855cc8c

Please sign in to comment.
Something went wrong with that request. Please try again.