Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Open source admin controller concerns

Damian Legawiec61a8b478d455
diff --git a/admin/app/controllers/concerns/spree/admin/analytics_concern.rb b/admin/app/controllers/concerns/spree/admin/analytics_concern.rb
new file mode 100644
index 0000000000..c361b83df2
--- /dev/null
+++ b/admin/app/controllers/concerns/spree/admin/analytics_concern.rb
@@ -0,0 +1,51 @@
+module Spree
+ module Admin
+ module AnalyticsConcern
+ extend ActiveSupport::Concern
+
+ included do
+ helper_method :analytics_time_range, :same_day?
+ end
+
+ def set_analytics_defaults
+ params[:analytics_currency] ||= current_currency
+
+ params[:date_from] = if params[:date_from].present?
+ params[:date_from].to_date&.in_time_zone(current_timezone)
+ else
+ 1.month.ago.end_of_day.to_date.in_time_zone(current_timezone)
+ end
+
+ params[:date_to] = if params[:date_to].present?
+ params[:date_to].to_date&.in_time_zone(current_timezone)
+ else
+ Time.zone.now.beginning_of_day.to_date.in_time_zone(current_timezone)
+ end
+ end
+
+ def analytics_time_range
+ @analytics_time_range ||= (params[:date_from].to_time.beginning_of_day)..(params[:date_to].to_time.end_of_day)
+ end
+
+ def previous_analytics_time_range
+ duration = analytics_time_range.last - analytics_time_range.first
+
+ @previous_analytics_time_range ||= analytics_time_range.first.ago(duration)..analytics_time_range.last.ago(duration).end_of_day
+ end
+
+ def same_day?
+ analytics_time_range.first.to_date == analytics_time_range.last.to_date
+ end
+
+ def calc_growth_rate(current_amount, previous_amount)
+ if previous_amount.zero? && current_amount.positive?
+ 100
+ elsif current_amount.zero? && previous_amount.zero?
+ 0
+ else
+ ((current_amount.to_f / previous_amount) * 100) - 100
+ end
+ end
+ end
+ end
+end
diff --git a/admin/app/controllers/concerns/spree/admin/bulk_operations_concern.rb b/admin/app/controllers/concerns/spree/admin/bulk_operations_concern.rb
new file mode 100644
index 0000000000..97930840fa
--- /dev/null
+++ b/admin/app/controllers/concerns/spree/admin/bulk_operations_concern.rb
@@ -0,0 +1,42 @@
+module Spree
+ module Admin
+ module BulkOperationsConcern
+ extend ActiveSupport::Concern
+
+ def bulk_add_tags
+ Spree::Tags::BulkAdd.call(tag_names: params[:tags], records: bulk_collection)
+
+ handle_bulk_operation_response
+ end
+
+ def bulk_remove_tags
+ Spree::Tags::BulkRemove.call(tag_names: params[:tags], records: bulk_collection)
+
+ handle_bulk_operation_response
+ end
+
+ private
+
+ def collection_actions
+ [:index, :bulk_modal, :bulk_add_tags, :bulk_remove_tags, :bulk_destroy]
+ end
+
+ def handle_bulk_operation_response
+ respond_to do |format|
+ format.turbo_stream do
+ flash.now[:success] = flash_message_for(bulk_collection, :successfully_updated)
+ render partial: 'spree/admin/shared/close_bulk_modal'
+ end
+ format.html do
+ flash[:success] = flash_message_for(bulk_collection, :successfully_updated)
+ redirect_to request.referer
+ end
+ end
+ end
+
+ def bulk_collection
+ @bulk_collection ||= model_class.accessible_by(current_ability, :update).where(id: params[:ids])
+ end
+ end
+ end
+end
diff --git a/admin/app/controllers/concerns/spree/admin/order_concern.rb b/admin/app/controllers/concerns/spree/admin/order_concern.rb
new file mode 100644
index 0000000000..da44a7a94c
--- /dev/null
+++ b/admin/app/controllers/concerns/spree/admin/order_concern.rb
@@ -0,0 +1,24 @@
+module Spree
+ module Admin
+ module OrderConcern
+ extend ActiveSupport::Concern
+
+ included do
+ rescue_from ActiveRecord::RecordNotFound, with: :resource_not_found
+ end
+
+ protected
+
+ def load_order
+ @order = current_store.orders.find_by!(number: params[:order_id])
+ authorize! action, @order
+ @order
+ end
+
+ def resource_not_found
+ flash[:error] = flash_message_for(model_class.new, :not_found)
+ redirect_to spree.admin_orders_path
+ end
+ end
+ end
+end
diff --git a/admin/app/views/spree/admin/shared/_close_bulk_modal.turbo_stream.erb b/admin/app/views/spree/admin/shared/_close_bulk_modal.turbo_stream.erb
new file mode 100644
index 0000000000..13bc7e86db
--- /dev/null
+++ b/admin/app/views/spree/admin/shared/_close_bulk_modal.turbo_stream.erb
@@ -0,0 +1,2 @@
+<%= turbo_render_alerts %>
+<%= turbo_close_modal('bulk-modal') %>
\ No newline at end of file