Created
November 1, 2011 17:27
-
-
Save gabrielstuff/1331263 to your computer and use it in GitHub Desktop.
users_controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < InheritedResources::Base | |
actions :show, :update, :index | |
before_filter :authenticate_user! | |
has_scope :max_age | |
has_scope :min_age | |
has_scope :sex | |
has_scope :filter, :type => :array do |controller, scope, value| | |
value = value.delete_if{|x| x == "-1"} | |
value.empty? ? scope : scope.filter(value) | |
end | |
has_scope :within do |controller, scope, value| | |
scope.within(value, :origin => controller.current_user).order(:distance) | |
end | |
def show | |
@user = if params[:id] =~ /^[0-9]+$/ | |
User.find(params[:id]) | |
else | |
User.find_by_username(params[:id]) | |
end | |
@user.current_user = current_user | |
VisitHistory.create(:visitor_id => current_user.id, :visited_id => params[:id]) if(current_user.id != params[:id].to_i) | |
if current_user.id == params[:id].to_i | |
@last_visisted_profile = current_user.visited_profiles.group(:id).limit(10) || [] | |
@last_visitor_profile = current_user.visitor_profiles.group(:id).limit(10) || [] | |
end | |
show! | |
end | |
def full_profile | |
@preferences = User.find(params[:id]).preferences | |
respond_with(@preferences, :location => nil) | |
end | |
def can_communicate_with | |
if current_user.can_communicate_with?(User.find(params[:id])) | |
render :text => "{}", :status => 200 | |
else | |
render :text => "You don't know this user", :status => 403 | |
end | |
end | |
protected | |
def resource_params | |
super.merge(:current_user => current_user) | |
end | |
def resource | |
temp = super | |
temp.current_user = current_user | |
return temp | |
end | |
def collection | |
@users ||= apply_scopes(end_of_association_chain).order(:points). | |
where("users.id != ? AND users.username IS NOT NULL AND username != ''", current_user.id). | |
without_users_who_banned(current_user) | |
@users.each {|user| user.current_user = current_user} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment