Skip to content

Commit

Permalink
Importa address
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby232 committed Dec 28, 2020
1 parent 974cb3a commit ba5edf6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 24 deletions.
16 changes: 15 additions & 1 deletion app/controllers/importers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def form_map_columns
columns = FatFreeCRM::ImportHandle.get_columns(@importer.attachment.path)

attributes = []
attributes_extra = []

object = @importer.entity_class
_attrs = object.attribute_names - ['id']
Expand All @@ -63,8 +64,21 @@ def form_map_columns
)
end

if @importer.entity_type == 'lead'
_attrs = Address.attribute_names - %w(id created_at updated_at deleted_at address_type addressable_type addressable_id)

_attrs.each do |attr|
attributes_extra.push(
{
name: attr,
required: Address.validators_on(attr).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
}
)
end
end

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

Expand Down
14 changes: 0 additions & 14 deletions app/models/polymorphic/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ class Importer < ActiveRecord::Base
:message => 'Only EXCEL files are allowed.'
validates_attachment_file_name :attachment, matches: [/\.xls/, /\.xlsx?$/]

def entity_attrs
attrs = []
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
Expand Down
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 @@ -6,7 +6,7 @@
= view_buttons
.create_asset
= link_to_inline("create_#{model_name}".to_sym, send("new_#{model_name}_path"), text: t("create_#{model_name}".to_sym))
.import_asset
.create_asset
= link_to_inline(:new_importer, new_importer_path(:campaign), text: t(:import_campaigns))

.title
Expand Down
20 changes: 20 additions & 0 deletions app/views/importers/form_map_columns.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,25 @@
required: attr[:required],
id: "map_#{ attr[:name] }"

- if attributes_extra.length
%div.subtitle
#{t :address}
%table
- attributes_extra.each do |attr|
%tr
%td
.label
#{ attr[:name] }
- if attr[:required]
%span.warn *
%td
= select_tag "map[business_address_attributes][#{ attr[:name] }]",
options_for_select(columns),
class: 'select2',
include_blank: true,
data: {placeholder: t(:select_blank)},
required: attr[:required],
id: "map_#{ attr[:name] }"

.buttonbar
= f.submit t(:save)
35 changes: 27 additions & 8 deletions lib/fat_free_crm/import_handle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,48 @@ def get_columns(path)
headers
end

def get_values(map, sheet, row)
values = {}
map.each do |att, i|
if i.is_a?(Hash)
values[att] = get_values(i, sheet, row)
elsif not i.empty? and i.to_i >= 0
value = sheet.row(row)[i.to_i]
values[att] = value
end
end

values
end

def process(importer)
errors = []
map = JSON.parse(importer.map)
xlsx = Roo::Spreadsheet.open(importer.attachment.path)

xlsx.each_with_pagename do |name, sheet|
((sheet.first_row + 1)..sheet.last_row).each do |row|
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
end
end
values = get_values(map, sheet, row)

# Todo Do this more geneic
# TODO Do this more geneic
business_address_attributes = {}
if importer.entity_type == 'lead'
values[:campaign_id] = importer.entity_id
if values.key?('business_address_attributes')
business_address_attributes = values.delete('business_address_attributes')
end
end

item = importer.entity_type.capitalize.constantize.create(values)
if item.valid?
item.save
if importer.entity_type == 'lead'
business_address_attributes["address_type"] = "Business"
business_address_attributes["addressable_type"] = "Lead"
business_address_attributes["addressable_id"] = item.id
address = Address.create(business_address_attributes)
address.save
end
else
errors << item.errors.full_messages
end
Expand Down

0 comments on commit ba5edf6

Please sign in to comment.