Skip to content

Commit 808252b

Browse files
author
Jesús Burgos
committed
Fixes some problems caught by tests
1 parent c406ad8 commit 808252b

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

lib/rails-settings.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ module RailsSettings
55
def self.can_protect_attributes?
66
(ActiveRecord::VERSION::MAJOR == 3) || defined?(ProtectedAttributes)
77
end
8+
9+
def self.can_use_becomes?
10+
![
11+
[3, 0],
12+
[3, 1],
13+
[3, 2],
14+
[4, 0]
15+
].include?([ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR])
16+
end
817
end
918

1019
require 'rails-settings/setting_object'
@@ -20,4 +29,3 @@ def self.has_settings(*args, &block)
2029
extend RailsSettings::Scopes
2130
end
2231
end
23-

lib/rails-settings/base.rb

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

15-
fetch_settings_record(var).becomes(self.class.setting_keys[var][:class_name].constantize)
15+
fetch_settings_record(var)
1616
end
1717

1818
def settings=(value)
@@ -44,14 +44,22 @@ def fetch_settings_record(var)
4444
end
4545

4646
def find_settings_record(var)
47-
setting_objects.detect { |s| s.var == var.to_s }
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
4851
end
4952

5053
def build_settings_record(var)
51-
if RailsSettings.can_protect_attributes?
52-
setting_objects.build({ :var => var.to_s }, :without_protection => true)
53-
else
54-
setting_objects.build(:var => var.to_s, :target => self)
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
5563
end
5664
end
5765
end

lib/rails-settings/setting_object.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ def respond_to?(method_name, include_priv=false)
2828
super || method_name.to_s =~ REGEX_SETTER || _setting?(method_name)
2929
end
3030

31+
# Annoying hack for old Rails
32+
unless RailsSettings.can_use_becomes?
33+
def becomes(klass)
34+
became = super(klass)
35+
became.instance_variable_set("@changed_attributes", @changed_attributes)
36+
became
37+
end
38+
end
39+
3140
def method_missing(method_name, *args, &block)
3241
if block_given?
3342
super
@@ -78,7 +87,12 @@ def _target_class
7887
end
7988

8089
def _setting?(method_name)
81-
_target_class.setting_keys[var.to_sym][:default_value].keys.include?(method_name.to_s)
90+
return false if target_id.nil? || target_type.nil?
91+
92+
_target_class
93+
.setting_keys[var.to_sym][:default_value]
94+
.keys
95+
.include?(method_name.to_s)
8296
end
8397
end
8498
end

0 commit comments

Comments
 (0)