custom_emojis_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # frozen_string_literal: true
  2. module Admin
  3. class CustomEmojisController < BaseController
  4. before_action :set_custom_emoji, except: [:index, :new, :create]
  5. before_action :set_filter_params
  6. def index
  7. authorize :custom_emoji, :index?
  8. @custom_emojis = filtered_custom_emojis.eager_load(:local_counterpart).page(params[:page])
  9. end
  10. def new
  11. authorize :custom_emoji, :create?
  12. @custom_emoji = CustomEmoji.new
  13. end
  14. def create
  15. authorize :custom_emoji, :create?
  16. @custom_emoji = CustomEmoji.new(resource_params)
  17. if @custom_emoji.save
  18. log_action :create, @custom_emoji
  19. redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.created_msg')
  20. else
  21. render :new
  22. end
  23. end
  24. def update
  25. authorize @custom_emoji, :update?
  26. if @custom_emoji.update(resource_params)
  27. log_action :update, @custom_emoji
  28. flash[:notice] = I18n.t('admin.custom_emojis.updated_msg')
  29. else
  30. flash[:alert] = I18n.t('admin.custom_emojis.update_failed_msg')
  31. end
  32. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  33. end
  34. def destroy
  35. authorize @custom_emoji, :destroy?
  36. @custom_emoji.destroy!
  37. log_action :destroy, @custom_emoji
  38. flash[:notice] = I18n.t('admin.custom_emojis.destroyed_msg')
  39. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  40. end
  41. def copy
  42. authorize @custom_emoji, :copy?
  43. emoji = CustomEmoji.find_or_initialize_by(domain: nil,
  44. shortcode: @custom_emoji.shortcode)
  45. emoji.image = @custom_emoji.image
  46. if emoji.save
  47. log_action :create, emoji
  48. flash[:notice] = I18n.t('admin.custom_emojis.copied_msg')
  49. else
  50. flash[:alert] = I18n.t('admin.custom_emojis.copy_failed_msg')
  51. end
  52. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  53. end
  54. def enable
  55. authorize @custom_emoji, :enable?
  56. @custom_emoji.update!(disabled: false)
  57. log_action :enable, @custom_emoji
  58. flash[:notice] = I18n.t('admin.custom_emojis.enabled_msg')
  59. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  60. end
  61. def disable
  62. authorize @custom_emoji, :disable?
  63. @custom_emoji.update!(disabled: true)
  64. log_action :disable, @custom_emoji
  65. flash[:notice] = I18n.t('admin.custom_emojis.disabled_msg')
  66. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  67. end
  68. private
  69. def set_custom_emoji
  70. @custom_emoji = CustomEmoji.find(params[:id])
  71. end
  72. def set_filter_params
  73. @filter_params = filter_params.to_hash.symbolize_keys
  74. end
  75. def resource_params
  76. params.require(:custom_emoji).permit(:shortcode, :image, :visible_in_picker)
  77. end
  78. def filtered_custom_emojis
  79. CustomEmojiFilter.new(filter_params).results
  80. end
  81. def filter_params
  82. params.permit(
  83. :local,
  84. :remote,
  85. :by_domain,
  86. :shortcode
  87. )
  88. end
  89. end
  90. end