Sha256: 1b62d3e6b9969b9f91afb2599e82b863be682d423498d817e310e4312b31a3cb

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

class ActiveadminSettings::Setting
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Globalize

  # Fields
  field :name
  
  translates do
    field :string, :default => ""
    fallbacks_for_empty_translations!
  end

  mount_uploader  :file, ActiveadminSettings::SettingsFileUploader

  # Validators
  validates_presence_of   :name
  validates_uniqueness_of :name
  validates_length_of     :name, minimum: 1

  # Indexes
  index :name

  # Instance
  def type
    (ActiveadminSettings.all_settings[name]["type"] ||= "string").to_s
  end

  def description
    (ActiveadminSettings.all_settings[name]["description"] ||= "").to_s
  end

  def default_value
    val = (ActiveadminSettings.all_settings[name]["default_value"] ||= "").to_s
    
    if type == "file" and not val.include? '//'
      val = ActionController::Base.helpers.asset_path(val)
    end
    
    val
  end

  def value
    val = respond_to?(type) ? send(type).to_s : send(:string).to_s
    val = default_value if val.empty?
    val.html_safe
  end

  # Class
  def self.initiate_setting(name)
    s = self.new(name: name)
    if s.type == "text" or s.type == "html"
      s.string = s.default_value
    end
    s.save
    s
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
activeadmin-settings-0.3.3 app/models/activeadmin_settings/setting.rb
activeadmin-settings-0.3.2 app/models/activeadmin_settings/setting.rb
activeadmin-settings-0.3.1 app/models/activeadmin_settings/setting.rb
activeadmin-settings-0.3.0 app/models/activeadmin_settings/setting.rb
activeadmin-settings-0.2.9 app/models/activeadmin_settings/setting.rb