Class: DealRedemptions::Admin::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/deal_redemptions/admin/importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(company = nil, product = nil) ⇒ Importer

Returns a new instance of Importer.



6
7
8
9
# File 'lib/deal_redemptions/admin/importer.rb', line 6

def initialize(company = nil, product = nil)
  @company = company
  @product = product
end

Instance Method Details

#export_redemptions_csv(redemptions) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/deal_redemptions/admin/importer.rb', line 34

def export_redemptions_csv(redemptions)
  @redeem_codes = []

  CSV.generate({ skip_blanks: true }) do |csv|
    csv << ["id", 'first_name', 'last_name', 'email', 'phone', 'address', 'address2', 'city', 'state', 'zip_code', 'country', 'redemption_codes', 'comments', 'created_at']
    redemptions.each do |item|
      # Loop redemption codes used
      item.redeem_code.each do |code|
        @redeem_codes.push code.code
      end

      csv << [
        item.id,
        item.first_name,
        item.last_name,
        item.email_address,
        item.phone,
        item.address1,
        item.address2,
        item.city,
        item.state,
        item.zip_code,
        item.country,
        @redeem_codes.join(', '),
        item.comments,
        item.created_at
      ]
    end
  end .html_safe
end

#import_codes_csv(file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/deal_redemptions/admin/importer.rb', line 11

def import_codes_csv(file)
  begin
    CSV.foreach file.path do |row|
      unless row[0].blank?
        code = DealRedemptions::RedeemCode.by_company(@company).find_by_code(row[0])

        unless code
          DealRedemptions::RedeemCode.create(
            company_id: @company,
            product_id: @product,
            code: row[0]
          )
        end
      end
    end

    # Return true after code import
    true
  rescue
    false
  end
end