Sha256: 0d9a0dc889075236d4add786682e38a42b33a9789becd4bbc977d197603c6ff2

Contents?: true

Size: 1.95 KB

Versions: 9

Compression:

Stored size: 1.95 KB

Contents

require 'ostruct'

module KktShoppe
  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("kkt_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)
      when 'Boolean'        then  value == 'true' ? true : false
      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
          value.blank? ? existing.destroy! : existing.update!(:value => value)
        else
          value.blank? ? nil : self.create!(:key => key, :value => value)
        end
      end
      hash
    end
    
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
kkt_shoppe-2.0.2 app/models/kkt_shoppe/setting.rb
kkt_shoppe-2.0.1 app/models/kkt_shoppe/setting.rb
kkt_shoppe-2.0.0 app/models/kkt_shoppe/setting.rb
kkt_shoppe-1.3.0 app/models/kkt_shoppe/setting.rb
kkt_shoppe-1.2.1 app/models/kkt_shoppe/setting.rb
kkt_shoppe-1.2.0 app/models/kkt_shoppe/setting.rb
kkt_shoppe-1.1.2 app/models/kkt_shoppe/setting.rb
kkt_shoppe-1.1.1 app/models/kkt_shoppe/setting.rb
kkt_shoppe-1.1.0 app/models/kkt_shoppe/setting.rb