Sha256: f150797e9407cf477d6ab97307b2614e603933c5347ef389a963ea5b2ed07dcc

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 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 :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)
    s = self.find_by_key_and_settable_id(key.to_s, nil)
    if s
      s.value
    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


end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fullstack-cms-0.1.5 app/models/setting.rb
fullstack-cms-0.1.4 app/models/setting.rb
fullstack-cms-0.1.3 app/models/setting.rb
fullstack-cms-0.1.2 app/models/setting.rb
fullstack-cms-0.1.1 app/models/setting.rb