app/models/shoppe/setting.rb in shoppe-0.0.16 vs app/models/shoppe/setting.rb in shoppe-0.0.17
- old
+ new
@@ -1,29 +1,34 @@
require 'ostruct'
module Shoppe
class Setting < ActiveRecord::Base
+ # Validations
validates :key, :presence => true, :uniqueness => true
validates :value, :presence => true
validates :value_type, :presence => true
before_validation do
self.value_type = I18n.t("shoppe.settings.types")[self.key.to_sym].try(:capitalize) || self.value.class.to_s
self.value = encoded_value
end
# The encoded value for saving in the backend (as a string)
+ #
+ # @return [String]
def encoded_value
case value_type
when 'Array', 'Hash' then value.to_json
when 'Boolean' then value.to_s == 'true' ? 'true' : 'false'
else value.to_s
end
end
# The decoded value for the setting attribute (in it's native type)
+ #
+ # @return [Object]
def decoded_value
case value_type
when 'Fixnum' then value.to_i
when 'Float' then value.to_f
when 'Array', 'Hash' then JSON.parse(value)
@@ -31,18 +36,22 @@
else value.to_s
end
end
# A full hash of all settings available in the current scope
+ #
+ # @return [Hash]
def self.to_hash
all.inject({}) do |h, setting|
h[setting.key.to_s] = setting.decoded_value
h
end
end
# Update settings from a given hash and persist them. Accepts a
# hash of keys (which should be strings).
+ #
+ # @return [Hash]
def self.update_from_hash(hash)
existing_settings = self.all.to_a
hash.each do |key, value|
existing = existing_settings.select { |s| s.key.to_s == key.to_s }.first
if existing