-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '7811-categories' into develop
- Loading branch information
Showing
43 changed files
with
1,407 additions
and
2,093 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
## | ||
# Display and administer categories | ||
# | ||
class Admin::CategoriesController < AdminController | ||
include TranslatableParams | ||
|
||
before_action :set_root, expect: [:destroy, :reorder] | ||
before_action :set_category, only: [:edit, :update, :destroy, :reorder] | ||
|
||
def index | ||
end | ||
|
||
def new | ||
@category = Category.new | ||
@category.build_all_translations | ||
end | ||
|
||
def create | ||
@category = Category.new(category_params) | ||
if @category.save | ||
flash[:notice] = 'Category was successfully created.' | ||
redirect_to admin_categories_path | ||
else | ||
@category.build_all_translations | ||
render action: 'new' | ||
end | ||
end | ||
|
||
def edit | ||
@category.build_all_translations | ||
end | ||
|
||
def update | ||
if @category.update(category_params) | ||
flash[:notice] = 'Category was successfully updated.' | ||
redirect_to admin_categories_path | ||
else | ||
@category.build_all_translations | ||
render action: 'edit' | ||
end | ||
end | ||
|
||
def destroy | ||
@category.destroy | ||
flash[:notice] = 'Category was successfully destroyed.' | ||
redirect_to admin_categories_path | ||
end | ||
|
||
def reorder | ||
transaction = reorder_categories(params[:categories]) | ||
if transaction[:success] | ||
head :ok | ||
else | ||
render plain: transaction[:error], status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def reorder_categories(category_ids) | ||
error = nil | ||
ActiveRecord::Base.transaction do | ||
category_ids.each_with_index do |id, index| | ||
CategoryRelationship.find_by!(parent_id: @category.id, child_id: id). | ||
update(position: index + 1) | ||
rescue ActiveRecord::RecordNotFound | ||
error = "Couldn't find Category #{id}" | ||
raise ActiveRecord::Rollback | ||
end | ||
end | ||
{ success: error.nil?, error: error } | ||
end | ||
|
||
def category_params | ||
category_params = translatable_params( | ||
params.require(:category), | ||
translated_keys: [:locale, :title, :description], | ||
general_keys: [:category_tag, :parent_ids] | ||
) | ||
category_params[:parent_ids] ||= [@root.id] | ||
category_params | ||
end | ||
|
||
def set_root | ||
@root = Category.public_body_root | ||
end | ||
|
||
def set_category | ||
@category = Category.find(params[:id]) | ||
end | ||
end |
106 changes: 0 additions & 106 deletions
106
app/controllers/admin_public_body_categories_controller.rb
This file was deleted.
Oops, something went wrong.
120 changes: 0 additions & 120 deletions
120
app/controllers/admin_public_body_headings_controller.rb
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.