Skip to content

Commit

Permalink
Fix some rubocop issues in admin/order_cycles_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
luisramos0 committed Nov 5, 2019
1 parent beadb81 commit dd5d9d2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 27 deletions.
1 change: 0 additions & 1 deletion .rubocop_manual_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Metrics/LineLength:
- app/controllers/admin/enterprise_roles_controller.rb
- app/controllers/admin/inventory_items_controller.rb
- app/controllers/admin/manager_invitations_controller.rb
- app/controllers/admin/order_cycles_controller.rb
- app/controllers/admin/product_import_controller.rb
- app/controllers/admin/proxy_orders_controller.rb
- app/controllers/admin/schedules_controller.rb
Expand Down
94 changes: 68 additions & 26 deletions app/controllers/admin/order_cycles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def index
respond_to do |format|
format.html
format.json do
render_as_json @collection, ams_prefix: params[:ams_prefix], current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection)
render_as_json @collection,
ams_prefix: params[:ams_prefix],
current_user: spree_current_user,
subscriptions_count: SubscriptionsCount.new(@collection)
end
end
end
Expand Down Expand Up @@ -41,7 +44,8 @@ def create

if @order_cycle_form.save
flash[:notice] = I18n.t(:order_cycles_create_notice)
render json: { success: true, edit_path: main_app.admin_order_cycle_incoming_path(@order_cycle) }
render json: { success: true,
edit_path: main_app.admin_order_cycle_incoming_path(@order_cycle) }
else
render json: { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity
end
Expand All @@ -67,7 +71,10 @@ def update

def bulk_update
if order_cycle_set.andand.save
render_as_json @order_cycles, ams_prefix: 'index', current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection)
render_as_json @order_cycles,
ams_prefix: 'index',
current_user: spree_current_user,
subscriptions_count: SubscriptionsCount.new(@collection)
else
order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }
render json: { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity
Expand All @@ -77,35 +84,26 @@ def bulk_update
def clone
@order_cycle = OrderCycle.find params[:id]
@order_cycle.clone!
redirect_to main_app.admin_order_cycles_path, notice: I18n.t(:order_cycles_clone_notice, name: @order_cycle.name)
redirect_to main_app.admin_order_cycles_path,
notice: I18n.t(:order_cycles_clone_notice, name: @order_cycle.name)
end

# Send notifications to all producers who are part of the order cycle
def notify_producers
Delayed::Job.enqueue OrderCycleNotificationJob.new(params[:id].to_i)

redirect_to main_app.admin_order_cycles_path, notice: I18n.t(:order_cycles_email_to_producers_notice)
redirect_to main_app.admin_order_cycles_path,
notice: I18n.t(:order_cycles_email_to_producers_notice)
end

protected

def collection
return Enterprise.where("1=0") unless json_request?
return order_cycles_from_set if params[:order_cycle_set]
ocs = if params[:as] == "distributor"
OrderCycle.preload(:schedules).ransack(params[:q]).result.
involving_managed_distributors_of(spree_current_user).order('updated_at DESC')
elsif params[:as] == "producer"
OrderCycle.preload(:schedules).ransack(params[:q]).result.
involving_managed_producers_of(spree_current_user).order('updated_at DESC')
else
OrderCycle.preload(:schedules).ransack(params[:q]).result.accessible_by(spree_current_user)
end

ocs.undated |
ocs.soonest_closing |
ocs.soonest_opening |
ocs.closed
ocs = order_cycles
ocs.undated | ocs.soonest_closing | ocs.soonest_opening | ocs.closed
end

def collection_actions
Expand All @@ -114,21 +112,60 @@ def collection_actions

private

def order_cycles
if params[:as] == "distributor"
order_cycles_as_distributor
elsif params[:as] == "producer"
order_cycles_as_producer
else
order_cycles_as_both
end
end

def order_cycles_as_distributor
OrderCycle.
preload(:schedules).
ransack(params[:q]).
result.
involving_managed_distributors_of(spree_current_user).
order('updated_at DESC')
end

def order_cycles_as_producer
OrderCycle.
preload(:schedules).
ransack(params[:q]).
result.
involving_managed_producers_of(spree_current_user).
order('updated_at DESC')
end

def order_cycles_as_both
OrderCycle.
preload(:schedules).
ransack(params[:q]).
result.
accessible_by(spree_current_user)
end

def load_data_for_index
if json_request?
# Split ransack params into all those that currently exist and new ones to limit returned ocs to recent or undated
# Split ransack params into all those that currently exist and new ones
# to limit returned ocs to recent or undated
orders_close_at_gt = params[:q].andand.delete(:orders_close_at_gt) || 31.days.ago
params[:q] = {
g: [params.delete(:q) || {}, { m: 'or', orders_close_at_gt: orders_close_at_gt, orders_close_at_null: true }]
g: [params.delete(:q) || {}, { m: 'or',
orders_close_at_gt: orders_close_at_gt,
orders_close_at_null: true }]
}
@collection = collection
end
end

def require_coordinator
if params[:coordinator_id] && @order_cycle.coordinator = permitted_coordinating_enterprises_for(@order_cycle).find_by_id(params[:coordinator_id])
return
end
@order_cycle.coordinator =
permitted_coordinating_enterprises_for(@order_cycle).find_by_id(params[:coordinator_id])
return if params[:coordinator_id] && @order_cycle.coordinator

available_coordinators = permitted_coordinating_enterprises_for(@order_cycle)
case available_coordinators.count
Expand All @@ -138,7 +175,9 @@ def require_coordinator
when 1
@order_cycle.coordinator = available_coordinators.first
else
flash[:error] = I18n.t(:order_cycles_no_permission_to_create_error) if params[:coordinator_id]
if params[:coordinator_id]
flash[:error] = I18n.t(:order_cycles_no_permission_to_create_error)
end
render :set_coordinator
end
end
Expand All @@ -162,7 +201,9 @@ def remove_protected_attrs
params[:order_cycle].delete :coordinator_id

unless Enterprise.managed_by(spree_current_user).include?(@order_cycle.coordinator)
params[:order_cycle].delete_if{ |k, _v| [:name, :orders_open_at, :orders_close_at].include? k.to_sym }
params[:order_cycle].delete_if do |k, _v|
[:name, :orders_open_at, :orders_close_at].include? k.to_sym
end
end
end

Expand All @@ -186,7 +227,8 @@ def order_cycle_set

def require_order_cycle_set_params
return if params[:order_cycle_set]
render json: { errors: t('admin.order_cycles.bulk_update.no_data') }, status: :unprocessable_entity
render json: { errors: t('admin.order_cycles.bulk_update.no_data') },
status: :unprocessable_entity
end

def ams_prefix_whitelist
Expand Down

0 comments on commit dd5d9d2

Please sign in to comment.