Module: CsvMapper::ControllerActions

Defined in:
lib/csv_mapper/controller_actions.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
# File 'lib/csv_mapper/controller_actions.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
  base.csv_mapper_config
end

Instance Method Details

#importObject



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
# File 'lib/csv_mapper/controller_actions.rb', line 23

def import
  resource_name = self.class.name.gsub(/Controller/, '').singularize
  resource_class = resource_name.constantize
  if request.post?
    # already mapped
    if params[:fields]
      create_resource_items_from_csv(resource_class)
      if @csv_import_errors.empty?
        flash[:notice] = 'Daten erfolgreich importiert!'
        redirect_to :action => :index
      else
        flash[:warning] = @csv_import_errors
        redirect_to :back
      end
    #no mapping yet
    else
      @mapper = CsvMapper::Importer.new(params, self.class.read_inheritable_attribute(:map_fields_options))
      @raw_data = @mapper.raw_data
      render 'controller_actions/mapper'
    end
  else
    render 'controller_actions/import'
  end
rescue CsvMapper::InconsistentStateError
  flash[:warning] = 'unbekannter Fehler.'
rescue CsvMapper::MissingFileContentsError
  flash[:warning] = 'Bitte eine CSV-Datei hochladen.'
  render 'controller_actions/import'
rescue FasterCSV::MalformedCSVError => e
  flash[:warning] = 'Fehlerhaft formatierte CSV-Datei: ' + e
  render 'controller_actions/import'
rescue Errno::ENOENT
  flash[:warning] = 'Datei nicht mehr auf dem Server. Bitte erneut hochladen!'
  render 'controller_actions/import'
end