Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < Helene::Sdb::Base
- ##############
- ### Schema ###
- ##############
- attribute :email, :type => :string, :null => false
- attribute :password, :type => :encrypted, :null => false
- attribute :role, :type => :string, :default => "user"
- attribute :account_plan, :type => :string
- attribute :first_name, :type => :string
- attribute :last_name, :type => :string
- attribute :website, :type => :url
- attribute :company, :type => :string
- attribute :description, :type => :string
- ####################
- ### Associations ###
- ####################
- has_many :inbox, :class_name => "Message", :foreign_key => :dst_id
- has_many :outbox, :class_name => "Message", :foreign_key => :src_id
- #######################
- ### Specializations ###
- #######################
- class Activation < User
- attr_accessor :email_confirmation
- attr_accessor :password_confirmation
- attr_accessor :token
- def self.class_for(attributes)
- User
- end
- # validates_presence_of :email
- # validates_presence_of :password
- # validates_presence_of :token, :message => 'a valid signup token is required to activate'
- # validates_confirmation_of :email, :message => 'is not the address used for signup'
- # validates_confirmation_of :password
- #
- # validates_each :token do |record, attr, value|
- # if value
- # token_data = TokenData[value]
- # email = token_data[:email]
- # record.errors_add attr, 'invalid token' if record.email != email
- # #email = Encryption.decrypt(value)
- # end
- # end
- def build_credit_card
- nil
- end
- end
- #####################
- ### Class Methods ###
- #####################
- Fattr :current
- def self.authenticate(*args)
- options = args.options
- args.flatten!
- email = options.getopt(:email, args.shift).to_s.downcase.strip
- password = options.getopt(:password, args.shift).to_s.strip
- current_controller = ActionController.current
- local = options.getopt(:local, current_controller && current_controller.send('local_request?')) rescue false
- users = User.find(:all, :conditions => {:email => email})
- return nil if users.empty?
- if users.size > 1
- Alert.alert(:duplicate_email, "#{email} is duplicated in the system")
- return false
- end
- user = users.first
- if local or user.password == password
- user
- else
- false
- end
- end
- def self.find_by_email(email)
- find(:first, :conditions => {:email => email})
- end
- ########################
- ### Instance Methods ###
- ########################
- def password
- pass = Array(attributes[:password]).first
- Encryption.decrypt(pass) rescue pass
- end
- def signup_token
- @signup_token ||= TokenData.create!(:data => {:email => email})
- end
- def name
- if(!first_name.blank? || !last_name.blank?)
- [first_name, last_name].join(' ').strip
- else
- email
- end
- end
- def shared_orders_with(user)
- [] # eventually this will be real once we can add other users to your order
- end
- def shared_dropboxes_with(user)
- # do we need to also add dropboxes where both user_ids are added on a third person's dropbox?
- db = []
- # db << dropboxes.find(:all, :joins => :dropbox_user_joins, :conditions => {"#{ DropboxUserJoin.table_name }.user_id" => user.id})
- # db << user.dropboxes.find(:all, :joins => :dropbox_user_joins, :conditions => {"#{ DropboxUserJoin.table_name }.user_id" => id})
- db.flatten
- end
- def message other, content, options = {}
- options.to_options!
- user = self
- options.reverse_merge!(:src_id => user.id, :dst_id => other.id, :content => content.to_s.strip)
- Message.create!(options)
- end
- # def inbox options = {}, &block
- # options.to_options!
- # user = self
- # Message.inbox(user, options, &block)
- # end
- #
- # def outbox(*args, &block)
- # Message.outbox(user=self, *args, &block)
- # end
- def request_connection_to(user, message)
- message(user, message, :message_type => :connection)
- end
- def pending_connection_requests
- Message.find(:all, :conditions => {:message_type => :connection, :viewed => false, :src_id => self.id})
- end
- def user_connections
- UserConnection.find(:all, :conditions => [
- {:accepter_id => self.id},
- 'or',
- {:requester_id => self.id}
- ])
- end
- # u = User.last
- # a = User.first
- # cu = a.connected_users
- # cu.map(&:first_name)
- #sql_for_select
- def connected_users
- ids = connected_user_ids
- return [] if ids.empty?
- puts "Calling User.find with these ids #{ids.join(', ')}"
- User.find(ids)
- end
- def connected_user_ids
- accepted_connections = UserConnection.find(:all, :conditions => {:accepter_id => self.id})
- puts "Found #{accepted_connections.size} accepted connections"
- requested_connections = UserConnection.find(:all, :conditions => {:requester_id => self.id})
- puts "Found #{requested_connections.size} requested connections"
- accepted_connections.map(&:requester_id) + requested_connections.map(&:accepter_id)
- end
- def is_connected?(user)
- conn = UserConnection.find(:first, :conditions => [
- {:accepter_id => self.id, :requester_id => user.id},
- 'or',
- {:requester_id => self.id, :accepter_id => user.id}
- ])
- !conn.nil?
- end
- # a.accept_connection_to u
- def accept_connection_to(user)
- return false if is_connected?(user) || user.id == self.id
- uc = UserConnection.new(:accepter_id => self.id, :requester_id => user.id)
- conn = uc.save
- !conn.nil?
- end
- def search_for_connections(query)
- return [] if query.blank?
- User.find(:all,
- :conditions => ["first_name LIKE :query OR last_name LIKE :query OR company like :query", {:query => "%#{query}%"}])
- end
- end
- __END__
- class User < ActiveRecord::Base
- Fattr :current
- is_paranoid
- belongs_to :role
- has_many :user_role_joins, :dependent => :destroy, :uniq => true
- has_many :other_roles, :through => :user_role_joins, :source => :role, :uniq => true
- has_one :credit_card, :dependent => :destroy
- has_many :channels
- has_many :orders
- has_many :user_connection_user_joins, :dependent => :destroy
- has_many :user_connections, :through => :user_connection_user_joins
- # has_many :dropboxes, :foreign_key => 'owner_id', :order => "name"
- # has_many :dropbox_user_joins
- # has_many :shared_dropboxes, :through => :dropbox_user_joins, :source => :dropbox, :order => "name"
- accepts_nested_attributes_for :credit_card, :allow_destroy => true, :reject_if => lambda{|hash| hash.values.all?{|value| value.blank?}}
- def shared_orders_with(user)
- [] # eventually this will be real once we can add other users to your order
- end
- def shared_dropboxes_with(user)
- # do we need to also add dropboxes where both user_ids are added on a third person's dropbox?
- db = []
- # db << dropboxes.find(:all, :joins => :dropbox_user_joins, :conditions => {"#{ DropboxUserJoin.table_name }.user_id" => user.id})
- # db << user.dropboxes.find(:all, :joins => :dropbox_user_joins, :conditions => {"#{ DropboxUserJoin.table_name }.user_id" => id})
- db.flatten
- end
- def add_dropbox(dropbox)
- dropbox.add_user(self) unless dropbox.owner_id==id
- dropbox
- end
- before_validation_on_create do |user|
- user.role ||= Role.for(:user)
- end
- after_create do |user|
- user.password ||= User.generate_password
- end
- validates_presence_of :email
- validates_presence_of :role
- validates_size_of :email, :in => 3..42
- validates_uniqueness_of :email, :scope => [:deleted_at]
- validates_size_of :password, :in => 3..42, :allow_nil => true
- def User.model_name
- self == User ? super : User.model_name
- end
- class Activation < User
- attr_accessor :email_confirmation
- attr_accessor :password_confirmation
- attr_accessor :token
- validates_presence_of :email
- validates_presence_of :password
- validates_presence_of :token, :message => 'a valid signup token is required to activate'
- validates_confirmation_of :email, :message => 'is not the address used for signup'
- validates_confirmation_of :password
- validates_each :token do |record, attr, value|
- if value
- token_data = TokenData[value]
- email = token_data[:email]
- record.errors_add attr, 'invalid token' if record.email != email
- #email = Encryption.decrypt(value)
- end
- end
- end
- def User.activate(*args, &block)
- Activation.create(*args, &block)
- end
- def User.authenticate(*args)
- options = args.options
- args.flatten!
- email = options.getopt(:email, args.shift).to_s.downcase.strip
- password = options.getopt(:password, args.shift).to_s.strip
- current_controller = ActionController.current
- local = options.getopt(:local, current_controller && current_controller.send('local_request?')) rescue false
- conditions = {:email => email}
- user = User.cached(:first, :conditions => conditions)
- return nil unless user
- if(local or (user.password == password))
- user
- else
- false
- end
- end
- def User.signup_token_for user_or_email
- user = User === user_or_email ? user_or_email : User.new(:email => user_or_email)
- @signup_token = TokenData.create!(:data => {:email => user.email})
- end
- def signup_token
- @signup_token ||= TokenData.create!(:data => {:email => email})
- end
- def User.admin
- User.cached(:first, :conditions => {:email => 'admin@myqval.com'}) or
- raise('no admin user!')
- end
- def password
- password = self['password']
- password.to_s.decrypted if password
- end
- def password= password
- self['password'] = password ? password.to_s.encrypted : nil
- end
- def name
- if(!first_name.blank? || !last_name.blank?)
- [first_name, last_name].join(' ').strip
- else
- email
- end
- end
- def roles
- [role, *other_roles]
- end
- def add_roles(*args)
- roles_to_add = args.flatten.compact.map{|arg| Role.for(arg)}
- roles_to_add.each do |role_to_add|
- next if roles.any?{|role| role == role_to_add}
- other_roles << role_to_add
- end
- unless new_record?
- save!
- reload
- end
- roles
- end
- %w( add_role plays! is! can! ).each{|method| alias_method method, 'add_roles'}
- def has_roles(*args)
- return true if roles.any?{|role| role == Role.admin}
- roles_to_check = args.flatten.compact.map{|arg| Role.for(arg)}
- roles_to_check.all?{|role_to_check| roles.any?{|role| role == role_to_check}}
- end
- %w( has_roles? has_role has_role? plays plays? is is? can can? ).each{|method| alias_method method, 'has_roles'}
- def User.generate_password(*args, &block)
- String.random(*args, &block)
- end
- def as other
- becomes(other)
- end
- def scoping *args, &block
- options = args.options
- models = args.flatten.compact.uniq
- user = self
- scope = {
- :find =>
- { :conditions => { :user_id => user.id } },
- :create =>
- { :user_id => user.id },
- }
- scope.update options
- stack = []
- until models.empty?
- model = models.pop
- stack.push(
- lambda do
- model.send(:with_scope, scope, &block)
- end
- )
- block = stack.last unless models.empty?
- end
- stack.last.call
- end
- end
Add Comment
Please, Sign In to add comment