Class: Blazer::UploadsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/blazer/uploads_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/blazer/uploads_controller.rb', line 14

def create
  @upload = Blazer::Upload.new(upload_params)
  # use creator_id instead of creator
  # since we setup association without checking if column exists
  @upload.creator = blazer_user if @upload.respond_to?(:creator_id=) && blazer_user

  success = params.require(:upload).key?(:file)
  if success
    Blazer::Upload.transaction do
      success = @upload.save
      if success
        begin
          update_file(@upload)
        rescue CSV::MalformedCSVError, Blazer::UploadError => e
          @upload.errors.add(:base, e.message)
          success = false
          raise ActiveRecord::Rollback
        end
      end
    end
  else
    @upload.errors.add(:base, "File can't be blank")
  end

  if success
    redirect_to upload_path(@upload)
  else
    render_errors @upload
  end
end

#destroyObject



81
82
83
84
85
# File 'app/controllers/blazer/uploads_controller.rb', line 81

def destroy
  Blazer.uploads_connection.execute("DROP TABLE IF EXISTS #{@upload.table_name}")
  @upload.destroy
  redirect_to uploads_path
end

#editObject



49
50
# File 'app/controllers/blazer/uploads_controller.rb', line 49

def edit
end

#indexObject



6
7
8
# File 'app/controllers/blazer/uploads_controller.rb', line 6

def index
  @uploads = Blazer::Upload.order(:table)
end

#newObject



10
11
12
# File 'app/controllers/blazer/uploads_controller.rb', line 10

def new
  @upload = Blazer::Upload.new
end

#showObject



45
46
47
# File 'app/controllers/blazer/uploads_controller.rb', line 45

def show
  redirect_to new_query_path(upload_id: @upload.id)
end

#updateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/blazer/uploads_controller.rb', line 52

def update
  original_table = @upload.table
  @upload.assign_attributes(upload_params)

  success = nil
  Blazer::Upload.transaction do
    success = @upload.save
    if success
      if params.require(:upload).key?(:file)
        begin
          update_file(@upload, drop: original_table)
        rescue CSV::MalformedCSVError, Blazer::UploadError => e
          @upload.errors.add(:base, e.message)
          success = false
          raise ActiveRecord::Rollback
        end
      elsif @upload.table != original_table
        Blazer.uploads_connection.execute("ALTER TABLE #{Blazer.uploads_table_name(original_table)} RENAME TO #{Blazer.uploads_connection.quote_table_name(@upload.table)}")
      end
    end
  end

  if success
    redirect_to upload_path(@upload)
  else
    render_errors @upload
  end
end