Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[admin] Add scopes and controller helpers for ui/table #5516

Merged
merged 14 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract common search patterns to a controller helper
This will be also the home for the upcoming scope support.

Co-Authored-By: Rainer Dema <[email protected]>
  • Loading branch information
elia and rainerdema committed Nov 24, 2023
commit cddacffceece7f2800e80c363f7c98ef2f06f696
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module SolidusAdmin::ControllerHelpers::Search
extend ActiveSupport::Concern

private

def apply_search_to(relation, param:)
apply_ransack_search_to(relation, param: param)
end

def apply_ransack_search_to(relation, param:)
relation
.ransack(params[param]&.except(:scope))
.result(distinct: true)
end
end
9 changes: 5 additions & 4 deletions admin/app/controllers/solidus_admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
module SolidusAdmin
class OrdersController < SolidusAdmin::BaseController
include Spree::Core::ControllerHelpers::StrongParameters
include SolidusAdmin::ControllerHelpers::Search

def index
orders = Spree::Order
.order(created_at: :desc, id: :desc)
.ransack(params[:q])
.result(distinct: true)
orders = apply_search_to(
Spree::Order.order(created_at: :desc, id: :desc),
param: :q,
)

set_page_and_extract_portion_from(
orders,
Expand Down
34 changes: 18 additions & 16 deletions admin/app/controllers/solidus_admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

module SolidusAdmin
class ProductsController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
products = apply_search_to(
Spree::Product.order(created_at: :desc, id: :desc),
param: :q,
)

set_page_and_extract_portion_from(
products,
per_page: SolidusAdmin::Config[:products_per_page]
)

respond_to do |format|
format.html { render component('products/index').new(page: @page) }
end
end

def edit
redirect_to action: :show
end
Expand Down Expand Up @@ -33,22 +51,6 @@ def update
end
end

def index
products = Spree::Product
.order(created_at: :desc, id: :desc)
.ransack(params[:q])
.result(distinct: true)

set_page_and_extract_portion_from(
products,
per_page: SolidusAdmin::Config[:products_per_page]
)

respond_to do |format|
format.html { render component('products/index').new(page: @page) }
end
end

def destroy
@products = Spree::Product.where(id: params[:id])

Expand Down
1 change: 1 addition & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

SolidusAdmin::Engine.routes.draw do
resource :account, only: :show

resources(
:products,
only: [:index, :show, :edit, :update],
Expand Down