Skip to content

Commit

Permalink
Merge pull request #6101 from chaimann/admin-promotion-categories-add…
Browse files Browse the repository at this point in the history
…-edit

Admin promotion categories add/edit
  • Loading branch information
tvdeyen authored Feb 14, 2025
2 parents ad4f5be + c87a012 commit a25b831
Show file tree
Hide file tree
Showing 37 changed files with 488 additions and 95 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Lint/AmbiguousBlockAssociation:
Exclude:
- "*/spec/**/*"
- "spec/**/*" # For the benefit of apps that inherit from this config
- "**/shared_examples/**/*"

# We use eval to add common_spree_dependencies into the Gemfiles of each of our gems
Security/Eval:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

RSpec.shared_examples_for 'promotion categories features' do
before { sign_in create(:admin_user, email: "[email protected]") }

it "lists promotion categories" do
create(factory_name, name: "test1", code: "code1")
create(factory_name, name: "test2", code: "code2")

visit index_path
expect(page).to have_content("test1")
expect(page).to have_content("test2")

expect(page).to be_axe_clean
end

it 'allows to create new promo category' do
visit index_path

click_on "Add new"
expect(turbo_frame_modal).to have_content("New Promotion Category")

fill_in "Code", with: "ste.1"
click_on "Add Promotion Category"

expect(turbo_frame_modal).to have_content("can't be blank")

fill_in "Name", with: "Soon to expire"
click_on "Add Promotion Category"

expect(page).to have_content("Promotion Category was successfully created.")
expect(page).to have_content("Soon to expire")
expect(page).to have_content("ste.1")
expect(model_class.count).to eq(1)
end

it 'allows to update promo category' do
create(factory_name, name: "Soon to expire", code: "ste.1")

visit index_path

click_on "Soon to expire"
expect(turbo_frame_modal).to have_content("Edit Promotion Category")

fill_in "Name", with: "Expired"
fill_in "Code", with: "exp.2"
click_on "Update Promotion Category"

expect(page).to have_content("Promotion Category was successfully updated.")
expect(page).to have_content("Expired")
expect(page).to have_content("exp.2")
end

it 'allows to delete promo category' do
create(factory_name, name: "Soon to expire", code: "ste.1")
create(factory_name, name: "Expired", code: "exp.2")

visit index_path

select_row("Expired")
click_on "Delete"
expect(page).to have_content("Promotion Categories were successfully removed.")
expect(page).not_to have_content("Expired")
expect(model_class.count).to eq(1)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# frozen_string_literal: true

RSpec.shared_examples_for 'promotion categories requests' do
let(:admin_user) { create(:admin_user) }

before do
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user)
end

describe "GET /index" do
it "renders the index template with a 200 OK status" do
get url_helpers.promotion_categories_path
expect(response).to have_http_status(:ok)
end
end

describe "GET /new" do
it "renders the new template with a 200 OK status" do
get url_helpers.new_promotion_category_path
expect(response).to have_http_status(:ok)
end
end

describe "POST /create" do
context "with valid parameters" do
let(:valid_attributes) { { name: "Expired", code: "exp.1" } }
let(:run_request) { post url_helpers.promotion_categories_path, params: { promotion_category: valid_attributes } }

it "creates a new promotion category" do
expect { run_request }.to change(model_class, :count).by(1)
end

it "redirects to the index page with a 303 See Other status" do
run_request
expect(response).to redirect_to(url_helpers.promotion_categories_path)
expect(response).to have_http_status(:see_other)
end

it "displays a success flash message" do
run_request
follow_redirect!
expect(response.body).to include("Promotion Category was successfully created.")
end
end

context "with invalid parameters" do
let(:invalid_attributes) { { name: "", code: "" } }
let(:run_request) { post url_helpers.promotion_categories_path, params: { promotion_category: invalid_attributes } }

it "does not create a new promotion category" do
expect { run_request }.not_to change(model_class, :count)
end

it "renders the new template with unprocessable_entity status" do
run_request
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

describe "GET /edit" do
it "renders the edit template with a 200 OK status" do
get url_helpers.edit_promotion_category_path(promotion_category)
expect(response).to have_http_status(:ok)
end
end

describe "PATCH /update" do
context "with valid parameters" do
let(:valid_attributes) { { name: "Updated", code: "upd.1" } }
let(:run_request) { patch url_helpers.promotion_category_path(promotion_category), params: { promotion_category: valid_attributes } }

it "updates the promotion category" do
run_request
promotion_category.reload
expect(promotion_category.name).to eq("Updated")
expect(promotion_category.code).to eq("upd.1")
end

it "redirects to the index page with a 303 See Other status" do
run_request
expect(response).to redirect_to(url_helpers.promotion_categories_path)
expect(response).to have_http_status(:see_other)
end

it "displays a success flash message" do
run_request
follow_redirect!
expect(response.body).to include("Promotion Category was successfully updated.")
end
end

context "with invalid parameters" do
let(:invalid_attributes) { { name: "", code: "" } }
let(:run_request) { patch url_helpers.promotion_category_path(promotion_category), params: { promotion_category: invalid_attributes } }

it "does not update the promotion category" do
expect { run_request }.not_to change { promotion_category.reload.name }
end

it "renders the edit template with unprocessable_entity status" do
run_request
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

describe "DELETE /destroy" do
before { promotion_category }

let(:run_request) { delete url_helpers.promotion_category_path(promotion_category) }

it "deletes the promotion category and redirects to the index page with a 303 See Other status" do
expect { run_request }.to change(model_class, :count).by(-1)

expect(response).to redirect_to(url_helpers.promotion_categories_path)
expect(response).to have_http_status(:see_other)
end

it "displays a success flash message after deletion" do
run_request
follow_redirect!
expect(response.body).to include("Promotion Categories were successfully removed.")
end
end
end
4 changes: 3 additions & 1 deletion admin/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
end
SimpleCov.command_name('solidus:admin')
SimpleCov.merge_timeout(3600)
SimpleCov.start('rails')
SimpleCov.start('rails') do
add_filter '/shared_examples/'
end
end

require 'solidus_admin'
Expand Down
10 changes: 10 additions & 0 deletions bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ echo "* Testing Solidus Sample *"
echo "**************************"
bin/rspec sample/spec

echo "**************************"
echo "* Testing Legacy Promotions *"
echo "**************************"
bin/rspec legacy_promotions/spec

echo "**************************"
echo "* Testing Solidus Promotions *"
echo "**************************"
bin/rspec promotions/spec

if [ -n "$COVERAGE" ]; then
# Generate coverage report
echo "******************************"
Expand Down
1 change: 1 addition & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LIBS = %w[
core
sample
legacy_promotions
promotions
]

# Ignore line info, e.g. foo/bar.rb:123 would become foo/bar.rb
Expand Down
10 changes: 10 additions & 0 deletions core/lib/spree/testing_support/capybara_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ def find_label_by_text(text)
# find the original.
find('label:not(.select2-offscreen)', text: /#{Regexp.escape(text)}/i, match: :one)
end

def dialog(parent: 'body', **options)
within(parent) do
find('dialog', visible: :all, **options)
end
end

def turbo_frame_modal
dialog(parent: find('turbo-frame', visible: :all))
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions legacy_promotions/config/locales/promotion_categories.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ en:
solidus_admin:
promotion_categories:
title: "Promotion Categories"
create:
success: "Promotion Category was successfully created."
update:
success: "Promotion Category was successfully updated."
destroy:
success: "Promotion Categories were successfully removed."
2 changes: 1 addition & 1 deletion legacy_promotions/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
extend SolidusAdmin::AdminResources

admin_resources :promotions, only: [:index, :destroy]
admin_resources :promotion_categories, only: [:index, :destroy]
admin_resources :promotion_categories, except: [:show]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= turbo_frame_tag :resource_modal, target: "_top" do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @promotion_category, url: form_url, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<%= render component("ui/forms/field").text_field(f, :code, class: "required") %>
</div>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
<% end %>
<% end %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class SolidusAdmin::PromotionCategories::Edit::Component < SolidusAdmin::Resources::Edit::Component
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
title: "Edit Promotion Category"
cancel: "Cancel"
submit: "Update Promotion Category"
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ def model_class
Spree::PromotionCategory
end

def row_url(promotion_category)
spree.edit_admin_promotion_category_path(promotion_category)
def title
t('solidus_admin.promotion_categories.title')
end

def edit_path(record)
solidus_admin.edit_promotion_category_path(record, **search_filter_params)
end

def turbo_frames
%w[resource_modal]
end

def page_actions
render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_promotion_category_path,
href: solidus_admin.new_promotion_category_path(**search_filter_params),
data: { turbo_frame: :resource_modal },
icon: "add-line",
)
end
Expand All @@ -22,7 +31,7 @@ def batch_actions
[
{
label: t('.batch_actions.delete'),
action: solidus_admin.promotion_categories_path,
action: solidus_admin.promotion_categories_path(**search_filter_params),
method: :delete,
icon: 'delete-bin-7-line',
},
Expand All @@ -39,17 +48,21 @@ def columns
def name_column
{
header: :name,
data: ->(promotion_category) do
content_tag :div, promotion_category.name
data: ->(record) do
link_to record.name, edit_path(record),
data: { turbo_frame: :resource_modal },
class: 'body-link'
end
}
end

def code_column
{
header: :code,
data: ->(promotion_category) do
content_tag :div, promotion_category.code
data: ->(record) do
link_to record.code, edit_path(record),
data: { turbo_frame: :resource_modal },
class: 'body-link'
end
}
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= turbo_frame_tag :resource_modal, target: "_top" do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @promotion_category, url: form_url, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<%= render component("ui/forms/field").text_field(f, :code, class: "required") %>
</div>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
<% end %>
<% end %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class SolidusAdmin::PromotionCategories::New::Component < SolidusAdmin::Resources::New::Component
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
title: "New Promotion Category"
cancel: "Cancel"
submit: "Add Promotion Category"
Loading

0 comments on commit a25b831

Please sign in to comment.