Module: Account::Memberships::ControllerBase
- Extended by:
- ActiveSupport::Concern
- Included in:
- Account::MembershipsController
- Defined in:
- app/controllers/concerns/account/memberships/controller_base.rb
Instance Method Summary collapse
- #demote ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #promote ⇒ Object
- #reinvite ⇒ Object
- #search ⇒ Object
- #show ⇒ Object
-
#update ⇒ Object
PATCH/PUT /account/memberships/:id PATCH/PUT /account/memberships/:id.json.
Instance Method Details
#demote ⇒ Object
58 59 60 61 62 63 |
# File 'app/controllers/concerns/account/memberships/controller_base.rb', line 58 def demote @membership.roles.delete Role.admin redirect_to account_team_memberships_path(@team) rescue RemovingLastTeamAdminException => _ redirect_to account_team_memberships_path(@team), alert: I18n.t("memberships.notifications.cant_demote") end |
#destroy ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/controllers/concerns/account/memberships/controller_base.rb', line 70 def destroy # Instead of destroying the membership, we nullify the user_id and use the membership record as a 'Tombstone' for referencing past associations (eg message at-mentions and Scaffolding::CompletelyConcrete::TangibleThings::Assignment) user_was = @membership.user @membership.nullify_user if user_was == current_user # if a user removes themselves from a team, we'll have to send them to their dashboard. redirect_to account_dashboard_path, notice: I18n.t("memberships.notifications.you_removed_yourself", team_name: @team.name) else redirect_to [:account, @team, :memberships], notice: I18n.t("memberships.notifications.destroyed") end rescue RemovingLastTeamAdminException redirect_to account_team_memberships_path(@team), alert: I18n.t("memberships.notifications.cant_remove") end |
#edit ⇒ Object
38 39 |
# File 'app/controllers/concerns/account/memberships/controller_base.rb', line 38 def edit end |
#index ⇒ Object
10 11 12 13 14 |
# File 'app/controllers/concerns/account/memberships/controller_base.rb', line 10 def index unless @memberships.count > 0 redirect_to account_team_invitations_path(@team), notice: I18n.t("memberships.notifications.no_members") end end |
#promote ⇒ Object
65 66 67 68 |
# File 'app/controllers/concerns/account/memberships/controller_base.rb', line 65 def promote @membership.roles << Role.admin unless @membership.roles.include?(Role.admin) redirect_to account_team_memberships_path(@team) end |
#reinvite ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/controllers/concerns/account/memberships/controller_base.rb', line 86 def reinvite if helpers.current_limits.can?(:create, Membership) @invitation = Invitation.new(membership: @membership, team: @team, email: @membership.user_email, from_membership: current_membership) if @invitation.save redirect_to [:account, @team, :memberships], notice: I18n.t("account.memberships.notifications.reinvited") else redirect_to [:account, @team, :memberships], notice: "There was an error creating the invitation (#{@invitation.errors..to_sentence})" end else flash[:error] = :create_limit redirect_to [:account, @team, :memberships] end end |
#search ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/controllers/concerns/account/memberships/controller_base.rb', line 16 def search # TODO This is a particularly crazy example where we're doing the search logic ourselves in SQL. # In the future, I could see us replacing this with a recommended example using Elasticsearch and the `searchkick` Ruby Gem. limit = params[:limit] || 100 page = [params[:page].to_i, 1].max # Ensure we never have a negative or zero page value search_term = "%#{params[:search]&.upcase}%" offset = (page - 1) * limit # Currently we're only searching on user.first_name, user.last_name, memberships.user_first_name and memberships.user_last_name. Should we also search on the email address? # This query could use impromement. Currently if you search for "Ad Pal" you wouldn't find a user "Adam Pallozzi" query = "UPPER(first_name) LIKE :search_term OR UPPER(last_name) LIKE :search_term OR UPPER(user_first_name) LIKE :search_term OR UPPER(user_last_name) LIKE :search_term" # We're using left outer join here because we may get memberships that don't belong to a membership yet memberships = @team.memberships.accessible_by(current_ability, :show).left_outer_joins(:user).where(query, search_term: search_term) total_results = memberships.size # the Areal.sql(LOWER(COALESCE...)) part means that if the user record doesn't exist or if there are records that start with a lower case letter, we will still sort everything correctly using the user.first_name instead. memberships_array = memberships.limit(limit).offset(offset).order(Arel.sql("LOWER(COALESCE(first_name, user_first_name) )")).map { |membership| {id: membership.id, text: membership.label_string.to_s} } results = {results: memberships_array, pagination: {more: (total_results > page * limit)}} render json: results.to_json end |
#show ⇒ Object
35 36 |
# File 'app/controllers/concerns/account/memberships/controller_base.rb', line 35 def show end |
#update ⇒ Object
PATCH/PUT /account/memberships/:id PATCH/PUT /account/memberships/:id.json
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/controllers/concerns/account/memberships/controller_base.rb', line 43 def update respond_to do |format| if @membership.update(membership_params) format.html { redirect_to [:account, @membership], notice: I18n.t("memberships.notifications.updated") } format.json { render :show, status: :ok, location: [:account, @membership] } else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @membership.errors, status: :unprocessable_entity } end rescue RemovingLastTeamAdminException => _ format.html { redirect_to [:account, @team, :memberships], alert: I18n.t("memberships.notifications.cant_demote") } format.json { render json: {exception: I18n.t("memberships.notifications.cant_demote")}, status: :unprocessable_entity } end end |