| 
									
										
										
										
											2010-03-04 08:33:26 +00:00
										 |  |  | class User | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   include Mongoid::Document | 
					
						
							| 
									
										
										
										
											2011-03-08 09:25:46 +00:00
										 |  |  |   include Mongoid::Timestamps | 
					
						
							| 
									
										
										
										
											2010-03-04 08:33:26 +00:00
										 |  |  |   | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mount_uploader :avatar, AvatarUploader | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2011-01-31 03:31:33 +00:00
										 |  |  |   field :admin, :type => Boolean, :default => true | 
					
						
							| 
									
										
										
										
											2011-11-18 08:43:14 +00:00
										 |  |  |   field :active_role | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2011-05-02 05:23:39 +00:00
										 |  |  |   has_many :user_attributes | 
					
						
							| 
									
										
										
										
											2011-04-28 14:47:34 +00:00
										 |  |  |   has_many :user_roles | 
					
						
							|  |  |  |   has_many :user_infos | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2011-02-01 02:46:26 +00:00
										 |  |  |   # Update or create the user_role records | 
					
						
							|  |  |  |   def user_roles=(*attrs) | 
					
						
							|  |  |  |     attrs[0].each do |roles| | 
					
						
							|  |  |  |       if roles[:id].blank? | 
					
						
							|  |  |  |         user_roles.build(roles) | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2011-02-01 02:46:26 +00:00
										 |  |  |         user_role = user_roles.detect {|a| a.id.to_s == roles[:id].to_s } | 
					
						
							| 
									
										
										
										
											2011-02-01 07:12:23 +00:00
										 |  |  |         user_role.update_attributes(roles) | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-03-04 08:33:26 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2011-04-28 14:47:34 +00:00
										 |  |  |   # Update or create the user_info records | 
					
						
							|  |  |  |   def user_infos=(*attrs) | 
					
						
							|  |  |  |     attrs[0].each do |infos| | 
					
						
							|  |  |  |       if infos[:id].blank? | 
					
						
							|  |  |  |         user_infos.build(infos) | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         user_info = user_infos.detect {|a| a.id.to_s == infos[:id].to_s } | 
					
						
							|  |  |  |         user_info.update_attributes(infos) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2011-02-01 07:12:23 +00:00
										 |  |  |   # Get an user_info from model key | 
					
						
							|  |  |  |   def get_info_from_model_key(key) | 
					
						
							|  |  |  |     self.user_infos.detect {|a| a.key.to_s == key.to_s } | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2011-02-01 02:46:26 +00:00
										 |  |  |   # Get an user_role from model key | 
					
						
							|  |  |  |   def get_role_from_model_key(key) | 
					
						
							|  |  |  |     self.user_roles.detect {|a| a.key.to_s == key.to_s } | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2011-11-17 12:02:58 +00:00
										 |  |  |   def name | 
					
						
							|  |  |  |     infos = self.user_infos.detect {|info| info.key.to_s.eql?('profile') } | 
					
						
							|  |  |  |     if infos | 
					
						
							|  |  |  |       first_name = infos.attribute_values.detect {|value| value.key.to_s.eql?('first_name') }[I18n.locale.to_s] rescue nil | 
					
						
							| 
									
										
										
										
											2011-11-19 13:47:18 +00:00
										 |  |  |       last_name = infos.attribute_values.detect {|value| value.key.to_s.eql?('family_name') }[I18n.locale.to_s] rescue nil | 
					
						
							| 
									
										
										
										
											2011-11-17 12:02:58 +00:00
										 |  |  |       return "#{first_name} #{last_name}" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       return nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  | end |