diff --git a/Gemfile b/Gemfile index 4aa8f6b..31ef50f 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ gem 'coffee-rails', '~> 4.2' # Use jquery as the JavaScript library gem 'jquery-rails' +gem 'jquery-ui-rails' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder diff --git a/Gemfile.lock b/Gemfile.lock index 3a41a6e..460400b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -63,6 +63,8 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + jquery-ui-rails (5.0.5) + railties (>= 3.2.16) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -158,6 +160,7 @@ DEPENDENCIES coffee-rails (~> 4.2) jbuilder (~> 2.5) jquery-rails + jquery-ui-rails listen (~> 3.0.5) puma (~> 3.0) rails (~> 5.0.0, >= 5.0.0.1) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index b12018d..d106681 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -11,6 +11,7 @@ // about supported directives. // //= require jquery +//= require jquery-ui/effect-blind //= require jquery_ujs //= require turbolinks //= require_tree . diff --git a/app/controllers/line_items_controller.rb b/app/controllers/line_items_controller.rb index b636c96..6159463 100644 --- a/app/controllers/line_items_controller.rb +++ b/app/controllers/line_items_controller.rb @@ -34,7 +34,7 @@ class LineItemsController < ApplicationController respond_to do |format| if @line_item.save format.html { redirect_to @line_item.cart } - format.js + format.js { @current_item = @line_item } format.json { render :show, status: :created, location: @line_item } else format.html { render :new } diff --git a/app/views/line_items/_line_item.html.erb b/app/views/line_items/_line_item.html.erb index 608b918..e66ce68 100644 --- a/app/views/line_items/_line_item.html.erb +++ b/app/views/line_items/_line_item.html.erb @@ -1,4 +1,8 @@ - +<% if line_item == @current_item %> + +<% else %> + +<% end %> <%= line_item.quantity %> × <%= line_item.product.title %> <%= number_to_currency(line_item.total_price) %> diff --git a/app/views/line_items/create.js.erb b/app/views/line_items/create.js.erb index 24cfafe..96a5666 100644 --- a/app/views/line_items/create.js.erb +++ b/app/views/line_items/create.js.erb @@ -1 +1,4 @@ $('#cart').html("<%=j render(@cart) %>") + +$('#current_item').css({'background-color':'#88ff88'}) + .animate({'background-color':'#114411'}, 1000) diff --git a/test/controllers/line_items_controller_test.rb b/test/controllers/line_items_controller_test.rb index c7bde77..3192ca8 100644 --- a/test/controllers/line_items_controller_test.rb +++ b/test/controllers/line_items_controller_test.rb @@ -26,6 +26,17 @@ class LineItemsControllerTest < ActionDispatch::IntegrationTest assert_select 'td', 'Programming Ruby 1.9' end + test "should create line_item via ajax" do + assert_difference('LineItem.count') do + post line_items_url, params: { product_id: products(:ruby).id }, xhr: true + end + + assert_response :success + assert_select_jquery :html, '#cart' do + assert_select 'tr#current_item td', /Programming Ruby 1.9/ + end + end + test "should show line_item" do get line_item_url(@line_item) assert_response :success