Commit 3512ab2f08e0442f76f7e80db675d7286acc9abe

Authored by Daniela Feitosa
1 parent 94c1d5ec

Fixed the reorder of form submissions

- storing the author_name when logged users submit a form
- migration to insert =author_name= on existent submissions

(ActionItem3263)
plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
... ... @@ -65,10 +65,8 @@ class CustomFormsPluginMyprofileController < MyProfileController
65 65  
66 66 def submissions
67 67 @form = CustomFormsPlugin::Form.find(params[:id])
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'
  68 + @sort_by = params[:sort_by] == 'author_name' ? 'author_name' : 'created_at'
  69 + @submissions = @form.submissions.order(@sort_by)
72 70  
73 71 respond_to do |format|
74 72 format.html
... ...
plugins/custom_forms/db/migrate/20140807042217_fill_in_author_name_on_submission.rb 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +class FillInAuthorNameOnSubmission < ActiveRecord::Migration
  2 + def up
  3 + CustomFormsPlugin::Submission.find_each do |submission|
  4 + unless submission.profile.nil?
  5 + submission.author_name = submission.profile.name
  6 + submission.save
  7 + end
  8 + end
  9 + end
  10 +
  11 + def down
  12 + CustomFormsPlugin::Submission.find_each do |submission|
  13 + unless submission.profile.nil?
  14 + submission.author_name = nil
  15 + submission.save
  16 + end
  17 + end
  18 + end
  19 +end
... ...
plugins/custom_forms/lib/custom_forms_plugin/submission.rb
... ... @@ -21,6 +21,12 @@ class CustomFormsPlugin::Submission &lt; Noosfero::Plugin::ActiveRecord
21 21 end
22 22 end
23 23  
  24 + before_create do |submission|
  25 + if submission.profile
  26 + submission.author_name = profile.name
  27 + end
  28 + end
  29 +
24 30 def build_answers submission
25 31 self.form.fields.each do |field|
26 32 next unless value = submission[field.id.to_s]
... ... @@ -52,4 +58,3 @@ class CustomFormsPlugin::Submission &lt; Noosfero::Plugin::ActiveRecord
52 58 end
53 59  
54 60 end
55   -
... ...
plugins/custom_forms/public/order.js 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +jQuery("select.filter").change(function(){
  2 + var filter = jQuery(this).find("option:selected").val();
  3 + var attribute = jQuery(this).attr('name');
  4 + redirect_to('?' + attribute + '=' + filter);
  5 +});
... ...
plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
... ... @@ -220,7 +220,7 @@ class CustomFormsPluginMyprofileControllerTest &lt; ActionController::TestCase
220 220 assert_not_nil assigns(:sort_by)
221 221 assert_select 'table.action-table', /Author\W*Time\W*john[\W\dh]*bob[\W\dh]*/
222 222  
223   - get :submissions, :profile => profile.identifier, :id => form.id, :sort_by => 'author'
  223 + get :submissions, :profile => profile.identifier, :id => form.id, :sort_by => 'author_name'
224 224 assert_not_nil assigns(:sort_by)
225 225 assert_select 'table.action-table', /Author\W*Time\W*bob[\W\dh]*john[\W\dh]*/
226 226 end
... ...
plugins/custom_forms/test/unit/custom_forms_plugin/submission_test.rb
1 1 require File.dirname(__FILE__) + '/../../../../../test/test_helper'
2 2  
3 3 class CustomFormsPlugin::SubmissionTest < ActiveSupport::TestCase
  4 + def setup
  5 + @profile = fast_create(Profile)
  6 + end
  7 + attr_reader :profile
  8 +
4 9 should 'validates presence of form' do
5 10 submission = CustomFormsPlugin::Submission.new
6 11 submission.valid?
7 12 assert submission.errors.include?(:form)
8 13  
9   - form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile))
  14 + form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => profile)
10 15 submission.form = form
11 16 submission.valid?
12 17 assert !submission.errors.include?(:form)
13 18 end
14 19  
15 20 should 'belong to a profile' do
16   - profile = fast_create(Profile)
17 21 submission = CustomFormsPlugin::Submission.new
18 22 submission.profile = profile
19 23 assert_equal profile, submission.profile
... ... @@ -33,14 +37,21 @@ class CustomFormsPlugin::SubmissionTest &lt; ActiveSupport::TestCase
33 37 end
34 38  
35 39 should 'have answers' do
36   - form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile))
  40 + form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => profile)
37 41 field = CustomFormsPlugin::Field.create!(:name => 'License', :form => form)
38   - submission = CustomFormsPlugin::Submission.create!(:form => form, :profile => fast_create(Profile))
  42 + submission = CustomFormsPlugin::Submission.create!(:form => form, :profile => profile)
39 43 a1 = submission.answers.create!(:field => field, :submission => submission)
40 44 a2 = submission.answers.create!(:field => field, :submission => submission)
41 45  
42 46 assert_includes submission.answers, a1
43 47 assert_includes submission.answers, a2
44 48 end
  49 +
  50 + should 'store profile name as author' do
  51 + form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => profile)
  52 + submission = CustomFormsPlugin::Submission.create(:form => form, :profile => profile)
  53 +
  54 + assert_equal profile.name, submission.author_name
  55 + end
45 56 end
46 57  
... ...
plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb
... ... @@ -10,7 +10,7 @@
10 10 <%= link_to '[CSV]', :format => 'csv' %>
11 11 </p>
12 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') %>
  13 + <%= labelled_select(_('Sort by')+': ', :sort_by, :first, :last, @sort_by, [['created_at', _('Time')], ['author_name', _('Author')]], :class => 'filter') %>
14 14 </p>
15 15 <table class="action-table">
16 16 <tr>
... ... @@ -30,3 +30,5 @@
30 30 <% button_bar do %>
31 31 <%= button :back, _('Back to forms'), :action => 'index' %>
32 32 <% end %>
  33 +
  34 +<%= javascript_include_tag '../plugins/custom_forms/order' %>
... ...