Sha256: fe7056b0a3a41d4fe25b670809a053f30b69a99144a5dff1ef3f15d67f34b3e7

Contents?: true

Size: 1.34 KB

Versions: 28

Compression:

Stored size: 1.34 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

28 entries across 28 versions & 1 rubygems

Version Path
refinerycms-0.9.5.13 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.12 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.11 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.10 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.9 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.8 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.7 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.6 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.5 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.4 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.3 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.2 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5.1 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.5 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.4.5 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.4.4 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.4.3 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.4.2 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.4.1 vendor/plugins/refinery_settings/app/models/refinery_setting.rb
refinerycms-0.9.4 vendor/plugins/refinery_settings/app/models/refinery_setting.rb