Commit 3512ab2f08e0442f76f7e80db675d7286acc9abe
1 parent
94c1d5ec
Exists in
master
and in
27 other branches
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)
Showing
7 changed files
with
51 additions
and
11 deletions
Show diff stats
plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
@@ -65,10 +65,8 @@ class CustomFormsPluginMyprofileController < MyProfileController | @@ -65,10 +65,8 @@ class CustomFormsPluginMyprofileController < MyProfileController | ||
65 | 65 | ||
66 | def submissions | 66 | def submissions |
67 | @form = CustomFormsPlugin::Form.find(params[:id]) | 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 | respond_to do |format| | 71 | respond_to do |format| |
74 | format.html | 72 | format.html |
plugins/custom_forms/db/migrate/20140807042217_fill_in_author_name_on_submission.rb
0 → 100644
@@ -0,0 +1,19 @@ | @@ -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 < Noosfero::Plugin::ActiveRecord | @@ -21,6 +21,12 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord | ||
21 | end | 21 | end |
22 | end | 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 | def build_answers submission | 30 | def build_answers submission |
25 | self.form.fields.each do |field| | 31 | self.form.fields.each do |field| |
26 | next unless value = submission[field.id.to_s] | 32 | next unless value = submission[field.id.to_s] |
@@ -52,4 +58,3 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord | @@ -52,4 +58,3 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord | ||
52 | end | 58 | end |
53 | 59 | ||
54 | end | 60 | end |
55 | - |
plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
@@ -220,7 +220,7 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase | @@ -220,7 +220,7 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase | ||
220 | assert_not_nil assigns(:sort_by) | 220 | assert_not_nil assigns(:sort_by) |
221 | assert_select 'table.action-table', /Author\W*Time\W*john[\W\dh]*bob[\W\dh]*/ | 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 | assert_not_nil assigns(:sort_by) | 224 | assert_not_nil assigns(:sort_by) |
225 | assert_select 'table.action-table', /Author\W*Time\W*bob[\W\dh]*john[\W\dh]*/ | 225 | assert_select 'table.action-table', /Author\W*Time\W*bob[\W\dh]*john[\W\dh]*/ |
226 | end | 226 | end |
plugins/custom_forms/test/unit/custom_forms_plugin/submission_test.rb
1 | require File.dirname(__FILE__) + '/../../../../../test/test_helper' | 1 | require File.dirname(__FILE__) + '/../../../../../test/test_helper' |
2 | 2 | ||
3 | class CustomFormsPlugin::SubmissionTest < ActiveSupport::TestCase | 3 | class CustomFormsPlugin::SubmissionTest < ActiveSupport::TestCase |
4 | + def setup | ||
5 | + @profile = fast_create(Profile) | ||
6 | + end | ||
7 | + attr_reader :profile | ||
8 | + | ||
4 | should 'validates presence of form' do | 9 | should 'validates presence of form' do |
5 | submission = CustomFormsPlugin::Submission.new | 10 | submission = CustomFormsPlugin::Submission.new |
6 | submission.valid? | 11 | submission.valid? |
7 | assert submission.errors.include?(:form) | 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 | submission.form = form | 15 | submission.form = form |
11 | submission.valid? | 16 | submission.valid? |
12 | assert !submission.errors.include?(:form) | 17 | assert !submission.errors.include?(:form) |
13 | end | 18 | end |
14 | 19 | ||
15 | should 'belong to a profile' do | 20 | should 'belong to a profile' do |
16 | - profile = fast_create(Profile) | ||
17 | submission = CustomFormsPlugin::Submission.new | 21 | submission = CustomFormsPlugin::Submission.new |
18 | submission.profile = profile | 22 | submission.profile = profile |
19 | assert_equal profile, submission.profile | 23 | assert_equal profile, submission.profile |
@@ -33,14 +37,21 @@ class CustomFormsPlugin::SubmissionTest < ActiveSupport::TestCase | @@ -33,14 +37,21 @@ class CustomFormsPlugin::SubmissionTest < ActiveSupport::TestCase | ||
33 | end | 37 | end |
34 | 38 | ||
35 | should 'have answers' do | 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 | field = CustomFormsPlugin::Field.create!(:name => 'License', :form => form) | 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 | a1 = submission.answers.create!(:field => field, :submission => submission) | 43 | a1 = submission.answers.create!(:field => field, :submission => submission) |
40 | a2 = submission.answers.create!(:field => field, :submission => submission) | 44 | a2 = submission.answers.create!(:field => field, :submission => submission) |
41 | 45 | ||
42 | assert_includes submission.answers, a1 | 46 | assert_includes submission.answers, a1 |
43 | assert_includes submission.answers, a2 | 47 | assert_includes submission.answers, a2 |
44 | end | 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 | end | 56 | end |
46 | 57 |
plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb
@@ -10,7 +10,7 @@ | @@ -10,7 +10,7 @@ | ||
10 | <%= link_to '[CSV]', :format => 'csv' %> | 10 | <%= link_to '[CSV]', :format => 'csv' %> |
11 | </p> | 11 | </p> |
12 | <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') %> | 13 | + <%= labelled_select(_('Sort by')+': ', :sort_by, :first, :last, @sort_by, [['created_at', _('Time')], ['author_name', _('Author')]], :class => 'filter') %> |
14 | </p> | 14 | </p> |
15 | <table class="action-table"> | 15 | <table class="action-table"> |
16 | <tr> | 16 | <tr> |
@@ -30,3 +30,5 @@ | @@ -30,3 +30,5 @@ | ||
30 | <% button_bar do %> | 30 | <% button_bar do %> |
31 | <%= button :back, _('Back to forms'), :action => 'index' %> | 31 | <%= button :back, _('Back to forms'), :action => 'index' %> |
32 | <% end %> | 32 | <% end %> |
33 | + | ||
34 | +<%= javascript_include_tag '../plugins/custom_forms/order' %> |