Skip to content

Commit 29c1ddc

Browse files
author
Jesús Burgos
committed
Ensure becomes is always used, also on new records
1 parent 808252b commit 29c1ddc

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

lib/rails-settings/base.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def settings(var)
1313
raise ArgumentError.new("Unknown key: #{var}") unless self.class.setting_keys[var]
1414

1515
fetch_settings_record(var)
16+
.becomes(self.class.setting_keys[var][:class_name].constantize)
1617
end
1718

1819
def settings=(value)
@@ -44,22 +45,14 @@ def fetch_settings_record(var)
4445
end
4546

4647
def find_settings_record(var)
47-
setting_objects
48-
.select { |s| s.var == var.to_s }
49-
.map { |s| s.becomes self.class.setting_keys[var][:class_name].constantize }
50-
.first
48+
setting_objects.detect { |s| s.var == var.to_s }
5149
end
5250

5351
def build_settings_record(var)
54-
build_args =
55-
if RailsSettings.can_protect_attributes?
56-
[{ :var => var.to_s }, :without_protection => true]
57-
else
58-
[:var => var.to_s, :target => self]
59-
end
60-
61-
setting_objects.build(*build_args) do |record|
62-
record.becomes self.class.setting_keys[var][:class_name].constantize
52+
if RailsSettings.can_protect_attributes?
53+
setting_objects.build({ :var => var.to_s }, :without_protection => true)
54+
else
55+
setting_objects.build(:var => var.to_s, :target => self)
6356
end
6457
end
6558
end

spec/settings_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
end
3333

3434
describe 'Objects' do
35+
context "settings should be an instance of :class_name" do
36+
it "should be an instance of 'SettingObject' by default" do
37+
expect(User.new.settings(:dashboard)).to be_a(RailsSettings::SettingObject)
38+
end
39+
40+
it "should be an instance of 'ProjectSettingObject' if defined" do
41+
expect(Project.new.settings(:info)).to be_a(ProjectSettingObject)
42+
end
43+
end
44+
3545
context 'without defaults' do
3646
let(:account) { Account.new :subdomain => 'foo' }
3747

0 commit comments

Comments
 (0)