Skip to content

Commit

Permalink
Settings::FavouriteTagsControllerをリファクタ
Browse files Browse the repository at this point in the history
過剰なbefore_actionを削除
  • Loading branch information
takayamaki committed Jan 9, 2025
1 parent 438c16c commit 72ed467
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions app/controllers/settings/favourite_tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,50 @@
class Settings::FavouriteTagsController < Settings::BaseController
layout 'admin'
before_action :authenticate_user!
before_action :set_account
before_action :set_favourite_tags, only: [:index, :create]
before_action :set_favourite_tag, only: [:edit, :update, :destroy]

def index
@favourite_tags = current_account.favourite_tags.with_order
@favourite_tag = FavouriteTag.new(visibility: FavouriteTag.visibilities[:public])
end

def edit
@favourite_tag
@favourite_tag = current_account.favourite_tags.find(params[:id])
end

def create
name = create_params[:name].delete_prefix('#')
@favourite_tag = FavouriteTag.new(account: @account, name: name, order: create_params[:order], visibility: create_params[:visibility])
@favourite_tag = FavouriteTag.new(
account: current_account,
name: create_params[:name].delete_prefix('#'),
order: create_params[:order],
visibility: create_params[:visibility]
)

if @favourite_tag.save
redirect_to settings_favourite_tags_path, notice: I18n.t('generic.changes_saved_msg')
else
@favourite_tags = current_account.favourite_tags.with_order
render :index
end
end

def update
name = update_params[:name].delete_prefix('#')
if @favourite_tag.update(name: name, order: update_params[:order], visibility: update_params[:visibility])
@favourite_tag = current_account.favourite_tags.find(params[:id])

@favourite_tag.update(
name: update_params[:name].delete_prefix('#'),
order: update_params[:order],
visibility: update_params[:visibility]
)

if @favourite_tag.save
redirect_to settings_favourite_tags_path, notice: I18n.t('generic.changes_saved_msg')
else
render :edit
end
end

def destroy
@favourite_tag.destroy
current_account.favourite_tags.destroy(params[:id])
redirect_to settings_favourite_tags_path
end

Expand All @@ -49,15 +60,7 @@ def update_params
params.require(:favourite_tag).permit(:name, :visibility, :order)
end

def set_account
@account = current_user.account
end

def set_favourite_tag
@favourite_tag = @account.favourite_tags.find(params[:id])
end

def set_favourite_tags
@favourite_tags = @account.favourite_tags.with_order
def current_account
current_user.account
end
end

0 comments on commit 72ed467

Please sign in to comment.