Class: ImportsController

Inherits:
ArtfullyOseController show all
Defined in:
app/controllers/imports_controller.rb

Instance Method Summary collapse

Instance Method Details

#approveObject



11
12
13
14
15
16
17
# File 'app/controllers/imports_controller.rb', line 11

def approve
  @import = organization.imports.find(params[:id])
  @import.approve!

  flash[:notice] = "Your file has been entered in the import queue. This process may take some time.  Reload this page to see progress or check back in a few minutes."
  redirect_to imports_path
end

#createObject



50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/imports_controller.rb', line 50

def create
  @import = Import.build(@type)
  @import.user = current_user
  @import.organization = organization

  if @import.save
    redirect_to import_path(@import)
  else
    render :new
  end
end

#destroyObject



62
63
64
65
66
# File 'app/controllers/imports_controller.rb', line 62

def destroy
  @import = organization.imports.find(params[:id])
  @import.destroy
  redirect_to imports_path
end

#indexObject



6
7
8
9
# File 'app/controllers/imports_controller.rb', line 6

def index
  @imports = organization.imports.includes(:user, :organization).order('created_at desc').all
  @sales_csv_download_link, @donations_csv_download_link = download_links
end

#newObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/imports_controller.rb', line 34

def new
  if params[:bucket].present? && params[:key].present?      
    @import = Import.build(@type)
    @import.organization  = organization
    @import.s3_bucket     = params[:bucket]
    @import.s3_key        = params[:key]
    @import.s3_etag       = params[:etag]
    @import.status        = "caching"
    @import.user_id       = current_user.id
    @import.caching!
    redirect_to import_path(@import)
  else
    @import = Import.build(@type)
  end
end

#recallObject



85
86
87
88
89
90
91
92
# File 'app/controllers/imports_controller.rb', line 85

def recall
  @import = organization.imports.find(params[:id])
  raise "Can't recall imports other than people imports." unless @import.is_a?(PeopleImport)

  Delayed::Job.enqueue RecallImportJob.new(@import.id)
  flash[:notice] = "The import is currently being recalled. Reload this page to see progress or check back in a few minutes."
  redirect_to imports_path
end

#showObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/imports_controller.rb', line 19

def show
  @import = organization.imports.find(params[:id])

  #
  # Building an import preview was just murdering us. The problem is way down in Array.index in parsed_row.load_value.
  # Temporarily shutting it off for > 1000
  #
  if @import.status == "pending"
    @imported_rows    = @import.import_rows.paginate(:page => params[:page], :per_page => 50)
  end

  @people   = Person.where(:import_id => @import.id).paginate(:page => params[:page], :per_page => 50) unless @import.id.nil?
  @messages = ImportMessage.where(:import_id => @import.id).paginate(:page => params[:messages_page], :per_page => 10) unless @import.id.nil?
end

#templateObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/imports_controller.rb', line 68

def template   
  case @type
  when "events"
    fields = ParsedRow::EVENT_FIELDS.merge(ParsedRow::PEOPLE_FIELDS).merge(ParsedRow::ADDRESS_FIELDS)
  when "people"
    fields = ParsedRow::PEOPLE_FIELDS.merge(ParsedRow::ADDRESS_FIELDS)
  when "donations"
    fields = ParsedRow::DONATION_FIELDS.merge(ParsedRow::PEOPLE_FIELDS).merge(ParsedRow::ADDRESS_FIELDS)
  else
    raise "Unknown import type."
  end
  
  columns = fields.map { |field, names| names.first }
  csv_string = CSV.generate { |csv| csv << columns }
  send_data csv_string, :filename => "Artfully-Import-Template.csv", :type => "text/csv", :disposition => "attachment"
end