vendor/plugins/refinery_settings/app/models/refinery_setting.rb in refinerycms-0.9.6.7 vs vendor/plugins/refinery_settings/app/models/refinery_setting.rb in refinerycms-0.9.6.8

- old
+ new

@@ -1,19 +1,26 @@ class RefinerySetting < ActiveRecord::Base - class SettingNotFound < RuntimeError; end validates_presence_of :name validates_uniqueness_of :name - serialize :value - + serialize :value # stores into YAML format + + # Number of settings to show per page when using will_paginate + def self.per_page + 10 + end + + # prettier version of the name. + # site_name becomes Site Name def title self.name.titleize end - # internals - + # Access method that allows dot notation to work. + # say you had a setting called "site_name". You could access that by going RefinerySetting[:site_name] + # but with this you can also access that by going RefinerySettting.site_name def self.method_missing(method, *args) method_name = method.to_s super(method, *args) rescue NoMethodError @@ -22,12 +29,13 @@ else self[method_name] end end - def self.find_or_set(name, or_this_value) - setting_value = find_or_create_by_name(:name => name.to_s, :value => or_this_value).value + def self.find_or_set(name, the_value) + setting = find_or_create_by_name(:name => name.to_s, :value => the_value) + setting.value end def self.[](name) self.find_by_name(name.to_s).value rescue nil end @@ -36,31 +44,45 @@ setting = find_or_create_by_name(name.to_s) setting.value = value setting.save! end + + + # Below is not very nice, but seems to be required + # The problem is when Rails serialises a fields like booleans + # it doesn't retreieve it back out as a boolean + # it just returns a string. This code maps the two boolean + # values correctly so a boolean is returned REPLACEMENTS = {"true" => true, "false" => false} def value - _value = self[:value] + current_value = self[:value] - unless _value.nil? - REPLACEMENTS.each do |current_value, new_value| - _value = new_value if _value == current_value + unless current_value.nil? + # This bit handles true and false so that true and false are actually returned + # not "0" and "1" + REPLACEMENTS.each do |current, new_value| + current_value = new_value if current_value == current end - _value = _value.to_i if _value.to_i.to_s == _value rescue _value + + # converts the serialised value back to an integer + # if the value is an integer + begin + if current_value.to_i.to_s == current_value + current_value = current_value.to_i + end + rescue + current_value + end end - return _value + return current_value end def value=(new_value) # must convert to string if true or false supplied otherwise it becomes 0 or 1, unfortunately. new_value = new_value.to_s if ["trueclass","falseclass"].include?(new_value.class.to_s.downcase) self[:value] = new_value end - def self.per_page - 10 - end - -end +end \ No newline at end of file