Skip to content

Commit

Permalink
Import 'campaign' OK
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby232 committed Dec 27, 2020
1 parent 1c2ef55 commit 02bcb62
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
23 changes: 15 additions & 8 deletions app/controllers/importers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ def new
# post /importers/create
#----------------------------------------------------------------------------
def create
error = false
errors = false
if params[:importer]
@importer = Importer.create(importer_params)
if @importer.valid?
@importer.save
redirect_to form_map_columns_importer_path(@importer)
else
error = @importer.errors.full_messages
errors = @importer.errors.full_messages
end
end
# Todo
# respond_to do |format|
# format.html { render "create", :locals => {error: error, columns: columns} }
# end

respond_to do |format|
if errors
format.html { render "create", :locals => {errors: errors} }
else
format.html { redirect_to form_map_columns_importer_path(@importer) }
end
end

end

# get /importers/:id/map
Expand All @@ -58,8 +62,11 @@ def map_columns
map = params[:map]
@importer.map = map.to_json
@importer.save
result = FatFreeCRM::ImportHandle.process(@importer)
@importer = FatFreeCRM::ImportHandle.process(@importer)

respond_to do |format|
format.html { render "map_columns" }
end
end

=begin
Expand Down
16 changes: 13 additions & 3 deletions app/models/polymorphic/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# created_at :datetime
# updated_at :datetime
#
require 'json'
class Importer < ActiveRecord::Base
attribute :entity_attrs

has_attached_file :attachment, :path => ":rails_root/public/importers/:id/:filename"

# validates_attachment :attachment, presence: true,
# content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }
validates_attachment :attachment, presence: true

validates_attachment_content_type :attachment,
:content_type => %w(text/xml application/xml application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/x-ole-storage),
Expand All @@ -27,11 +27,21 @@ class Importer < ActiveRecord::Base

def entity_attrs
attrs = []
if self.entity_type == 'campaign'
case self.entity_type
when 'campaign'
attrs = %w(user_id assigned_to name access status budget target_leads target_conversion target_revenue leads_count opportunities_count revenue starts_on ends_on objectives deleted_at created_at updated_at background_info)
when 'lead'
attrs = %w(user_id assigned_to first_name last_name access title company source status referred_by email alt_email phone mobile blog linkedin facebook twitter rating do_not_call deleted_at created_at updated_at background_info skype)
else
# Todo
puts "Error: entity_type not found"
end
attrs
end

def get_messages()
JSON.parse(messages)
end

ActiveSupport.run_load_hooks(:fat_free_crm_importer, self)
end
5 changes: 3 additions & 2 deletions app/views/importers/create.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- if error
.flash_error = error
- if errors
- errors.each do |error|
.flash_error #{ error }
- else
#{ render(partial: "map_columns" ) }
4 changes: 4 additions & 0 deletions app/views/importers/map_columns.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%div #{t :importer_status_label } #{ @importer.status }
- @importer.get_messages.each do |message|
%div #{message}

3 changes: 2 additions & 1 deletion config/locales/fat_free_crm.en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -948,4 +948,5 @@ en-US:
upload_file: Upload file
save: Save
importer_description: Upload excel file to import its content to the databases. Only valid excel files are allowed.
map_columns_description: Link the excel column that corresponds to each attribute or leave it blank.
map_columns_description: Link the excel column that corresponds to each attribute or leave it blank.
importer_status_label: Status
1 change: 1 addition & 0 deletions config/locales/fat_free_crm.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -927,3 +927,4 @@ es:
save: Guardar
importer_description: Subir fichero excel para importar su contenido a las base de datos. Solo se admiten fichero excel validos.
map_columns_description: Enlazar la columna del excel que le corresponde a cada atributo o dejar en blanco.
importer_status_label: Estado
5 changes: 1 addition & 4 deletions lib/fat_free_crm/import_handle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
require 'roo'
require 'json'

module FatFreeCRM
class ImportHandle
Expand All @@ -28,13 +29,11 @@ def process(importer)

xlsx.each_with_pagename do |name, sheet|
((sheet.first_row + 1)..sheet.last_row).each do |row|
#item = importer.entity_type.capitalize().constantize.new
values = {}
map.each do |att,i|
if not i.empty? and i.to_i >= 0
value = sheet.row(row)[i.to_i]
values[att] = value
#item.instance_variable_set(:@attributes, {att => value})
end
end
item = importer.entity_type.capitalize().constantize.create(values)
Expand All @@ -51,8 +50,6 @@ def process(importer)
else
importer.status = :error
importer.messages = errors.to_json
puts 'Errors'
puts errors.to_json
end
importer.save

Expand Down

0 comments on commit 02bcb62

Please sign in to comment.