2012-09-22 22:39:12 -04:00
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
|
|
|
|
2014-08-12 18:14:04 -04:00
|
|
|
has_many :topics
|
|
|
|
has_many :synapses
|
|
|
|
has_many :maps
|
|
|
|
has_many :mappings
|
2016-03-11 17:16:04 +11:00
|
|
|
has_many :tokens
|
2012-09-22 22:39:12 -04:00
|
|
|
|
2015-12-15 12:33:22 +08:00
|
|
|
after_create :generate_code
|
2013-07-11 11:13:27 -04:00
|
|
|
|
|
|
|
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
|
2014-01-28 23:27:32 -05:00
|
|
|
|
2013-01-25 20:49:40 -05:00
|
|
|
serialize :settings, UserPreference
|
2012-10-29 13:52:29 -04:00
|
|
|
|
2014-10-21 20:31:59 -04: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 11:13:27 -04:00
|
|
|
validates_uniqueness_of :name # done by devise
|
|
|
|
validates_uniqueness_of :email # done by devise
|
2014-10-24 11:30:26 -04:00
|
|
|
|
2016-01-07 15:32:19 +08:00
|
|
|
validates :joinedwithcode, :presence => true, :inclusion => { :in => $codes, :message => "%{value} is not valid" }, :on => :create
|
2014-05-04 15:12:38 -04:00
|
|
|
|
2014-07-07 21:02:43 -04:00
|
|
|
# This method associates the attribute ":image" with a file attachment
|
|
|
|
has_attached_file :image, :styles => {
|
2014-11-25 15:06:30 -05:00
|
|
|
:thirtytwo => ['32x32#', :png],
|
|
|
|
:sixtyfour => ['64x64#', :png],
|
|
|
|
:ninetysix => ['96x96#', :png],
|
|
|
|
:onetwentyeight => ['128x128#', :png]
|
2014-09-03 19:05:25 -04:00
|
|
|
},
|
2015-10-30 14:30:24 +08:00
|
|
|
:default_url => 'https://s3.amazonaws.com/metamaps-assets/site/user.png'
|
2014-07-08 00:21:43 -04:00
|
|
|
|
2014-07-07 21:02:43 -04:00
|
|
|
# Validate the attached image is image/jpg, image/png, etc
|
|
|
|
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
|
2014-08-12 11:09:53 -04:00
|
|
|
|
2015-11-06 17:08:42 +08:00
|
|
|
# override default as_json
|
2014-08-12 11:09:53 -04:00
|
|
|
def as_json(options={})
|
2014-08-12 12:01:01 -04:00
|
|
|
{ :id => self.id,
|
|
|
|
:name => self.name,
|
2015-04-28 18:43:47 -04:00
|
|
|
:image => self.image.url(:sixtyfour),
|
|
|
|
:admin => self.admin
|
2014-08-12 11:09:53 -04:00
|
|
|
}
|
|
|
|
end
|
2015-11-06 17:08:42 +08:00
|
|
|
|
|
|
|
def as_json_for_autocomplete
|
2015-11-28 21:51:28 +08:00
|
|
|
json = {}
|
|
|
|
json['id'] = id
|
|
|
|
json['label'] = name
|
|
|
|
json['value'] = name
|
|
|
|
json['profile'] = image.url(:sixtyfour)
|
|
|
|
json['mapCount'] = maps.count
|
|
|
|
json['generation'] = generation
|
|
|
|
json['created_at'] = created_at.strftime("%m/%d/%Y")
|
|
|
|
json['rtype'] = "mapper"
|
|
|
|
json
|
2015-11-06 17:08:42 +08:00
|
|
|
end
|
2013-01-25 20:49:40 -05:00
|
|
|
|
2015-11-06 17:08:42 +08:00
|
|
|
#generate a random 8 letter/digit code that they can use to invite people
|
2013-07-11 11:13:27 -04:00
|
|
|
def generate_code
|
2015-12-16 22:16:02 +08:00
|
|
|
self.code ||= rand(36**8).to_s(36)
|
2014-11-21 17:24:08 -05:00
|
|
|
$codes.push(self.code)
|
2015-12-15 12:33:22 +08:00
|
|
|
self.generation = get_generation!
|
2014-11-21 16:50:42 -05:00
|
|
|
end
|
|
|
|
|
2015-12-15 12:33:22 +08:00
|
|
|
def get_generation!
|
2015-11-06 17:08:42 +08:00
|
|
|
if code == joinedwithcode
|
|
|
|
update(generation: 0)
|
2014-11-21 16:50:42 -05:00
|
|
|
else
|
2015-12-07 11:44:08 +08:00
|
|
|
update(generation: User.find_by_code(joinedwithcode).generation + 1)
|
2014-11-21 16:50:42 -05:00
|
|
|
end
|
2013-07-11 11:13:27 -04:00
|
|
|
end
|
|
|
|
|
2013-01-25 20:49:40 -05:00
|
|
|
def settings
|
|
|
|
# make sure we always return a UserPreference instance
|
|
|
|
if read_attribute(:settings).nil?
|
|
|
|
write_attribute :settings, UserPreference.new
|
|
|
|
end
|
2015-11-06 17:08:42 +08:00
|
|
|
read_attribute :settings
|
2013-01-25 20:49:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def settings=(val)
|
|
|
|
write_attribute :settings, val
|
|
|
|
end
|
2015-11-06 17:08:42 +08:00
|
|
|
|
2012-09-22 22:39:12 -04:00
|
|
|
end
|