Skip to content

Commit

Permalink
Merge pull request #305 from amitk/export_data_to_csv
Browse files Browse the repository at this point in the history
Export data to csv[#296]
  • Loading branch information
prasadsurase authored Jul 25, 2017
2 parents 534c4ff + 3500560 commit e0f96c2
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/admin/redeem_requests.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
24 changes: 20 additions & 4 deletions app/controllers/admin/redeem_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion app/helpers/admin/redeem_requests_helper.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/views/admin/redeem_requests/_redeem_request.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/redeem_requests/_tagtable.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions app/views/admin/redeem_requests/index.html.haml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
%button.btn.btn-primary{:type => 'submit'} Save
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require File.expand_path('../boot', __FILE__)

require 'csv'
require 'rails'
# Pick the frameworks you want:
require 'active_model/railtie'
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 64 additions & 1 deletion test/controllers/admin/redeem_requests_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
16 changes: 16 additions & 0 deletions test/helpers/admin/redeem_requests_helper_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e0f96c2

Please sign in to comment.