You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

  1. # frozen_string_literal: true
  2. class RelationshipsController < ApplicationController
  3. layout 'admin'
  4. before_action :authenticate_user!
  5. before_action :set_accounts, only: :show
  6. before_action :set_relationships, only: :show
  7. before_action :set_body_classes
  8. helper_method :following_relationship?, :followed_by_relationship?, :mutual_relationship?
  9. def show
  10. @form = Form::AccountBatch.new
  11. end
  12. def update
  13. @form = Form::AccountBatch.new(form_account_batch_params.merge(current_account: current_account, action: action_from_button))
  14. @form.save
  15. rescue ActionController::ParameterMissing
  16. # Do nothing
  17. ensure
  18. redirect_to relationships_path(filter_params)
  19. end
  20. private
  21. def set_accounts
  22. @accounts = RelationshipFilter.new(current_account, filter_params).results.page(params[:page]).per(40)
  23. end
  24. def set_relationships
  25. @relationships = AccountRelationshipsPresenter.new(@accounts.pluck(:id), current_user.account_id)
  26. end
  27. def form_account_batch_params
  28. params.require(:form_account_batch).permit(:action, account_ids: [])
  29. end
  30. def following_relationship?
  31. params[:relationship].blank? || params[:relationship] == 'following'
  32. end
  33. def mutual_relationship?
  34. params[:relationship] == 'mutual'
  35. end
  36. def followed_by_relationship?
  37. params[:relationship] == 'followed_by'
  38. end
  39. def filter_params
  40. params.slice(:page, *RelationshipFilter::KEYS).permit(:page, *RelationshipFilter::KEYS)
  41. end
  42. def action_from_button
  43. if params[:follow]
  44. 'follow'
  45. elsif params[:unfollow]
  46. 'unfollow'
  47. elsif params[:remove_from_followers]
  48. 'remove_from_followers'
  49. elsif params[:block_domains]
  50. 'block_domains'
  51. end
  52. end
  53. def set_body_classes
  54. @body_classes = 'admin'
  55. end
  56. end