Sha256: 8888e0d7ba6f7b38ad2d37a16abe5cbc97c58545d056c00cc2bac2506c31d659

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module Cardboard
  class Setting < ActiveRecord::Base

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

    serialize :template, Hash


    # 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

1 entries across 1 versions & 1 rubygems

Version Path
cardboard_cms-0.3.1 app/models/cardboard/setting.rb