2012-09-23 02:39:12 +00:00
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
has_many :topics
|
|
|
|
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
|
2013-07-11 15:13:27 +00:00
|
|
|
|
|
|
|
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
|
2014-01-29 04:27:32 +00:00
|
|
|
|
2013-01-26 01:49:40 +00:00
|
|
|
serialize :settings, UserPreference
|
2012-10-29 17:52:29 +00:00
|
|
|
|
2014-10-22 00:31:59 +00:00
|
|
|
validates :password, :presence => true,
|
|
|
|
:length => { :within => 8..40 },
|
|
|
|
:on => :create
|
|
|
|
validates :password, :length => { :within => 8..40 },
|
|
|
|
:allow_blank => true,
|
|
|
|
:on => :update
|
|
|
|
validates_confirmation_of :password
|
|
|
|
|
|
|
|
validates_presence_of :name # done by devise
|
|
|
|
validates_presence_of :email # done by devise
|
2013-07-11 15:13:27 +00:00
|
|
|
validates_uniqueness_of :name # done by devise
|
|
|
|
validates_uniqueness_of :email # done by devise
|
2014-10-24 15:30:26 +00:00
|
|
|
|
2014-11-21 22:24:08 +00:00
|
|
|
validates :joinedwithcode, :presence => true, :inclusion => { :in => $codes, :message => "%{value} is not valid" }, :on => :create
|
2014-05-04 19:12:38 +00:00
|
|
|
|
2014-07-08 01:02:43 +00:00
|
|
|
# This method associates the attribute ":image" with a file attachment
|
|
|
|
has_attached_file :image, :styles => {
|
2014-11-25 20:06:30 +00:00
|
|
|
:thirtytwo => ['32x32#', :png],
|
|
|
|
:sixtyfour => ['64x64#', :png],
|
|
|
|
:ninetysix => ['96x96#', :png],
|
|
|
|
:onetwentyeight => ['128x128#', :png]
|
2014-09-03 23:05:25 +00:00
|
|
|
},
|
2015-10-28 04:44:38 +00:00
|
|
|
:default_url => 'https://s3.amazonaws.com/metamaps-assets/site/user.png'
|
2014-07-08 04:21:43 +00:00
|
|
|
|
2014-07-08 01:02:43 +00:00
|
|
|
# Validate the attached image is image/jpg, image/png, etc
|
|
|
|
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
|
2014-08-12 15:09:53 +00:00
|
|
|
|
|
|
|
def as_json(options={})
|
2014-08-12 16:01:01 +00:00
|
|
|
{ :id => self.id,
|
|
|
|
:name => self.name,
|
2015-04-28 22:43:47 +00:00
|
|
|
:image => self.image.url(:sixtyfour),
|
|
|
|
:admin => self.admin
|
2014-08-12 15:09:53 +00:00
|
|
|
}
|
|
|
|
end
|
2013-01-26 01:49:40 +00:00
|
|
|
|
2013-07-11 15:13:27 +00:00
|
|
|
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)
|
2014-11-21 21:50:42 +00:00
|
|
|
|
2014-11-21 22:24:08 +00:00
|
|
|
$codes.push(self.code)
|
|
|
|
|
2014-11-21 21:50:42 +00:00
|
|
|
self.generation = self.get_generation
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_generation
|
|
|
|
if self.joinedwithcode == self.code
|
|
|
|
# if your joinedwithcode equals your code you must be GEN 0
|
|
|
|
gen = 0
|
|
|
|
elsif self.generation
|
|
|
|
# if your generation has already been calculated then just return that value
|
|
|
|
gen = self.generation
|
|
|
|
else
|
|
|
|
# if your generation hasn't been calculated, base it off the
|
|
|
|
# generation of the person whose code you joined with + 1
|
|
|
|
gen = User.find_by_code(self.joinedwithcode).get_generation + 1
|
|
|
|
end
|
2013-07-11 15:13:27 +00:00
|
|
|
end
|
|
|
|
|
2013-01-26 01:49:40 +00:00
|
|
|
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
|