Skip to content

Commit e4ad1f7

Browse files
committed
made SiteSetting predicates generated
1 parent a3952dd commit e4ad1f7

3 files changed

Lines changed: 32 additions & 38 deletions

File tree

app/models/site_setting.rb

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,46 +44,23 @@ def self.exist?(name)
4444
SiteSetting.where(name: name).any?
4545
end
4646

47-
# Checks whether the setting is a global site setting
48-
# @return [Boolean]
47+
# Is the setting global?
48+
# @return [Boolean] check result
4949
def global?
5050
community_id.nil?
5151
end
5252

53-
# Is the setting array-valued?
54-
# @return [Boolean] check result
55-
def array?
56-
value_type.downcase == 'array'
57-
end
58-
59-
# Is the setting boolean-valued?
60-
# @return [Boolean] check result
61-
def boolean?
62-
value_type.downcase == 'boolean'
63-
end
64-
65-
# Is the setting floating point number-valued?
66-
# @return [Boolean] check result
67-
def float?
68-
value_type.downcase == 'float'
69-
end
70-
71-
# Is the setting integer-valued?
72-
# @return [Boolean] check result
73-
def integer?
74-
value_type.downcase == 'integer'
75-
end
76-
77-
# Is the setting string-valued (plain text)?
78-
# @return [Boolean] check result
79-
def string?
80-
value_type.downcase == 'string'
53+
# Defines predicates for each value type
54+
[:array, :boolean, :float, :integer, :string, :text].each do |method|
55+
define_method "#{method}?" do
56+
value_type.downcase.to_sym == method
57+
end
8158
end
8259

83-
# Is the setting text-valued (HTML-aware text)?
60+
# Is the setting numeric-valued?
8461
# @return [Boolean] check result
85-
def text?
86-
value_type.downcase == 'text'
62+
def numeric?
63+
float? || integer?
8764
end
8865

8966
def typed

test/fixtures/site_settings.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ int:
1313
value: 42
1414
value_type: integer
1515

16+
float:
17+
name: SettingWithFloatValue
18+
value: 3.14
19+
value_type: float
20+
21+
string:
22+
name: SettingWithStringValue
23+
value: plain text value
24+
value_type: string
25+
1626
text:
1727
name: SettingWithTextValue
1828
value: "<p>a paragraph of text</p>"

test/models/site_setting_test.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,18 @@ class SiteSettingTest < ActiveSupport::TestCase
5454
assert_equal SiteSetting.where(name: 'test').first, setting1
5555
end
5656

57-
test 'text? should correctly check if the setting accepts long text values' do
58-
text_setting = site_settings(:text)
59-
int_setting = site_settings(:int)
57+
test 'type predicates should correctly check the setting\'s value type' do
58+
[:array, :boolean, :float, :integer, :string, :text].each do |method|
59+
site_settings.each do |setting|
60+
assert_equal setting.value_type == method.to_s, setting.send("#{method}?")
61+
end
62+
end
63+
end
6064

61-
assert_equal text_setting.text?, true
62-
assert_equal int_setting.text?, false
65+
test 'numeric? should correctly check if the setting\'s value is numeric' do
66+
assert site_settings(:int).numeric?
67+
assert site_settings(:float).numeric?
68+
assert_not site_settings(:string).numeric?
69+
assert_not site_settings(:text).numeric?
6370
end
6471
end

0 commit comments

Comments
 (0)