2012-09-23 02:39:12 +00:00
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
has_many :topics
|
2012-10-18 00:51:54 +00:00
|
|
|
has_many :synapses
|
2012-10-26 10:04:52 +00:00
|
|
|
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
|
|
|
|
2014-07-08 01:02:43 +00:00
|
|
|
attr_accessible :name, :email, :image, :password, :password_confirmation, :code, :joinedwithcode, :remember_me
|
2013-07-10 18:02:38 +00:00
|
|
|
|
2013-01-26 01:49:40 +00:00
|
|
|
serialize :settings, UserPreference
|
2012-10-29 17:52:29 +00:00
|
|
|
|
2013-07-11 15:13:27 +00:00
|
|
|
validates_uniqueness_of :name # done by devise
|
|
|
|
validates_uniqueness_of :email # done by devise
|
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 => {
|
|
|
|
:thumb => ['100x100>', :png],
|
|
|
|
:square => ['200x200#', :png],
|
|
|
|
:round => ['200x200#', :png]
|
2014-07-08 04:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#, :convert_options => {:round => Proc.new{self.convert_options}}
|
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/
|
|
|
|
|
|
|
|
def self.convert_options
|
|
|
|
trans = ''
|
|
|
|
trans << ' ( +clone -alpha extract '
|
|
|
|
trans << '-draw "fill black polygon 0,0 0,100 100,0 fill white circle 100,100 100,0" '
|
|
|
|
trans << '( +clone -flip ) -compose multiply -composite '
|
|
|
|
trans << '( +clone -flop ) -compose multiply -composite '
|
|
|
|
trans << ') -alpha off -compose copy_opacity -composite '
|
|
|
|
end
|
|
|
|
|
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
|
2013-11-14 17:28:31 +00:00
|
|
|
codes = []
|
|
|
|
end
|
2014-05-04 19:12:38 +00:00
|
|
|
|
2013-11-14 17:28:31 +00:00
|
|
|
validates :joinedwithcode, :presence => true, :inclusion => { :in => codes, :message => "%{value} is not a valid code" }, :on => :create
|
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)
|
|
|
|
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
|