Sha256: 8ce7efa875f969d470fb86eaf2ea9b11467c97a3ba7b0a3e14fce2760ac54ece

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module Cardboard
  class Setting < ActiveRecord::Base
    # self.table_name = "cardboard_settings"
    # attr_accessible :name, :fields_attributes

    has_many :fields, :as => :object_with_field
    accepts_nested_attributes_for :fields, :allow_destroy => true

    # thread save caching of the settings
    @lock = ::Mutex.new
    after_commit do
      Setting.clear_saved_settings
    end

    class << self
      def add(id, hash_attributes)
        myself = self.first_or_create
        field = myself.fields.where(identifier: id).first_or_initialize
        field.update_attributes!(hash_attributes)
      end

      def clear_saved_settings
        @lock.synchronize do
          @_settings = nil
        end
      end

      def method_missing(sym, *args, &block)
        begin
          super
        rescue
          @lock.synchronize do
            @_settings ||= {}
            @_settings[sym.to_sym] ||= begin
              return nil unless self.first
              s = self.first.fields.where(identifier: sym.to_s).first
              raise ::ActiveRecord::NoMethodError if s.nil?
              s.value
            end
          end
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cardboard_cms-0.1.8 app/models/cardboard/setting.rb
cardboard_cms-0.1.6 app/models/cardboard/setting.rb
cardboard_cms-0.1.4 app/models/cardboard/setting.rb