Skip to content

Commit

Permalink
Import new way
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby232 committed Dec 27, 2020
1 parent 96c3661 commit eda70db
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 132 deletions.
56 changes: 55 additions & 1 deletion app/controllers/importers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,61 @@
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------

require 'json'

class ImportersController < ApplicationController
# get /importers/new AJAX
#----------------------------------------------------------------------------
def new
@importer = Importer.new
@importer.entity_type = params[:entity_type]
if params[:entity_id]
@importer.entity_id = params[:entity_id]
end
respond_with(@importer)
end

# post /importers/create
#----------------------------------------------------------------------------
def create
error = 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
end
end
# Todo
# respond_to do |format|
# format.html { render "create", :locals => {error: error, columns: columns} }
# end
end

# get /importers/:id/map
#----------------------------------------------------------------------------
def form_map_columns
@importer = Importer.find(params[:id])
columns = FatFreeCRM::ImportHandle.get_columns(@importer.attachment.path)

respond_to do |format|
format.html { render "form_map_columns", :locals => {columns: columns} }
end
end

# post /importers/:id/map
#----------------------------------------------------------------------------
def map_columns
@importer = Importer.find(params[:id])
@importer.status = :map
map = params[:map]
@importer.map = map.to_json
@importer.save
result = FatFreeCRM::ImportHandle.process(@importer)

end

=begin
# get /campaigns/import AJAX
Expand Down Expand Up @@ -103,6 +157,6 @@ def create
private

def importer_params
params.require(:importer).permit(:attachment,:entity_type,:entity_id)
params.require(:importer).permit(:attachment, :entity_type, :entity_id)
end
end
13 changes: 10 additions & 3 deletions app/models/polymorphic/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
# Table name: importers
#
# id :integer not null, primary key
# entity_type :string
# entity_type :string
# entity_id :integer
# attachment_file_size :integer
# attachment_file_name :string(255)
# attachment_content_type :string(255)
# status :string(255)
# created_at :datetime
# updated_at :datetime
#

class Importer < ActiveRecord::Base
attr_accessor :status, :entity_type
attribute :entity_attrs

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

Expand All @@ -25,6 +25,13 @@ class Importer < ActiveRecord::Base
:message => 'Only EXCEL files are allowed.'
validates_attachment_file_name :attachment, matches: [/\.xls/, /\.xlsx?$/]

def entity_attrs
attrs = []
if self.entity_type == '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)
end
attrs
end

ActiveSupport.run_load_hooks(:fat_free_crm_importer, self)
end
2 changes: 1 addition & 1 deletion app/views/campaigns/_list_title_bar.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
.create_asset
= link_to_inline("create_#{model_name}".to_sym, send("new_#{model_name}_path"), text: t("create_#{model_name}".to_sym))
.import_asset
= link_to_inline("import_#{model_name}".to_sym, send("import_#{model_name}_path"), text: t("import_#{model_name}".to_sym))
= link_to_inline(:new_importer, new_importer_path(:campaign), text: t(:import_campaigns))

.title
%span{id: "create_#{model_name}_title"} #{t controller_name.to_sym}
Expand Down
4 changes: 1 addition & 3 deletions app/views/campaigns/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
= render 'list_title_bar'

.remote#create_campaign{ hidden }
.remote#campaign_import{ hidden }

%iframe#uploading{ name: "uploading", style: "width:100px; height:10px; border:5px" }
.remote#new_importer{ hidden }

= render 'search'

Expand Down
3 changes: 1 addition & 2 deletions app/views/campaigns/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
= render 'campaigns/title_bar', campaign: @campaign

= render "comments/new", commentable: @campaign
.remote#campaign_import_leads{ hidden }
%iframe#uploading{ name: "uploading", style: "width:100px; height:10px; border:5px" }
.remote#create_importer{ hidden }

= render partial: "shared/timeline", collection: @timeline

Expand Down
38 changes: 0 additions & 38 deletions app/views/importers/_import.html.haml

This file was deleted.

17 changes: 0 additions & 17 deletions app/views/importers/_import_leads.html.haml

This file was deleted.

1 change: 0 additions & 1 deletion app/views/importers/_import_leads_maps.html.haml

This file was deleted.

13 changes: 13 additions & 0 deletions app/views/importers/_new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
= form_for(@importer, url: create_importer_path, html:{ multipart: true}) do |f|
= f.hidden_field :entity_type
- if @importer.entity_id
= f.hidden_field :entity_id
%p
%small #{t :importer_description}

%div #{t :xls_file}
= fields_for(Importer) do |a|
%div= a.file_field :attachment

.buttonbar
= f.submit t(:upload_file)
4 changes: 4 additions & 0 deletions app/views/importers/create.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- if error
.flash_error = error
- else
#{ render(partial: "map_columns" ) }
16 changes: 16 additions & 0 deletions app/views/importers/form_map_columns.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
= form_for(@importer, url: map_columns_importer_path(@importer), html: { method: :post }) do |f|
= f.hidden_field :id

%p #{t :map_columns_description}

%div
%table
- @importer.entity_attrs.each do |attr|
%tr
%td
.label #{ attr }
%td
= select_tag "map[#{ attr }]", options_for_select(columns), class: 'select2', :include_blank => true, id: "map_#{ attr }"

.buttonbar
= f.submit t(:save)
6 changes: 0 additions & 6 deletions app/views/importers/import.js.haml

This file was deleted.

6 changes: 0 additions & 6 deletions app/views/importers/import_leads.js.haml

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/importers/import_upload.js.haml

This file was deleted.

6 changes: 6 additions & 0 deletions app/views/importers/new.js.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- _id = "new_importer"

crm.flick('empty', 'toggle');
crm.flip_form('#{_id}');
$('##{_id}').html('#{ j render(partial: "new" ) }');
crm.set_title('#{_id}', '#{ j t(_id) }');
11 changes: 8 additions & 3 deletions config/locales/fat_free_crm.en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,11 @@ en-US:

# Import from excel
#----------------------------------------------------------------------------
import_leads: 'Import Leads'
import_leads_help: 'Todo help for import leads from excel'
xls_file: 'EXCEL File'
import_campaigns: Import Campaigns
import_leads: Import Leads
import_leads_help: Todo help for import leads from excel
xls_file: EXCEL File
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.
11 changes: 8 additions & 3 deletions config/locales/fat_free_crm.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,11 @@ es:

# Import from excel
#----------------------------------------------------------------------------
import_leads: 'Importar clientes potenciales'
import_leads_help: 'Todo help for import leads from excel'
xls_file: 'Fichero EXCEL'
import_campaigns: Importar Campañas
import_leads: Importar clientes potenciales
import_leads_help: Todo help for import leads from excel
xls_file: Fichero EXCEL
upload_file: Subir fichero
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.
8 changes: 5 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,15 @@
end
end

resources :importers do
end

resources :fields, as: :custom_fields
resources :fields, as: :core_fields

resources :settings, only: :index
resources :plugins, only: :index
end

get 'importers/new/:entity_type(/:entity_id)' => 'importers#new', as: :new_importer
post 'importers' => 'importers#create', as: :create_importer
get 'importers/:id/map' => 'importers#form_map_columns', as: :form_map_columns_importer
post 'importers/:id/map' => 'importers#map_columns', as: :map_columns_importer
end
13 changes: 8 additions & 5 deletions db/migrate/20201217030615_create_importers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
class CreateImporters < ActiveRecord::Migration[4.2]
def self.up
create_table :importers do |t|
t.integer :attachment_file_size # Uploaded file size
t.string :attachment_file_name # Uploaded full file name
t.string :attachment_content_type # MIME content type
t.string :entity_type # led, campaign
t.string :status # new, success, error
t.integer :attachment_file_size # Uploaded file size
t.string :attachment_file_name, null: false # Uploaded full file name
t.string :attachment_content_type # MIME content type
t.string :entity_type, null: false # led, campaign
t.string :entity_id # led, campaign
t.string :status, null: false, default: :new # new, map , imported , error
t.text :map
t.text :messages
t.timestamps
end
end
Expand Down
13 changes: 10 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_12_17_030615) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "account_contacts", id: :serial, force: :cascade do |t|
t.integer "account_id"
t.integer "contact_id"
Expand Down Expand Up @@ -249,10 +253,13 @@

create_table "importers", id: :serial, force: :cascade do |t|
t.integer "attachment_file_size"
t.string "attachment_file_name"
t.string "attachment_file_name", null: false
t.string "attachment_content_type"
t.string "entity_type"
t.string "status"
t.string "entity_type", null: false
t.string "entity_id"
t.string "status", default: "new", null: false
t.text "map"
t.text "messages"
t.datetime "created_at"
t.datetime "updated_at"
end
Expand Down
Loading

0 comments on commit eda70db

Please sign in to comment.