canonical_email_blocks_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. class Api::V1::Admin::CanonicalEmailBlocksController < Api::BaseController
  3. include Authorization
  4. include AccountableConcern
  5. LIMIT = 100
  6. before_action -> { authorize_if_got_token! :'admin:read', :'admin:read:canonical_email_blocks' }, only: [:index, :show, :test]
  7. before_action -> { authorize_if_got_token! :'admin:write', :'admin:write:canonical_email_blocks' }, except: [:index, :show, :test]
  8. before_action :set_canonical_email_blocks, only: :index
  9. before_action :set_canonical_email_blocks_from_test, only: [:test]
  10. before_action :set_canonical_email_block, only: [:show, :destroy]
  11. after_action :verify_authorized
  12. after_action :insert_pagination_headers, only: :index
  13. PAGINATION_PARAMS = %i(limit).freeze
  14. def index
  15. authorize :canonical_email_block, :index?
  16. render json: @canonical_email_blocks, each_serializer: REST::Admin::CanonicalEmailBlockSerializer
  17. end
  18. def show
  19. authorize @canonical_email_block, :show?
  20. render json: @canonical_email_block, serializer: REST::Admin::CanonicalEmailBlockSerializer
  21. end
  22. def test
  23. authorize :canonical_email_block, :test?
  24. render json: @canonical_email_blocks, each_serializer: REST::Admin::CanonicalEmailBlockSerializer
  25. end
  26. def create
  27. authorize :canonical_email_block, :create?
  28. @canonical_email_block = CanonicalEmailBlock.create!(resource_params)
  29. log_action :create, @canonical_email_block
  30. render json: @canonical_email_block, serializer: REST::Admin::CanonicalEmailBlockSerializer
  31. end
  32. def destroy
  33. authorize @canonical_email_block, :destroy?
  34. @canonical_email_block.destroy!
  35. log_action :destroy, @canonical_email_block
  36. render json: @canonical_email_block, serializer: REST::Admin::CanonicalEmailBlockSerializer
  37. end
  38. private
  39. def resource_params
  40. params.permit(:canonical_email_hash, :email)
  41. end
  42. def set_canonical_email_blocks
  43. @canonical_email_blocks = CanonicalEmailBlock.order(id: :desc).to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
  44. end
  45. def set_canonical_email_blocks_from_test
  46. @canonical_email_blocks = CanonicalEmailBlock.matching_email(params[:email])
  47. end
  48. def set_canonical_email_block
  49. @canonical_email_block = CanonicalEmailBlock.find(params[:id])
  50. end
  51. def insert_pagination_headers
  52. set_pagination_headers(next_path, prev_path)
  53. end
  54. def next_path
  55. api_v1_admin_canonical_email_blocks_url(pagination_params(max_id: pagination_max_id)) if records_continue?
  56. end
  57. def prev_path
  58. api_v1_admin_canonical_email_blocks_url(pagination_params(min_id: pagination_since_id)) unless @canonical_email_blocks.empty?
  59. end
  60. def pagination_max_id
  61. @canonical_email_blocks.last.id
  62. end
  63. def pagination_since_id
  64. @canonical_email_blocks.first.id
  65. end
  66. def records_continue?
  67. @canonical_email_blocks.size == limit_param(LIMIT)
  68. end
  69. def pagination_params(core_params)
  70. params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
  71. end
  72. end