Class: Preference
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Preference
- Defined in:
- app/models/users/preference.rb
Overview
Copyright © 2008-2013 Michael Dvorkin and contributors.
Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or www.opensource.org/licenses/mit-license.php
Schema Information
Table name: preferences
id :integer not null, primary key
user_id :integer
name :string(32) default(""), not null
value :text
created_at :datetime
updated_at :datetime
Instance Method Summary collapse
-
#[](name) ⇒ Object
——————————————————————-.
-
#[]=(name, value) ⇒ Object
——————————————————————-.
- #cached_prefs ⇒ Object
Instance Method Details
#[](name) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/users/preference.rb', line 24 def [](name) # Following line is to preserve AR relationships return super(name) if name.to_s == "user_id" # get the value of belongs_to return cached_prefs[name.to_s] if cached_prefs.key?(name.to_s) cached_prefs[name.to_s] = if user.present? && pref = Preference.find_by_name_and_user_id(name.to_s, user.id) JSON.parse(Base64.decode64(pref.value), symbolize_name: true) end end |
#[]=(name, value) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/users/preference.rb', line 36 def []=(name, value) return super(name, value) if name.to_s == "user_id" # set the value of belongs_to encoded = Base64.encode64(value.to_json) if pref = Preference.find_by(name: name.to_s, user_id: user.id) pref.update_attribute(:value, encoded) else Preference.create(user: user, name: name.to_s, value: encoded) end cached_prefs[name.to_s] = value end |
#cached_prefs ⇒ Object
48 49 50 |
# File 'app/models/users/preference.rb', line 48 def cached_prefs @cached_prefs ||= {} end |