diff --git a/app/assets/stylesheets/admin/redeem_requests.scss b/app/assets/stylesheets/admin/redeem_requests.scss index 52998458..7c07fa71 100644 --- a/app/assets/stylesheets/admin/redeem_requests.scss +++ b/app/assets/stylesheets/admin/redeem_requests.scss @@ -3,4 +3,8 @@ // You can use Sass (SCSS) here: http://sass-lang.com/ .slide{ float: right; +} + +.btn.btn-primary.glyphicon.glyphicon-download{ + float: right; } \ No newline at end of file diff --git a/app/controllers/admin/redeem_requests_controller.rb b/app/controllers/admin/redeem_requests_controller.rb index a95acda9..82ffd81b 100644 --- a/app/controllers/admin/redeem_requests_controller.rb +++ b/app/controllers/admin/redeem_requests_controller.rb @@ -2,13 +2,11 @@ class Admin::RedeemRequestsController < ApplicationController include Admin::RedeemRequestsHelper before_action :authenticate_user! before_action :authenticate_admin! + before_action :load_redeem_request, only: [:index, :download] def index - #status(false) = status(Open) - #status(true) = status(Close) - @status = params[:status] ? params[:status] : false - @redeem_requests = RedeemRequest.where(:status => @status).desc(:created_at).page(params[:page]) + @redeem_requests = @redeem_requests.page(params[:page]) if request.xhr? respond_to do|format| format.js @@ -32,10 +30,28 @@ def destroy redirect_to admin_redeem_requests_path end + def download + csv_string = CSV.generate do |csv| + csv << ["User", "Gift Shop", "Store", "Points", "Cost", "Date", "Coupon Code", "Address", "Status"] + @redeem_requests.each do |redeem_request| + csv << [redeem_request.user.email, redeem_request.retailer, redeem_request.store, + redeem_request.points, redeem_request.points/REDEEM['one_dollar_to_points'], + redeem_request.updated_at.strftime(fmt='%F %T'), redeem_request.coupon_code, + redeem_request.address, redeem_request.status] + end + end + send_data csv_string, type: 'text/csv; header = present;', disposition: "filename = requests.csv" + end + private def redeem_params params.fetch(:redeem_request).permit(:coupon_code, :comment, :points, :status) end + def load_redeem_request + @status = params[:status] || false + @redeem_requests = RedeemRequest.where(status: @status).desc(:created_at) + end + end diff --git a/app/helpers/admin/redeem_requests_helper.rb b/app/helpers/admin/redeem_requests_helper.rb index 8d5bd72e..620cc23f 100644 --- a/app/helpers/admin/redeem_requests_helper.rb +++ b/app/helpers/admin/redeem_requests_helper.rb @@ -1,7 +1,7 @@ module Admin::RedeemRequestsHelper def total_capital_of_points - RedeemRequest.where(status: false).sum(:points)/10 + RedeemRequest.where(status: false).sum(:points)/REDEEM['one_dollar_to_points'] end end diff --git a/app/views/admin/redeem_requests/_redeem_request.html.haml b/app/views/admin/redeem_requests/_redeem_request.html.haml index 2cdbbcd5..e2750f5d 100644 --- a/app/views/admin/redeem_requests/_redeem_request.html.haml +++ b/app/views/admin/redeem_requests/_redeem_request.html.haml @@ -7,6 +7,7 @@ = redeem_request.retailer.upcase %td= redeem_request.store? ? redeem_request.store.upcase : "NA" %td= redeem_request.points + %td= redeem_request.points/10 %td= redeem_request.created_at.strftime('%D %T') %td= redeem_request.coupon_code %td= redeem_request.address diff --git a/app/views/admin/redeem_requests/_tagtable.html.haml b/app/views/admin/redeem_requests/_tagtable.html.haml index f25d12df..93127959 100644 --- a/app/views/admin/redeem_requests/_tagtable.html.haml +++ b/app/views/admin/redeem_requests/_tagtable.html.haml @@ -4,6 +4,7 @@ %th.col-xs-1 Gift Shop %th.col-xs-1 Store %th.col-xs-1 Points + %th.col-xs-1 Cost($) %th.col-xs-2 Date %th.col-xs-2 Coupon Code %th.col-xs-1 Address diff --git a/app/views/admin/redeem_requests/index.html.haml b/app/views/admin/redeem_requests/index.html.haml index eaa2bb50..31838a04 100644 --- a/app/views/admin/redeem_requests/index.html.haml +++ b/app/views/admin/redeem_requests/index.html.haml @@ -1,10 +1,15 @@ %section.content-header - %h1 + %h1 Redeem Requests %span.badge $ #{total_capital_of_points} %label.slide %input.check#redeem{checked: 'checked', name: 'status', type: 'checkbox', data: {toggle: 'toggle', width: '100', height: '40', on: 'Status Open', off: 'Status Close'}} #console-event + = link_to download_admin_redeem_requests_path(:status => "true"), class: "btn btn-primary glyphicon glyphicon-download" do + %b Completed + = link_to download_admin_redeem_requests_path(:status => "false"), class: "btn btn-primary glyphicon glyphicon-download" do + %b Active + %section.content .row .col-xs-12 @@ -26,4 +31,4 @@ = f.input :status, as: :select, collection: { 'Closed' => 'true', 'Open' => 'false'}, include_blank: false .modal-footer %button.btn.btn-default{'data-dismiss' => 'modal', :type => 'button'} Cancel - %button.btn.btn-primary{:type => 'submit'} Save \ No newline at end of file + %button.btn.btn-primary{:type => 'submit'} Save diff --git a/config/application.rb b/config/application.rb index 4d8f5c85..14391b1f 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,5 +1,6 @@ require File.expand_path('../boot', __FILE__) +require 'csv' require 'rails' # Pick the frameworks you want: require 'active_model/railtie' diff --git a/config/routes.rb b/config/routes.rb index 1200f208..9e89771b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -60,7 +60,11 @@ get :mark_as_close end - resources :redeem_requests, only: [:index, :update, :destroy] + resources :redeem_requests, only: [:index, :update, :destroy] do + collection do + get :download + end + end resources :ignored_files, except: [:show] do get :search, on: :collection diff --git a/test/controllers/admin/redeem_requests_controller_test.rb b/test/controllers/admin/redeem_requests_controller_test.rb index fcb8cb1e..683a885f 100644 --- a/test/controllers/admin/redeem_requests_controller_test.rb +++ b/test/controllers/admin/redeem_requests_controller_test.rb @@ -80,7 +80,70 @@ class Admin::RedeemRequestsControllerTest < ActionController::TestCase assert_not_nil assigns(:status) assert_equal redeem_request.count, 3 end - + + test "should generate a csv file" do + seed_data + redeem_request = create(:redeem_request, :points => 100, user: @user) + get :download, params: { :status => "false" } + assert_response :success + assert_equal "text/csv; header = present;", response.content_type + get :download, params: { :status => "true" } + assert_response :success + assert_equal "text/csv; header = present;", response.content_type + end + + test "check header of csv file for status false" do + seed_data + redeem_request = create(:redeem_request, :points => 100, user: @user) + get :download, params: { :status => "false" } + assert_response :success + attr = "User,Gift Shop,Store,Points,Cost,Date,Coupon Code,Address,Status".split(',') + csv = CSV.parse response.body + assert_equal csv[0],attr + end + + test "check header of csv file for status true" do + seed_data + redeem_request = create(:redeem_request, :points => 100, user: @user) + get :download, params: { :status => "true" } + assert_response :success + attr = "User,Gift Shop,Store,Points,Cost,Date,Coupon Code,Address,Status".split(',') + csv = CSV.parse response.body + assert_equal csv[0],attr + end + + test "check fields of csv file for status true" do + seed_data + gift_shop = ["github","amazon","other"] + store = [nil,"amazon.com","amazon.in","amazon.uk"] + status = ["false","true"] + redeem_request = create(:redeem_request, :points => 100, user: @user) + get :download, params: { :status => "true" } + assert_response :success + csv = CSV.parse response.body + assert_includes gift_shop, csv[1][1] + assert_includes store, csv[1][2] + assert_kind_of Fixnum, csv[1][3].to_i + assert_not_equal csv[1][3].to_i, csv[1][4].to_i + assert_includes status, csv[1][8] + end + + test "check fields of csv file for status false" do + seed_data + gift_shop = ["github","amazon","other"] + store = [nil,"amazon.com","amazon.in","amazon.uk"] + status = ["false","true"] + redeem_request = create(:redeem_request, :points => 100, user: @user) + get :download, params: {:status => "false"} + assert_response :success + csv = CSV.parse response.body + assert_includes gift_shop, csv[1][1] + assert_includes store, csv[1][2] + assert_kind_of Fixnum, csv[1][3].to_i + assert_not_equal csv[1][3].to_i, csv[1][4].to_i + assert_includes status, csv[1][8] + end + def seed_data round = create(:round, :status => 'open') role = create(:role, :name => 'Admin') diff --git a/test/helpers/admin/redeem_requests_helper_test.rb b/test/helpers/admin/redeem_requests_helper_test.rb index 13db0bcb..a4b2894f 100644 --- a/test/helpers/admin/redeem_requests_helper_test.rb +++ b/test/helpers/admin/redeem_requests_helper_test.rb @@ -1,4 +1,20 @@ require "test_helper" class Admin::RedeemRequestsHelperTest < ActionView::TestCase + + test "check worth of points" do + seed_data + redeem_request = create(:redeem_request,:points => 100, user: @user) + total_capital = redeem_request.points/10 + assert_equal total_capital, total_capital_of_points + end + + def seed_data + round = create(:round, :status => 'open') + role = create(:role, :name => 'Admin') + @user = create(:user, goal: create(:goal)) + @user.roles << role + transaction = create(:transaction, :type => 'credit', :points => 120, user: @user) + end + end