Skip to content

Instantly share code, notes, and snippets.

@laspluviosillas
Created January 31, 2014 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laspluviosillas/8744782 to your computer and use it in GitHub Desktop.
Save laspluviosillas/8744782 to your computer and use it in GitHub Desktop.
# POST
def check_assignation
@inventory_ids = params[:inv]
@inventories = OleCore::Inventory.includes(:course, account: {subscription: :subscription_plan})
.where(id: params[:inv])
prevent_empty_trainees_and_courses
# If this assignation doesn't require addons, no confirmation required either.
if !CourseAssigner.requires_addons?(@inventories, @checked_users, current_user)
@confirmation = OpenStruct.new(needs_confirmation: false)
return
end
user_count = User.outside_subscription.where(login_id: @checked_users).count
new_price = current_subscription.price_for_extra_users(user_count)
if new_price > 0
@confirmation = OpenStruct.new(needs_confirmation: true, extra_amount: new_price)
else
@confirmation = OpenStruct.new(needs_confirmation: false)
end
rescue AllocateException => e
flash.now[:alert] = e.to_s
end
private
def prevent_empty_trainees_and_courses
raise AllocateException.new t(:please_select_at_least_one_course_and_one_trainee) if @inventories.empty? && @checked_users.nil?
raise AllocateException.new t(:please_select_at_least_one_course_to_assign) if @inventories.empty?
raise AllocateException.new t(:please_select_at_least_one_trainee_to_assign_units_to) if @checked_users.nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment