Skip to content

Commit

Permalink
rapidftr#48 : Export selected records to csv
Browse files Browse the repository at this point in the history
  • Loading branch information
pawan authored and pawan committed Dec 1, 2010
1 parent 7036847 commit 7437338
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 15 deletions.
26 changes: 23 additions & 3 deletions app/controllers/children_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def show
format.html # show.html.erb
format.xml { render :xml => @child }
format.json { render :json => @child.to_json }
format.csv do
child_ids = [@child]
export_to_csv(child_ids, current_user_name+"_#{Time.now.strftime("%Y%m%d-%H%M")}.csv")
end
format.pdf do
pdf_data = PdfGenerator.new.child_photo(@child)
send_pdf( pdf_data, "#{@child.unique_identifier}-#{Clock.now.strftime('%Y%m%d-%H%M')}.pdf" )
Expand Down Expand Up @@ -146,14 +150,27 @@ def advanced_search
end
end

def photo_pdf
def export_data
child_ids = params.map{ |k, v| 'selected' == v ? k : nil }.compact
if child_ids.empty?
raise ErrorResponse.bad_request('You must select at least one record to be exported')
end
children = child_ids.map{ |child_id| Child.get(child_id) }
if params[:commit] == "Export to PDF"
export_photo_to_pdf(children, "#{current_user_name}-#{Clock.now.strftime('%Y%m%d-%H%M')}.pdf" )
end
if params[:commit] == "Export to CSV"
export_to_csv(children, current_user_name+"_#{Time.now.strftime("%Y%m%d-%H%M")}.csv")
end
end

def export_photo_to_pdf children, filename
pdf_data = PdfGenerator.new.child_photos(children)
send_pdf( pdf_data, "#{current_user_name}-#{Clock.now.strftime('%Y%m%d-%H%M')}.pdf" )
send_pdf( pdf_data, filename)
end

def export_to_csv children, filename
render_as_csv(children, filename)
end

private
Expand All @@ -180,9 +197,12 @@ def render_as_csv results_temp, filename
field_names = FormSection.all_child_field_names
field_names.unshift "unique_identifier"
csv = FasterCSV.generate do |rows|
if results_temp.nil?
results_temp = @children
end
rows << field_names
results_temp.each do |child|
rows << field_names.map { |field_name| child[field_name] }
rows << field_names.map { |field_name| child[field_name] }
end
end

Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
</p>
<p>
<%= link_to 'Export All Child Records to CSV', url_for(:controller=>"children", :action=>"index", :escape => false, :format=>"csv") %>
</p>
<p>
<%= link_to 'Export Some Records to CSV', :advanced_search_children %>
</p>
3 changes: 2 additions & 1 deletion app/views/children/_search_results.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% has_results = (@results && @results.size > 0) %>
<%- form_tag photo_pdf_children_path, :method => :post do %>
<%- form_tag export_data_children_path, :method => :post do %>

<br />
<div class="profiles-list">
Expand All @@ -8,6 +8,7 @@
</div>

<%= submit_tag "Export to PDF" if has_results %>
<%= submit_tag "Export to CSV" if has_results %>
<%-end%>
<%= link_to( "Export to CSV", current_url_with_format_of('csv') ) if has_results %>
1 change: 1 addition & 0 deletions app/views/children/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<li class="back"><%= link_to 'Back', :back %></li>
<li class="view-log"><%= link_to 'View the change log', child_history_path(@child) %></li>
<li><%= link_to "Export to PDF", child_path(@child, :format => 'pdf') %></li>
<li><%= link_to "Export to CSV", child_path(@child, :format => 'csv') %></li>
</ul>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ActionController::Routing::Routes.draw do |map|

map.resources :children, :collection => { :search => :get, :photo_pdf => :post, :advanced_search => :get } do |child|
map.resources :children, :collection => { :search => :get, :export_photo_to_pdf => :post, :advanced_search => :get, :export_csv => :post, :export_data => :post} do |child|
child.resource :history, :only => :show
child.resources :attachments, :only => :show
end
Expand Down
28 changes: 26 additions & 2 deletions features/csv_export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature:

Scenario: A csv file with the correct number of lines is produced
Given I am logged in
When I search using a name of "D"
When I search using a name of "D"
And I follow "Export to CSV"
Then I should receive a CSV file with 3 lines
And the CSV data should be:
Expand All @@ -37,4 +37,28 @@ Feature:
| Dan | London | dan_123 |
| Dave | Venice | dave_456 |
| Mike | Paris | mike_789 |
And the CSV filename should be "all_records_20101023.csv"
And the CSV filename should be "all_records_20101023.csv"

Scenario: A csv file with selected records is produced
Given I am logged in as an admin
When I go to the admin page
And I follow "Export Some Records to CSV"
And I search using a name of "D"
And I select search result #1
And I press "Export to CSV"
Then I should receive a CSV file with 2 lines
And the CSV data should be:
| name | last_known_location |unique_id|
| Dan | London | dan_123 |

Scenario: A csv file with a single record is created in case on one record found
Given I am logged in as an admin
When I go to the admin page
And I follow "Export Some Records to CSV"
And I search using a name of "Dan"
And I follow "Export to CSV"
Then I should receive a CSV file with 2 lines
And the CSV data should be:
| name | last_known_location |unique_id|
| Dan | London | dan_123 |

4 changes: 2 additions & 2 deletions features/pdf_export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ Feature: So that hard copy printout of missing child photos are available
| name | unique_id | photo_path |
| Billy No Photo | will_uid | |
And I am on the saved record page for child with name "Billy No Photo"
And I follow "Export to PDF"
Then I should receive a PDF file
And I follow "Export to PDF"
Then I should receive a PDF file
16 changes: 11 additions & 5 deletions spec/controllers/children_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ def csv_response
get( :search, :format => 'csv', :query => 'blah' )
response.body
end


def csv_export_data_response
get( :export, :format => 'csv', :query => 'blah' )
response.body
end

it 'should contain the correct column headers based on the defined fields' do
inject_results([])
first_line = csv_response.split("\n").first
Expand Down Expand Up @@ -282,7 +287,7 @@ def stub_out_child_get(mock_child = mock(Child))
stub_out_pdf_generator
Child.should_receive(:get).with('a_child_id')
post(
:photo_pdf,
:export_data,
{ 'a_child_id' => 'selected', 'some_other_post_param' => 'blah' }
)
end
Expand All @@ -294,7 +299,7 @@ def stub_out_child_get(mock_child = mock(Child))
Child.should_receive(:get).with('child_three')

post(
:photo_pdf,
:export_data,
{
'child_one' => 'selected',
'child_two' => 'selected',
Expand All @@ -317,10 +322,11 @@ def stub_out_child_get(mock_child = mock(Child))
and_return('')

post(
:photo_pdf,
:export_data,
{
'child_1' => 'selected',
'child_2' => 'selected',
:commit => "Export to PDF"
}
)
end
Expand All @@ -336,7 +342,7 @@ def stub_out_child_get(mock_child = mock(Child))
should_receive(:send_data).
with( :fake_pdf_data, :filename => "foo-user-20000101-2015.pdf", :type => "application/pdf" )

post( :photo_pdf, 'ignored' => 'selected' )
post( :export_data, 'ignored' => 'selected', :commit => "Export to PDF" )
end
end
end
6 changes: 5 additions & 1 deletion spec/routing/children_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
end

it 'handles a multi-child photo pdf request' do
{ :post => 'children/photo_pdf' }.should route_to( :controller => 'children', :action => 'photo_pdf' )
{ :post => 'children/export_photo_to_pdf' }.should route_to( :controller => 'children', :action => 'export_photo_to_pdf' )
end

it 'handles a multi-child export request' do
{ :post => 'children/export_data' }.should route_to( :controller => 'children', :action => 'export_data' )
end
end
end

0 comments on commit 7437338

Please sign in to comment.