Module: Sufia::UsersControllerBehavior

Extended by:
ActiveSupport::Concern
Included in:
UsersController
Defined in:
app/controllers/concerns/sufia/users_controller_behavior.rb

Instance Method Summary collapse

Instance Method Details

#editObject

Display form for users to edit their profile information



35
36
37
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 35

def edit
  @trophies = Sufia::TrophyPresenter.find_by_user(@user)
end

#followObject

Follow a user



64
65
66
67
68
69
70
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 64

def follow
  unless current_user.following?(@user)
    current_user.follow(@user)
    UserFollowEventJob.perform_later(current_user, @user)
  end
  redirect_to sufia.profile_path(@user.to_param), notice: "You are following #{@user}"
end

#indexObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 15

def index
  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_value).page(params[:page]).per(10)

  respond_to do |format|
    format.html
    format.json { render json: @users.to_json }
  end
end

#notifications_numberObject



81
82
83
84
85
86
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 81

def notifications_number
  @notify_number = 0
  return if action_name == "index" && controller_name == "mailbox"
  return unless user_signed_in?
  @notify_number = current_user.mailbox.inbox(unread: true).count
end

#showObject

Display user profile



30
31
32
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 30

def show
  @presenter = Sufia::UserProfilePresenter.new(@user, current_ability)
end

#unfollowObject

Unfollow a user



73
74
75
76
77
78
79
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 73

def unfollow
  if current_user.following?(@user)
    current_user.stop_following(@user)
    UserUnfollowEventJob.perform_later(current_user, @user)
  end
  redirect_to sufia.profile_path(@user.to_param), notice: "You are no longer following #{@user}"
end

#updateObject

Process changes from profile form



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 40

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.full_messages
    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(work_id: smash_trophy).destroy_all
  end
  UserEditProfileEventJob.perform_later(@user)
  redirect_to sufia.profile_path(@user.to_param), notice: "Your profile has been updated"
end

#update_directory?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/controllers/concerns/sufia/users_controller_behavior.rb', line 59

def update_directory?
  ['1', 'true'].include? params[:user][:update_directory]
end