Commit c57d22d8151b0b27f6448f0dc54a5f5abb2303dd

Authored by Larissa Reis
1 parent 328b19c4

User can order submissions in alphabetical order

plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
... ... @@ -66,6 +66,10 @@ class CustomFormsPluginMyprofileController < MyProfileController
66 66 def submissions
67 67 @form = CustomFormsPlugin::Form.find(params[:id])
68 68 @submissions = @form.submissions
  69 +
  70 + @sort_by = params[:sort_by]
  71 + @submissions = @submissions.sort_by { |s| s.profile.present? ? s.profile.name : s.author_name } if @sort_by == 'author'
  72 +
69 73 respond_to do |format|
70 74 format.html
71 75 format.csv do
... ...
plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
... ... @@ -179,5 +179,22 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase
179 179 assert_equal 'Cool form', form.description
180 180 assert_equal 'Source', field.name
181 181 end
  182 +
  183 + should 'order submissions by name or time' do
  184 + form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software')
  185 + field = CustomFormsPlugin::TextField.create!(:name => "Title")
  186 + form.fields << field
  187 + sub1 = CustomFormsPlugin::Submission.create!(:author_name => "john", :author_email => 'john@example.com', :form => form)
  188 + bob = create_user('bob').person
  189 + sub2 = CustomFormsPlugin::Submission.create!(:profile => bob, :form => form)
  190 +
  191 + get :submissions, :profile => profile.identifier, :id => form.id, :sort_by => 'time'
  192 + assert_not_nil assigns(:sort_by)
  193 + assert_select 'table.action-table', /Author\W*Time\W*john[\W\dh]*bob[\W\dh]*/
  194 +
  195 + get :submissions, :profile => profile.identifier, :id => form.id, :sort_by => 'author'
  196 + assert_not_nil assigns(:sort_by)
  197 + assert_select 'table.action-table', /Author\W*Time\W*bob[\W\dh]*john[\W\dh]*/
  198 + end
182 199 end
183 200  
... ...
plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb
... ... @@ -9,6 +9,9 @@
9 9 <%= _('Download all form responses as') %>:
10 10 <%= link_to '[CSV]', :format => 'csv' %>
11 11 </p>
  12 + <p>
  13 + <%= labelled_select(_('Sort by')+': ', :sort_by, :first, :last, @sort_by, [['time', _('Time')], ['author', _('Author')]], :onchange => 'document.location.href = "?sort_by="+this.value') %>
  14 + </p>
12 15 <table class="action-table">
13 16 <tr>
14 17 <th style='width: 50%'><%= _('Author') %></th>
... ...