Module: Sufia::UsersControllerBehavior
- Extended by:
- ActiveSupport::Concern
- Included in:
- UsersController
- Defined in:
- app/controllers/concerns/sufia/users_controller_behavior.rb
Instance Method Summary collapse
-
#edit ⇒ Object
Display form for users to edit their profile information.
-
#follow ⇒ Object
Follow a user.
- #index ⇒ Object
-
#show ⇒ Object
Display user profile.
- #toggle_trophy ⇒ Object
-
#unfollow ⇒ Object
Unfollow a user.
-
#update ⇒ Object
Process changes from profile form.
- #update_directory? ⇒ Boolean
Instance Method Details
#edit ⇒ Object
Display form for users to edit their profile information
44 45 46 |
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 44 def edit @trophies = @user.trophy_files end |
#follow ⇒ Object
Follow a user
91 92 93 94 95 96 97 |
# File 'app/controllers/concerns/sufia/users_controller_behavior.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(@user.to_param), notice: "You are following #{@user.to_s}" end |
#index ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 15 def index sort_val = get_sort query = params[:uq].blank? ? nil : "%"+params[:uq].downcase+"%" base = User.where(*base_query) unless query.blank? base = base.where("#{Devise.authentication_keys.first} like lower(?) OR display_name like lower(?)", query, query) end @users = base.references(:trophies).order(sort_val).page(params[:page]).per(10) respond_to do |format| format.html format.json { render json: @users.to_json } end end |
#show ⇒ Object
Display user profile
32 33 34 35 36 37 38 39 40 41 |
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 32 def show if @user.respond_to? :profile_events @events = @user.profile_events(100) else @events = [] end @trophies = @user.trophy_files @followers = @user.followers @following = @user.all_following end |
#toggle_trophy ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 72 def toggle_trophy unless current_user.can? :edit, params[:file_id] redirect_to root_path, alert: "You do not have permissions to the file" return false end # TODO make sure current user has access to file t = current_user.trophies.where(generic_file_id: params[:file_id]).first if t t.destroy #TODO do this better says Mike return false if t.persisted? else t = current_user.trophies.create(generic_file_id: params[:file_id]) return false unless t.persisted? end render json: t end |
#unfollow ⇒ Object
Unfollow a user
100 101 102 103 104 105 106 |
# File 'app/controllers/concerns/sufia/users_controller_behavior.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(@user.to_param), notice: "You are no longer following #{@user.to_s}" end |
#update ⇒ Object
Process changes from profile form
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 49 def update if params[:user] @user.attributes = user_params @user.populate_attributes if update_directory? end unless @user.save redirect_to sufia.edit_profile_path(@user.to_param), alert: @user.errors. return end # TODO this should be moved to TrophiesController params.keys.select {|k, v| k.starts_with? 'remove_trophy_' }.each do |smash_trophy| smash_trophy = smash_trophy.sub(/^remove_trophy_/, '') current_user.trophies.where(generic_file_id: smash_trophy).destroy_all end Sufia.queue.push(UserEditProfileEventJob.new(@user.user_key)) redirect_to sufia.profile_path(@user.to_param), notice: "Your profile has been updated" end |
#update_directory? ⇒ Boolean
68 69 70 |
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 68 def update_directory? ['1', 'true'].include? params[:user][:update_directory] end |