Sha256: bf2f1bf01abcfd1421cf5198b6f8d736c5a95eaf31338ff9e7b533be791658e0

Contents?: true

Size: 1.39 KB

Versions: 16

Compression:

Stored size: 1.39 KB

Contents

class RefinerySetting < ActiveRecord::Base
  class SettingNotFound < RuntimeError; end

  validates_presence_of :name
  validates_uniqueness_of :name

  serialize :value

  def title
    self.name.titleize
  end

  # internals

  def self.method_missing(method, *args)
    method_name = method.to_s
    super(method, *args)

  rescue NoMethodError
    if method_name =~ /=$/
      self[method_name.gsub('=', '')] = args.first
    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
  end

  def self.[](name)
    self.find_by_name(name.to_s).value rescue nil
  end

  def self.[]=(name, value)
    setting = find_or_create_by_name(name.to_s)
    setting.value = value
    setting.save!
  end

  REPLACEMENTS = {"true" => true, "false" => false}

  def value
    _value = self[:value]

   unless _value.nil?
      REPLACEMENTS.each do |current_value, new_value|
        _value = new_value if _value == current_value
      end
      _value = _value.to_i if _value.to_i.to_s == _value rescue _value
    end

    return _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

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
refinerycms-0.9.5.29 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.28 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.27 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.26 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.25 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.24 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.23 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.22 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.21 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.20 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.19 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.18 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.17 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.16 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.15 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.14 vendor/plugins/refinery_settings/app/models/refinery_setting.rb