metamaps--metamaps/app/models/user.rb

48 lines
1.3 KiB
Ruby
Raw Normal View History

2012-09-23 02:39:12 +00:00
require 'open-uri'
class User < ActiveRecord::Base
has_many :topics
2012-10-18 00:51:54 +00:00
has_many :synapses
has_many :maps
has_many :mappings
2012-09-23 02:39:12 +00:00
2014-01-29 04:27:32 +00:00
before_create :generate_code
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
2014-01-29 04:27:32 +00:00
attr_accessible :name, :email, :password, :password_confirmation, :code, :joinedwithcode, :remember_me
2013-07-10 18:02:38 +00:00
serialize :settings, UserPreference
validates_uniqueness_of :name # done by devise
validates_uniqueness_of :email # done by devise
2014-05-04 19:12:38 +00:00
if ActiveRecord::Base.connection.table_exists? 'users'
codes = ActiveRecord::Base.connection.execute("SELECT code FROM users").map {|user| user["code"] }
else
codes = []
end
2014-05-04 19:12:38 +00:00
validates :joinedwithcode, :presence => true, :inclusion => { :in => codes, :message => "%{value} is not a valid code" }, :on => :create
def generate_code
#generate a random 8 letter/digit code that they can use to invite people
self.code = rand(36**8).to_s(36)
end
def settings
# make sure we always return a UserPreference instance
if read_attribute(:settings).nil?
write_attribute :settings, UserPreference.new
read_attribute :settings
else
read_attribute :settings
end
end
def settings=(val)
write_attribute :settings, val
end
2012-09-23 02:39:12 +00:00
end