Created
May 23, 2012 16:06
-
-
Save jacob-s-son/2776128 to your computer and use it in GitHub Desktop.
Simple active record class to store in database some application settings adjustable by users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationSetting < ActiveRecord::Base | |
belongs_to :setting_type | |
validates_presence_of :code, :name, :value, :setting_type | |
validates_uniqueness_of :code | |
attr_accessible :code, :name, :value, :setting_type, :setting_type_id | |
class << self | |
#for usage like ApplicationSetting["some_setting_code"] | |
def [] key | |
find_by_code(key).try :value | |
end | |
end | |
def value | |
#setting type could be decimal, integer, string | |
case setting_type.name | |
when "decimal" then BigDecimal( read_attribute(:value).to_s ) | |
when "integer" then read_attribute(:value).to_i | |
else read_attribute(:value) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class ApplicationSettingTest < ActiveSupport::TestCase | |
test "should return value of a setting when setting's coe is passed in []" do | |
assert_equal ApplicationSetting["SUFFICIENT_USER_LEVEL"], 8 #this setting is seeded in our application | |
end | |
test "should return nil if no setting found" do | |
assert_equal ApplicationSetting["lala"], nil | |
end | |
test "should convert value string to interger, if setting_type is integer" do | |
as = ApplicationSetting.new(:code => "code", :name => "name", :setting_type => SettingType.find_by_name("integer"), :value => "5") | |
assert_equal as.value.class, Fixnum | |
assert_equal as.value, 5 | |
end | |
test "should convert value string to BigDecimal, if setting_type is decimal" do | |
as = ApplicationSetting.new(:code => "code", :name => "name", :setting_type => SettingType.find_by_name("decimal"), :value => "5.78") | |
assert_equal as.value.class, BigDecimal | |
assert_equal as.value, 5.78 | |
end | |
test "should return plain value if setting_type is other than decimal or integer" do | |
as = ApplicationSetting.new(:code => "code", :name => "name", :setting_type => SettingType.find_by_name("string"), :value => "5.78") | |
assert_equal as.value.class, String | |
assert_equal as.value, "5.78" | |
end | |
test "should return nil if value attribute is nil" do | |
as = ApplicationSetting.new(:code => "code", :name => "name", :setting_type => SettingType.find_by_name("string"), :value => nil) | |
assert_equal as.value.class, NilClass | |
assert_equal as.value, nil | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateApplicationSettings < ActiveRecord::Migration | |
def change | |
create_table :application_settings do |t| | |
t.integer :setting_type_id | |
t.string :code | |
t.string :value | |
t.string :name | |
t.timestamps | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateSettingTypes < ActiveRecord::Migration | |
def change | |
create_table :setting_types do |t| | |
t.string :name | |
t.timestamps | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SettingsFkIndeces < ActiveRecord::Migration | |
def up | |
add_index :application_settings, :code, :unique => true | |
add_index :application_settings, :setting_type_id | |
end | |
def down | |
remove_index :application_settings, :column_name =>:code | |
remove_index :application_settings, :column_name =>:setting_type_id | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SettingType < ActiveRecord::Base | |
has_many :application_settings | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment