Sha256: e76588030b01dfacc878a3382c4977875f1a9d896c0bc9e88f7d6a1113ea887f
Contents?: true
Size: 1.75 KB
Versions: 2
Compression:
Stored size: 1.75 KB
Contents
class Setting < ActiveRecord::Base belongs_to :settable, :polymorphic => true validates_presence_of :key, :kind validates :key, :uniqueness => {:scope => [:settable_id, :settable_type]} field :key index :key field :kind field :group field :integer_value, :integer field :float_value, :float field :text_value, :text field :string_value, :string field :datetime_value, :datetime field :date_value, :date has_attached :file_value def self.kinds [ :string, :integer, :float, :boolean, :text, :datetime, :date, :file ] end def value setting_kind = self.kind # this will break paperclip 'missing.png' feature, but at least # provides a way to check if an attachment is missing if setting_kind == "file" return nil if !file_value? end self.send("#{setting_kind}_value") end # eg. Setting.global("site-name") def self.global(key, opts = {}) autocreate = opts.delete(:autocreate) kind = opts.delete(:kind) || :string default = opts.delete(:default) group = opts.delete(:group) || "global" options = opts.delete(:options) s = autocreate ? self.find_or_create_by_key_and_settable_id(key.to_s, nil, :kind => kind, :"#{kind}_value" => default, :group => group, :options => options) : self.find_by_key_and_settable_id(key.to_s, nil) if s s.value || default end end def self.update_global(key, value) s = self.find_by_key_and_settable_id(key.to_s, nil) if s s.update_attributes(:value => value) end end field :options, :text serialize :options, Hash # after_initialize do |record| # record.options ||= {} # end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
fullstack-cms-0.1.17 | app/models/setting.rb |
fullstack-cms-0.1.15 | app/models/setting.rb |