Skip to content

Commit

Permalink
Allow SettingObject to respond to try getters.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcferna committed Jun 24, 2016
1 parent 1792dc3 commit d807ce7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rails-settings/setting_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SettingObject < ActiveRecord::Base
REGEX_GETTER = /\A([a-z]\w+)\Z/i

def respond_to?(method_name, include_priv=false)
super || method_name.to_s =~ REGEX_SETTER
super || method_name.to_s =~ REGEX_SETTER || _setting?(method_name)
end

def method_missing(method_name, *args, &block)
Expand Down Expand Up @@ -76,5 +76,9 @@ def _set_value(name, v)
def _target_class
target_type.constantize
end

def _setting?(method_name)
_target_class.default_settings[var.to_sym].keys.include?(method_name.to_s)
end
end
end
6 changes: 6 additions & 0 deletions spec/setting_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
expect(new_setting_object.filter).to eq(true)
end

it "should return defaults when using `try`" do
expect(new_setting_object.try(:theme)).to eq('blue')
expect(new_setting_object.try(:view)).to eq('monthly')
expect(new_setting_object.try(:filter)).to eq(true)
end

it "should store different objects to value hash" do
new_setting_object.integer = 42
new_setting_object.float = 1.234
Expand Down

0 comments on commit d807ce7

Please sign in to comment.