Skip to content

Commit 7813b6e

Browse files
author
Theresa
committed
Merge branch 'master' of github.com:openHPI/codeocean-classroom into refactor
2 parents 54a213d + 81dac43 commit 7813b6e

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

app/controllers/exercises_controller.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class ExercisesController < ApplicationController
44
load_and_authorize_resource
5-
before_action :set_exercise, only: [:show, :edit, :update, :destroy, :push_external]
5+
before_action :set_exercise, only: [:show, :edit, :update, :destroy, :add_to_cart,:push_external]
66

77
rescue_from CanCan::AccessDenied do |_exception|
88
redirect_to root_path, alert: 'You are not authorized for this exercise.'
@@ -78,6 +78,15 @@ def destroy
7878
end
7979
end
8080

81+
def add_to_cart
82+
unless current_user.cart
83+
Cart.create(user: current_user)
84+
end
85+
cart = Cart.find_by(user: current_user)
86+
cart.exercises << @exercise
87+
redirect_to @exercise, notice: 'Exercise was successfully added to your cart.'
88+
end
89+
8190
def exercises_all
8291
@exercises = Exercise.all
8392
end

app/models/ability.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def initialize(user)
99
can :show, Exercise
1010
can :create, Exercise
1111
can :duplicate, Exercise
12+
can :export, Exercise
13+
can :add_to_cart, Exercise
1214
can :read, Exercise
1315
can :edit, Exercise do |exercise|
1416
exercise.user == user

app/views/exercises/show.html.erb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<% end %>
6262
</dl>
6363

64-
<% if can? :manage, Exercise%>
64+
<% if can? :export, Exercise%>
6565
<dl class="dl-horizontal">
6666
<dt><%= 'Export to ... :' %></dt>
6767
<dd>
@@ -71,7 +71,17 @@
7171
<%= button_tag 'Submit', class: 'btn btn-success btn-xs' %>
7272
<% end %>
7373
</dd>
74+
75+
<dt>or<br><br></dt>
76+
<dd></dd>
77+
<dt>Add to cart :</dt>
78+
<dd>
79+
<%= button_to 'Add to cart', add_to_cart_path(@exercise), class: 'btn btn-primary btn-xs' %>
80+
</dd>
7481
</dl>
82+
83+
84+
7585
<% end %>
7686

7787
<div class="actions btn-group" role="group">
@@ -83,8 +93,3 @@
8393
<%= link_to 'Back', exercises_path, class: 'btn btn-default' %>
8494

8595
</div>
86-
87-
<script>
88-
89-
</script>
90-

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<%= menu_group pull: :right do %>
3232
<% if current_user %>
3333
<% if current_user.cart %>
34-
<%= menu_item 'Cart (' + current_user.cart_count.to_s + ')', current_user.cart %>
34+
<%= menu_item 'Cart (' + current_user.cart_count.to_s + ')', cart_path(current_user.cart) %>
3535
<% end %>
3636
<%= menu_item current_user.email, user_path(current_user) %>
3737
<%= menu_item 'Log out', logout_path, method: :delete %>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
get 'exercises/exercises_all'
1919

2020
get 'exercises/:id/duplicate', to: 'exercises#duplicate', as: 'duplicate_exercise'
21+
post 'exercises/:id/add_to_cart', to: 'exercises#add_to_cart', as: 'add_to_cart'
2122

2223
resources :labels
2324
resources :label_categories

0 commit comments

Comments
 (0)