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::ReportsController reusable search params #2012

Merged
merged 1 commit into from
Jun 20, 2017
Merged
Changes from all commits
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
45 changes: 24 additions & 21 deletions backend/app/controllers/spree/admin/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -26,27 +26,7 @@ def index
end

def sales_total
params[:q] = {} unless params[:q]

if params[:q][:completed_at_gt].blank?
params[:q][:completed_at_gt] = Time.current.beginning_of_month
else
params[:q][:completed_at_gt] = begin
Time.zone.parse(params[:q][:completed_at_gt]).beginning_of_day
rescue
Time.current.beginning_of_month
end
end

if params[:q] && !params[:q][:completed_at_lt].blank?
params[:q][:completed_at_lt] = begin
Time.zone.parse(params[:q][:completed_at_lt]).end_of_day
rescue
""
end
end

params[:q][:s] ||= "completed_at desc"
params[:q] = search_params

@search = Order.complete.ransack(params[:q])
@orders = @search.result
@@ -68,6 +48,29 @@ def sales_total
end

@@available_reports = {}

private

def search_params
params.fetch(:q, {}).tap do |q|
q[:completed_at_gt] = adjust_start_date q[:completed_at_gt]
q[:completed_at_lt] = adjust_end_date(q[:completed_at_lt]) if q[:completed_at_lt].present?
q[:s] ||= 'completed_at desc'
end
end

def adjust_start_date(string_date = nil)
return Time.current.beginning_of_month if string_date.blank?
Time.zone.parse(string_date).beginning_of_day
rescue ArgumentError
Time.current.beginning_of_month
end

def adjust_end_date(string_date)
Time.zone.parse(string_date).end_of_day
rescue ArgumentError
""
end
end
end
end