Class: DealRedemptions::Admin::ImportController

Inherits:
DealRedemptions::ApplicationController show all
Defined in:
app/controllers/deal_redemptions/admin/import_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/deal_redemptions/admin/import_controller.rb', line 14

def create
  # Check if file is CSV type
  if params[:file].content_type == 'text/csv'
    @importer = DealRedemptions::Admin::Importer.new(params[:company], params[:product])
    if @importer.import_codes_csv(params[:file])
      redirect_to admin_import_path, notice: 'Redemption codes were successfully uploaded.'
    else
      redirect_to admin_import_path, alert: 'Make sure you\'re not uploading existing codes. Please reference your file again.'
    end
  else
    redirect_to admin_import_path, alert: 'Please only upload CSV formatted files'
  end
end

#export_redemptionsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/deal_redemptions/admin/import_controller.rb', line 28

def export_redemptions
  @codes = DealRedemptions::Redemption.where(created_at: Date.parse(params[:exportStartDate]).strftime("%Y-%m-%d")..Date.parse(params[:exportEndDate]).strftime("%Y-%m-%d")).includes(:redeem_code)

  respond_to do |format|
    format.csv do
      if @codes.count > 0
        import = DealRedemptions::Admin::Importer.new
        @redemptions = import.export_redemptions_csv(@codes)

        response.headers['Content-Type'] = 'text/csv'
        response.headers['Content-Disposition'] = 'attachment; filename=redemptions.csv'
        render csv: @redemptions
      else
        redirect_to admin_import_path, alert: 'No redemptions found to export.'
      end
    end
  end
end

#newObject



9
10
11
12
# File 'app/controllers/deal_redemptions/admin/import_controller.rb', line 9

def new
  @companies = DealRedemptions::Company.find_active
  @products = DealRedemptions::Product.all
end