Skip to content

Commit

Permalink
Re-use an import mapping issue salesking#2
Browse files Browse the repository at this point in the history
  • Loading branch information
vijendra committed Feb 20, 2012
1 parent 390c81e commit c4aab22
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def create
def update
if @attachment.update_attributes(params[:attachment])
respond_to do |format|
format.html { redirect_to new_attachment_mapping_url(@attachment) }
format.html { redirect_to (@attachment.mapping.blank? ? new_attachment_mapping_url(@attachment) : new_attachment_import_url(@attachment))
}
format.js { render json: {rows: @attachment.rows(4)}, status: :ok }
end
else
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/mappings_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module MappingsHelper
def mapping_options
Mapping.by_company(session['company_id']).with_fields.map{|m| [m.title, m.id]}
end
end
2 changes: 1 addition & 1 deletion app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Attachment < ActiveRecord::Base
validates :filename, :disk_filename, presence: true
validates :col_sep, :quote_char, :encoding, presence: true

attr_accessible :col_sep, :quote_char, :uploaded_data, :encoding
attr_accessible :col_sep, :quote_char, :uploaded_data, :encoding, :mapping_id
attr_reader :error_rows

# Any upload file gets passed in as uploaded_data attribute
Expand Down
7 changes: 6 additions & 1 deletion app/models/mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ class Mapping < ActiveRecord::Base
default_scope order('mappings.id desc')

accepts_nested_attributes_for :mapping_elements

scope :by_company, lambda{|company_id| where(:company_id => company_id)}
scope :with_fields, joins(:mapping_elements).
select('mappings.id, count(mapping_elements.id) as element_count').
group('mappings.id').
having('element_count > 0')

def title
I18n.t('mappings.title', count: mapping_elements.count, fields: mapping_elements.collect(&:target).to_sentence)
end
Expand Down
8 changes: 7 additions & 1 deletion app/views/mappings/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
%h2
= t('link.step', count: 2, total: 3)
= t('mappings.new')

= form_for @attachment do |f|
%p= t('mappings.reuse')
= f.select 'mapping_id', options_for_select(mapping_options)
= f.submit t('mappings.proceed_to_import'), :class => "btn primary large"
%hr

= form_for [@attachment, @mapping] do |f|
.clearfix
Expand All @@ -22,4 +28,4 @@
.target{attrs.merge('data-target' => name)}= name
.actions
= f.submit t('mappings.proceed_to_import'), :class => "btn primary large"
= link_to t('link.back_to_list'), attachments_path, class: "btn secondary"
= link_to t('link.back_to_list'), attachments_path, class: "btn secondary"
4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ en:
new: Upload new CSV file
upload: Upload
proceed_to_mapping: Proceed to mapping
delete_confirm: "Please note that, this is irreversible process. This will delete associated imports. Are you sure?"
delete_confirm: "Please note that, this is irreversible process. This will delete associated imports. Are you sure?"
csv_import_error: "Try with different 'Column Separator' and/or 'Quote character'."
mappings:
title:
one: "%{count} field: %{fields}"
Expand All @@ -44,6 +45,7 @@ en:
proceed_to_import: Proceed to import
source_fields: Source fields
target_fields: Target fields
reuse: Select an existing mapping from the list to reuse that or create a new mapping in the below section.
dnd_info: "Please drag&drop fields above onto their targets below. Drop multiple fields on a target to join values (space-delimited) e.g for the tag list."
imports:
title_success:
Expand Down
9 changes: 7 additions & 2 deletions spec/controllers/attachments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,16 @@
assigns[:attachment].quote_char.should == '^'
end

it "redirects to new attachment mapping on html request" do
put :update, id: @authorized_attachment, attachment: {col_sep: ';'}
it "redirects to new attachment mapping on html request if mapping is not set" do
put :update, id: @authorized_attachment, attachment: {col_sep: ';', mapping_id: ''}
response.should redirect_to(new_attachment_mapping_url(@authorized_attachment))
end

it "redirects to new attachment import on html request if mapping is set" do
mapping = Factory(:mapping)
put :update, id: @authorized_attachment, attachment: {col_sep: ';', mapping_id: mapping.id}
response.should redirect_to(new_attachment_import_url(@authorized_attachment))
end
it "reneders successful json response on js request" do
put :update, id: @authorized_attachment, attachment: {col_sep: ';'}, format: 'js'
response.code.should == "200"
Expand Down
2 changes: 1 addition & 1 deletion spec/models/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
it { should validate_presence_of(attribute)}
end

[:col_sep, :quote_char, :uploaded_data, :encoding].each do |attribute|
[:col_sep, :quote_char, :uploaded_data, :encoding, :mapping_id].each do |attribute|
it { should allow_mass_assignment_of(attribute) }
end

Expand Down

0 comments on commit c4aab22

Please sign in to comment.