Sha256: 65365ae966137070709c1fe1ad0e113479f5e3b7ba50f13cb06c6f3113ee3444

Contents?: true

Size: 1.64 KB

Versions: 15

Compression:

Stored size: 1.64 KB

Contents

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://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
#

class Preference < ActiveRecord::Base
  belongs_to :user

  #-------------------------------------------------------------------
  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)
                                Marshal.load(Base64.decode64(pref.value))
    end
  end

  #-------------------------------------------------------------------
  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: 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

  def cached_prefs
    @cached_prefs ||= {}
  end

  ActiveSupport.run_load_hooks(:fat_free_crm_preference, self)
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
fat_free_crm-0.14.2 app/models/users/preference.rb
fat_free_crm-0.15.2 app/models/users/preference.rb
fat_free_crm-0.16.4 app/models/users/preference.rb
fat_free_crm-0.14.1 app/models/users/preference.rb
fat_free_crm-0.15.1 app/models/users/preference.rb
fat_free_crm-0.16.3 app/models/users/preference.rb
fat_free_crm-0.16.2 app/models/users/preference.rb
fat_free_crm-0.16.1 app/models/users/preference.rb
fat_free_crm-0.16.0 app/models/users/preference.rb
fat_free_crm-0.15.0 app/models/users/preference.rb
fat_free_crm-0.15.0.beta.2 app/models/users/preference.rb
fat_free_crm-0.15.0.beta app/models/users/preference.rb
fat_free_crm-0.14.0 app/models/users/preference.rb
reduced_fat_crm-0.15.0.beta app/models/users/preference.rb
reduced_fat_crm-0.14.0 app/models/users/preference.rb