Skip to content

Commit

Permalink
Merge branch 'release/0.0.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
stex committed Oct 1, 2015
2 parents f3cc5e5 + 56e21f6 commit 28223cb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
18 changes: 15 additions & 3 deletions lib/setting_accessors/accessor.rb
Expand Up @@ -16,6 +16,18 @@ def [](key)
has_key?(key) ? @temp_settings[key.to_sym] : SettingAccessors.setting_class.get(key, @record)
end

#
# Tries to fetch a setting value using the provided key and #[].
# It will only return the +default+ value if there is
# - no temporary setting with the given key AND
# - no already persisted setting (see #[])
#
def fetch(key, default = nil)
result = self[key]
return default if result.nil? && !has_key?(key)
result
end

def has_key?(key)
@temp_settings.has_key?(key.to_sym)
end
Expand All @@ -34,23 +46,23 @@ def []=(key, val)
# specified in the setting config file.
#
def get_or_default(key)
self[key] || SettingAccessors.setting_class.get_or_default(key, @record)
fetch key, SettingAccessors.setting_class.get_or_default(key, @record)
end

#
# Tries to find a setting for this record first.
# If none is found, tries to find a global setting with the same name
#
def get_or_global(key)
self[key] || SettingAccessors.setting_class.get(key)
fetch key, SettingAccessors.setting_class.get(key)
end

#
# Tries to find a setting for this record first,
# if none is found, it will return the given value instead.
#
def get_or_value(key, value)
self.has_key?(key) ? self[key] : value
fetch key, value
end

def get_with_fallback(key, fallback = nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/setting_accessors/version.rb
@@ -1,3 +1,3 @@
module SettingAccessors
VERSION = '0.0.7'
VERSION = '0.0.8'
end
2 changes: 2 additions & 0 deletions test/dummy/app/models/user.rb
Expand Up @@ -8,6 +8,8 @@ class User < ActiveRecord::Base

setting_accessor :a_boolean, :fallback => false

setting_accessor :class_wise_truthy_boolean, :type => :boolean, :default => true, :fallback => :default

setting_accessor :class_wise_with_value_fallback, :type => :string, :fallback => 'Oiski Poiski!'

setting_accessor :class_wise_with_default_fallback, :type => :string, :fallback => :default, :default => 'Kapitanski'
Expand Down
12 changes: 12 additions & 0 deletions test/dummy/test/models/setting_test.rb
Expand Up @@ -128,4 +128,16 @@ class SettingTest < ActiveSupport::TestCase
assert_equal 'Kapitanski', User.new.class_wise_with_default_fallback
end
end

context 'Boolean settings' do
should 'respect temporary settings which are set to `false`' do
user = User.new(:class_wise_truthy_boolean => false)
assert_equal user.class_wise_truthy_boolean, false
end

should 'respect temporary settings which are set to `true`' do
user = User.new(:a_boolean => true)
assert_equal user.a_boolean, true
end
end
end
1 change: 0 additions & 1 deletion test/dummy/test/models/user_test.rb
Expand Up @@ -17,6 +17,5 @@ class UserTest < ActiveSupport::TestCase
assert_equal @user.as_json[setting_name.to_s], @user.send(setting_name)
end
end

end
end

0 comments on commit 28223cb

Please sign in to comment.