Class: Preference

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/users/preference.rb

Overview

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

Instance Method Details

#[](name) ⇒ Object




34
35
36
37
38
39
40
41
42
# File 'app/models/users/preference.rb', line 34

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.has_key?(name.to_s)
  cached_prefs[name.to_s] = if (pref = Preference.find_by_name_and_user_id(name.to_s, self.user.id))
    Marshal.load(Base64.decode64(pref.value))
  end
end

#[]=(name, value) ⇒ Object




45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/users/preference.rb', line 45

def []= (name, value)
  return super(name, value) if name.to_s == "user_id" # set the value of belongs_to

  encoded = Base64.encode64(Marshal.dump(value))
  if pref = Preference.find_by_name_and_user_id(name.to_s, self.user.id)
    pref.update_attribute(:value, encoded)
  else
    Preference.create(:user => self.user, :name => name.to_s, :value => encoded)
  end
  cached_prefs[name.to_s] = value
end

#cached_prefsObject



57
58
59
# File 'app/models/users/preference.rb', line 57

def cached_prefs
  @cached_prefs ||= {}
end