From 3512ab2f08e0442f76f7e80db675d7286acc9abe Mon Sep 17 00:00:00 2001
From: Daniela Feitosa
Date: Thu, 7 Aug 2014 16:40:40 +0000
Subject: [PATCH] Fixed the reorder of form submissions
---
plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb | 6 ++----
plugins/custom_forms/db/migrate/20140807042217_fill_in_author_name_on_submission.rb | 19 +++++++++++++++++++
plugins/custom_forms/lib/custom_forms_plugin/submission.rb | 7 ++++++-
plugins/custom_forms/public/order.js | 5 +++++
plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb | 2 +-
plugins/custom_forms/test/unit/custom_forms_plugin/submission_test.rb | 19 +++++++++++++++----
plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb | 4 +++-
7 files changed, 51 insertions(+), 11 deletions(-)
create mode 100644 plugins/custom_forms/db/migrate/20140807042217_fill_in_author_name_on_submission.rb
create mode 100644 plugins/custom_forms/public/order.js
diff --git a/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
index caafcb6..eec5bc9 100644
--- a/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
+++ b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
@@ -65,10 +65,8 @@ class CustomFormsPluginMyprofileController < MyProfileController
def submissions
@form = CustomFormsPlugin::Form.find(params[:id])
- @submissions = @form.submissions
-
- @sort_by = params[:sort_by]
- @submissions = @submissions.sort_by { |s| s.profile.present? ? s.profile.name : s.author_name } if @sort_by == 'author'
+ @sort_by = params[:sort_by] == 'author_name' ? 'author_name' : 'created_at'
+ @submissions = @form.submissions.order(@sort_by)
respond_to do |format|
format.html
diff --git a/plugins/custom_forms/db/migrate/20140807042217_fill_in_author_name_on_submission.rb b/plugins/custom_forms/db/migrate/20140807042217_fill_in_author_name_on_submission.rb
new file mode 100644
index 0000000..5e72611
--- /dev/null
+++ b/plugins/custom_forms/db/migrate/20140807042217_fill_in_author_name_on_submission.rb
@@ -0,0 +1,19 @@
+class FillInAuthorNameOnSubmission < ActiveRecord::Migration
+ def up
+ CustomFormsPlugin::Submission.find_each do |submission|
+ unless submission.profile.nil?
+ submission.author_name = submission.profile.name
+ submission.save
+ end
+ end
+ end
+
+ def down
+ CustomFormsPlugin::Submission.find_each do |submission|
+ unless submission.profile.nil?
+ submission.author_name = nil
+ submission.save
+ end
+ end
+ end
+end
diff --git a/plugins/custom_forms/lib/custom_forms_plugin/submission.rb b/plugins/custom_forms/lib/custom_forms_plugin/submission.rb
index c859b76..154a736 100644
--- a/plugins/custom_forms/lib/custom_forms_plugin/submission.rb
+++ b/plugins/custom_forms/lib/custom_forms_plugin/submission.rb
@@ -21,6 +21,12 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord
end
end
+ before_create do |submission|
+ if submission.profile
+ submission.author_name = profile.name
+ end
+ end
+
def build_answers submission
self.form.fields.each do |field|
next unless value = submission[field.id.to_s]
@@ -52,4 +58,3 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord
end
end
-
diff --git a/plugins/custom_forms/public/order.js b/plugins/custom_forms/public/order.js
new file mode 100644
index 0000000..bfdf49e
--- /dev/null
+++ b/plugins/custom_forms/public/order.js
@@ -0,0 +1,5 @@
+jQuery("select.filter").change(function(){
+ var filter = jQuery(this).find("option:selected").val();
+ var attribute = jQuery(this).attr('name');
+ redirect_to('?' + attribute + '=' + filter);
+});
diff --git a/plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb b/plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
index 7a4fa45..c9ddecd 100644
--- a/plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
+++ b/plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
@@ -220,7 +220,7 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase
assert_not_nil assigns(:sort_by)
assert_select 'table.action-table', /Author\W*Time\W*john[\W\dh]*bob[\W\dh]*/
- get :submissions, :profile => profile.identifier, :id => form.id, :sort_by => 'author'
+ get :submissions, :profile => profile.identifier, :id => form.id, :sort_by => 'author_name'
assert_not_nil assigns(:sort_by)
assert_select 'table.action-table', /Author\W*Time\W*bob[\W\dh]*john[\W\dh]*/
end
diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/submission_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/submission_test.rb
index 7269857..3a9122d 100644
--- a/plugins/custom_forms/test/unit/custom_forms_plugin/submission_test.rb
+++ b/plugins/custom_forms/test/unit/custom_forms_plugin/submission_test.rb
@@ -1,19 +1,23 @@
require File.dirname(__FILE__) + '/../../../../../test/test_helper'
class CustomFormsPlugin::SubmissionTest < ActiveSupport::TestCase
+ def setup
+ @profile = fast_create(Profile)
+ end
+ attr_reader :profile
+
should 'validates presence of form' do
submission = CustomFormsPlugin::Submission.new
submission.valid?
assert submission.errors.include?(:form)
- form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile))
+ form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => profile)
submission.form = form
submission.valid?
assert !submission.errors.include?(:form)
end
should 'belong to a profile' do
- profile = fast_create(Profile)
submission = CustomFormsPlugin::Submission.new
submission.profile = profile
assert_equal profile, submission.profile
@@ -33,14 +37,21 @@ class CustomFormsPlugin::SubmissionTest < ActiveSupport::TestCase
end
should 'have answers' do
- form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile))
+ form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => profile)
field = CustomFormsPlugin::Field.create!(:name => 'License', :form => form)
- submission = CustomFormsPlugin::Submission.create!(:form => form, :profile => fast_create(Profile))
+ submission = CustomFormsPlugin::Submission.create!(:form => form, :profile => profile)
a1 = submission.answers.create!(:field => field, :submission => submission)
a2 = submission.answers.create!(:field => field, :submission => submission)
assert_includes submission.answers, a1
assert_includes submission.answers, a2
end
+
+ should 'store profile name as author' do
+ form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => profile)
+ submission = CustomFormsPlugin::Submission.create(:form => form, :profile => profile)
+
+ assert_equal profile.name, submission.author_name
+ end
end
diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb
index be17179..f34318d 100644
--- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb
+++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb
@@ -10,7 +10,7 @@
<%= link_to '[CSV]', :format => 'csv' %>
- <%= labelled_select(_('Sort by')+': ', :sort_by, :first, :last, @sort_by, [['time', _('Time')], ['author', _('Author')]], :onchange => 'document.location.href = "?sort_by="+this.value') %>
+ <%= labelled_select(_('Sort by')+': ', :sort_by, :first, :last, @sort_by, [['created_at', _('Time')], ['author_name', _('Author')]], :class => 'filter') %>
@@ -30,3 +30,5 @@
<% button_bar do %>
<%= button :back, _('Back to forms'), :action => 'index' %>
<% end %>
+
+<%= javascript_include_tag '../plugins/custom_forms/order' %>
--
libgit2 0.21.2