Skip to content

Commit

Permalink
fixup! Add new admin category controller and views
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Nov 23, 2023
1 parent 9a518d4 commit fe410bc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Admin::CategoriesController < AdminController
include TranslatableParams

before_action :set_root
before_action :set_root, expect: [:destroy, :reorder]
before_action :set_category, only: [:edit, :update, :destroy, :reorder]

def index
Expand Down
65 changes: 45 additions & 20 deletions spec/controllers/admin/categories_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
expect(response).to be_successful
end

it 'assigns root' do
get :index
expect(assigns(:root)).to eq(Category.public_body_root)
end

it 'assigns category tree' do
get :index
expect(assigns(:category_tree)).to eq(Category.public_body_tree)
Expand All @@ -19,6 +24,11 @@
end

describe 'GET new' do
it 'assigns root' do
get :new
expect(assigns(:root)).to eq(Category.public_body_root)
end

it 'responds successfully' do
get :new
expect(response).to be_successful
Expand All @@ -42,6 +52,11 @@
end

describe 'POST create' do
it 'assigns root' do
post :create, params: { category: { title: 'Title' } }
expect(assigns(:root)).to eq(Category.public_body_root)
end

context 'on success' do
let(:params) do
{
Expand Down Expand Up @@ -159,6 +174,11 @@
)
end

it 'assigns root' do
get :edit, params: { id: category.id }
expect(assigns(:root)).to eq(Category.public_body_root)
end

it 'responds successfully' do
get :edit, params: { id: category.id }
expect(response).to be_successful
Expand Down Expand Up @@ -209,14 +229,19 @@
}
end

it 'assigns root' do
patch :update, params: { id: category.id, category: params }
expect(assigns(:root)).to eq(Category.public_body_root)
end

it 'finds the category to update' do
post :update, params: { id: category.id, category: params }
patch :update, params: { id: category.id, category: params }
expect(assigns(:category)).to eq(category)
end

it "saves edits to a category's parent associations" do
new_parent = FactoryBot.create(:category)
post :update, params: {
patch :update, params: {
id: category.id,
category: { parent_id: new_parent.id }
}
Expand All @@ -229,15 +254,15 @@
end

it 'does not save edits to category' do
post :update, params: {
patch :update, params: {
id: category.id,
category: { category_tag: 'renamed' }
}
expect(assigns(:category).valid?).to eq(false)
end

it 'renders the edit action' do
post :update, params: {
patch :update, params: {
id: category.id,
category: { category_tag: 'renamed' }
}
Expand All @@ -259,24 +284,24 @@
end

it 'saves edits to a category' do
post :update, params: { id: category.id, category: params }
patch :update, params: { id: category.id, category: params }
expect(assigns(:category).title).to eq('Renamed')
end

it 'notifies the admin that the category was created' do
post :update, params: { id: category.id, category: params }
patch :update, params: { id: category.id, category: params }
expect(flash[:notice]).to eq('Category was successfully updated.')
end

it 'redirects to the category edit page' do
post :update, params: { id: category.id, category: params }
patch :update, params: { id: category.id, category: params }
expect(response).to redirect_to(admin_categories_path)
end

it 'saves edits to category_tag if the category has no associated bodies' do
category = FactoryBot.create(:category, category_tag: 'empty')

post :update, params: {
patch :update, params: {
id: category.id,
category: { category_tag: 'Renamed' }
}
Expand All @@ -287,7 +312,7 @@
it "creates a new translation if there isn't one for the default_locale" do
AlaveteliLocalization.set_locales('es en_GB', 'en_GB')

post :update, params: {
patch :update, params: {
id: category.id,
category: { title: 'Category en_GB' }
}
Expand Down Expand Up @@ -316,7 +341,7 @@
end

it 'saves edits to a category in another locale' do
post :update, params: { id: category.id, category: params }
patch :update, params: { id: category.id, category: params }
expect(assigns(:category).title(:es)).to eq('Renamed')
expect(assigns(:category).title(:en)).to eq(category.title(:en))
end
Expand All @@ -336,7 +361,7 @@
end

it 'adds multiple new translations' do
post :update, params: {
patch :update, params: {
id: category.id,
category: {
translations_attributes: {
Expand All @@ -352,7 +377,7 @@
end

it 'updates an existing translation and adds a translation' do
post :update, params: {
patch :update, params: {
id: category.id,
category: {
translations_attributes: {
Expand All @@ -372,7 +397,7 @@
end

it 'redirects to the edit page after a successful update' do
post :update, params: { id: category.id, category: { title: 'Title' } }
patch :update, params: { id: category.id, category: { title: 'Title' } }
expect(response).to redirect_to(admin_categories_path)
end
end
Expand All @@ -383,12 +408,12 @@
end

it 'renders the form if creating the record was unsuccessful' do
post :update, params: { id: category.id, category: { title: 'Title' } }
patch :update, params: { id: category.id, category: { title: 'Title' } }
expect(response).to render_template('edit')
end

it 'is rebuilt with the given params' do
post :update, params: { id: category.id, category: { title: 'Title' } }
patch :update, params: { id: category.id, category: { title: 'Title' } }
expect(assigns(:category).title).to eq('Title')
end
end
Expand Down Expand Up @@ -417,12 +442,12 @@
end

it 'is rebuilt with the default locale translation' do
post :update, params: { id: category.id, category: params }
patch :update, params: { id: category.id, category: params }
expect(assigns(:category).title(:en)).to eq('My Updated Category')
end

it 'is rebuilt with the alternative locale translation' do
post :update, params: { id: category.id, category: params }
patch :update, params: { id: category.id, category: params }
expect(assigns(:category).title(:es)).to eq('Mi Categoria Actualizada')
end
end
Expand Down Expand Up @@ -461,15 +486,15 @@
let!(:category_2) { FactoryBot.create(:category, parent: parent) }

it 'responds successfully' do
post :reorder, params: {
patch :reorder, params: {
id: parent.id, categories: [category_2.id, category_1.id]
}
expect(response).to be_successful
end

it 'reorders existing child categories' do
expect {
post :reorder, params: {
patch :reorder, params: {
id: parent.id, categories: [category_2.id, category_1.id]
}
}.to(
Expand All @@ -481,7 +506,7 @@

it 'returns error if trying to reorder non-child categories' do
other_category = FactoryBot.create(:category)
post :reorder, params: {
patch :reorder, params: {
id: parent.id, categories: [category_1.id, other_category.id]
}
expect(response).to_not be_successful
Expand Down

0 comments on commit fe410bc

Please sign in to comment.