Class: UsersController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/users_controller.rb

Overview

Copyright © 2012 The Pennsylvania State University

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Instance Method Summary collapse

Instance Method Details

#editObject

Display form for users to edit their profile information



45
46
47
48
# File 'app/controllers/users_controller.rb', line 45

def edit
  @user = current_user
  @trophies = @user.trophy_ids
end

#followObject

Follow a user



91
92
93
94
95
96
97
# File 'app/controllers/users_controller.rb', line 91

def follow
  unless current_user.following?(@user)
    current_user.follow(@user)
    Sufia.queue.push(UserFollowEventJob.new(current_user.user_key, @user.user_key))
  end
  redirect_to sufia.profile_path(URI.escape(@user.to_s,'@.')), notice: "You are following #{@user.to_s}"
end

#indexObject



22
23
24
25
26
27
28
29
30
# File 'app/controllers/users_controller.rb', line 22

def index
  sort_val = get_sort
  query = params[:uq].blank? ? nil : "%"+params[:uq].downcase+"%"
  if query.blank?
    @users = User.order(sort_val).page(params[:page]).per(10) if query.blank? 
  else
    @users = User.where("(login like lower(?) OR display_name like lower(?))",query,query).order(sort_val).page(params[:page]).per(10)
  end
end

#showObject

Display user profile



33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/users_controller.rb', line 33

def show
  if @user.respond_to? :profile_events
    @events = @user.profile_events(100) 
  else 
    @events = []
  end
  @trophies = @user.trophy_ids
  @followers = @user.followers
  @following = @user.all_following
end

#toggle_trophyObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/users_controller.rb', line 69

def toggle_trophy    
   id = params[:file_id]
   id = "#{Sufia::Engine.config.id_namespace}:#{id}" unless id.include?(":")
   unless current_user.can? :edit, id
     redirect_to root_path, alert: "You do not have permissions to the file"
     return false
   end
   # TO DO  make sure current user has access to file
   t = Trophy.where(:generic_file_id => params[:file_id], :user_id => current_user.id).first
   if t.blank? 
     t = Trophy.create(:generic_file_id => params[:file_id], :user_id => current_user.id)
     return false unless t.persisted?
   else
     t.delete  
     #TODO do this better says Mike
     return false if t.persisted?  
   end
   render :json => t
end

#unfollowObject

Unfollow a user



100
101
102
103
104
105
106
# File 'app/controllers/users_controller.rb', line 100

def unfollow
  if current_user.following?(@user)
    current_user.stop_following(@user)
    Sufia.queue.push(UserUnfollowEventJob.new(current_user.user_key, @user.user_key))
  end
  redirect_to sufia.profile_path(URI.escape(@user.to_s,'@.')), notice: "You are no longer following #{@user.to_s}"
end

#updateObject

Process changes from profile form



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/users_controller.rb', line 51

def update
  @user.update_attributes(params[:user])

  @user.populate_attributes if params[:update_directory]
  @user.avatar = nil if params[:delete_avatar]
  unless @user.save
    redirect_to sufia.edit_profile_path(URI.escape(@user.to_s,'@.')), alert: @user.errors.full_messages
    return
  end
  Sufia.queue.push(UserEditProfileEventJob.new(@user.user_key))
  redirect_to sufia.profile_path(URI.escape(@user.to_s,'@.')), notice: "Your profile has been updated"
  delete_trophy = params.keys.reject{|k,v|k.slice(0,'remove_trophy'.length)!='remove_trophy'}
  delete_trophy = delete_trophy.map{|v| v.slice('remove_trophy_'.length..-1)}
  delete_trophy.each do | smash_trophy |
    Trophy.where(user_id: current_user.id, generic_file_id: smash_trophy.slice("#{Sufia::Engine.config.id_namespace}:".length..-1)).each.map(&:delete)
    #Trophy.where(user_id: current_user.id, generic_file_id: smash_trophy.slice("#{Sufia::Engine.config.id_namespace}:".length..-1)).each.map(&:delete)
  end
end