Sha256: 77c42a5fbba7ce11f149d9578d4bbb3b326863429b6f506009cf683995949cab

Contents?: true

Size: 1.94 KB

Versions: 18

Compression:

Stored size: 1.94 KB

Contents

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)
      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

18 entries across 18 versions & 3 rubygems

Version Path
shoppe-1.1.1 app/models/shoppe/setting.rb
shoppe-1.1.0 app/models/shoppe/setting.rb
shoppe-1.0.9 app/models/shoppe/setting.rb
shoppe-1.0.8 app/models/shoppe/setting.rb
kylekthompson-shoppe-1.0.7 app/models/shoppe/setting.rb
shoppe-1.0.7 app/models/shoppe/setting.rb
shoppe-1.0.6 app/models/shoppe/setting.rb
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/shoppe-1.0.5/app/models/shoppe/setting.rb
shoppe-1.0.5 app/models/shoppe/setting.rb
shoppe-1.0.3 app/models/shoppe/setting.rb
shoppe-1.0.2 app/models/shoppe/setting.rb
shoppe-1.0.1 app/models/shoppe/setting.rb
shoppe-1.0.0 app/models/shoppe/setting.rb
shoppe-0.0.21 app/models/shoppe/setting.rb
shoppe-0.0.20 app/models/shoppe/setting.rb
shoppe-0.0.19 app/models/shoppe/setting.rb
shoppe-0.0.18 app/models/shoppe/setting.rb
shoppe-0.0.17 app/models/shoppe/setting.rb