-
Notifications
You must be signed in to change notification settings - Fork 63
/
upload_controller.rb
61 lines (48 loc) · 1.96 KB
/
upload_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# encoding: utf-8
require 'csv'
module Spotlight::Resources
class UploadController < ApplicationController
helper :all
before_filter :authenticate_user!
load_and_authorize_resource :exhibit, class: Spotlight::Exhibit
before_filter :build_resource, only: [:new, :create, :template]
load_and_authorize_resource class: 'Spotlight::Resources::Upload', through_association: "exhibit.resources", instance_name: 'resource'
def create
@resource.attributes = resource_params
if @resource.save_and_commit
flash[:notice] = t('spotlight.resources.upload.success')
if params["add-and-continue"]
redirect_to new_exhibit_resources_upload_path(@resource.exhibit)
else
redirect_to admin_exhibit_catalog_index_path(@resource.exhibit, sort: :timestamp)
end
else
flash[:error] = t('spotlight.resources.upload.error')
redirect_to admin_exhibit_catalog_index_path(@resource.exhibit, sort: :timestamp)
end
end
def csv_upload
file = csv_params[:url]
csv = CSV.parse(file.read, {headers:true, return_headers: false, encoding: 'utf-8'}).map(&:to_hash)
Spotlight::AddUploadsFromCSV.perform_later(csv, current_exhibit, current_user)
flash[:notice] = t('spotlight.resources.upload.csv.success', file_name: file.original_filename)
redirect_to :back
end
def template
render text: CSV.generate { |csv| csv << data_param_keys.unshift(:url) }, content_type: 'text/csv'
end
private
def build_resource
@resource ||= Spotlight::Resources::Upload.new exhibit: current_exhibit
end
def csv_params
params.require(:resources_csv_upload).permit(:url)
end
def resource_params
params.require(:resources_upload).permit(:url, data: data_param_keys)
end
def data_param_keys
Spotlight::Resources::Upload.fields(current_exhibit).collect(&:field_name) + current_exhibit.custom_fields.collect(&:field)
end
end
end