Sha256: 0db32fe643d4ca6ef99929554a24b83c598cf28d145db74b3ce9ee9d9d7b9d55
Contents?: true
Size: 998 Bytes
Versions: 1
Compression:
Stored size: 998 Bytes
Contents
require 'active_support/concern' module Flavors module Preferences extend ::ActiveSupport::Concern module ClassMethods def preference(name, options = {}, &callback) has_many :preferences, :as => :prefered, :class_name => "::Flavors::Preference" define_method(name) do read_preference(name, options[:default]) end define_method("#{name}?") do read_preference(name, options[:default]) end define_method("#{name}=") do |value| write_preference(name, value) callback.call(self, value) if callback end end end def read_preference(name, default = nil) if p = self.preferences.where(:name => name).first p.value elsif default.present? default else nil end end def write_preference(name, value) p = self.preferences.where(:name => name).first_or_create p.update_attribute(:value, value) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
flavors-0.4.0 | lib/flavors/preferences/preferences.rb |