From 49fc73d73ef6815bc02ab44ef6717da8bf574b06 Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Mon, 11 Nov 2013 11:18:42 -0300 Subject: [PATCH] Adds named_scope for easy retrieval of tasks requested by profile --- plugins/custom_forms/lib/custom_forms_plugin/membership_survey.rb | 2 ++ plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/plugins/custom_forms/lib/custom_forms_plugin/membership_survey.rb b/plugins/custom_forms/lib/custom_forms_plugin/membership_survey.rb index a57cb50..76fae3b 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/membership_survey.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/membership_survey.rb @@ -5,6 +5,8 @@ class CustomFormsPlugin::MembershipSurvey < Task include CustomFormsPlugin::Helper + named_scope :from, lambda {|profile| {:conditions => {:requestor_id => profile.id}}} + def perform form = CustomFormsPlugin::Form.find(form_id) answers = build_answers(submission, form) diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb index a47a264..e693204 100644 --- a/plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb +++ b/plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb @@ -23,9 +23,22 @@ class CustomFormsPlugin::MembershipSurveyTest < ActiveSupport::TestCase end submission = CustomFormsPlugin::Submission.last - assert_equal submission.answers.count, 1 + assert_equal submission.answers.count, 1 answer = submission.answers.first assert_equal answer.value, 'Jack' end + + should 'have a named_scope that retrieves all tasks requested by profile' do + profile = fast_create(Profile) + person = fast_create(Person) + form = CustomFormsPlugin::Form.create!(:name => 'Simple Form', :profile => profile) + task1 = CustomFormsPlugin::MembershipSurvey.create!(:form_id => form.id, :target => person, :requestor => profile) + task2 = CustomFormsPlugin::MembershipSurvey.create!(:form_id => form.id, :target => person, :requestor => fast_create(Profile)) + scope = CustomFormsPlugin::MembershipSurvey.from(profile) + + assert_equal ActiveRecord::NamedScope::Scope, scope.class + assert_includes scope, task1 + assert_not_includes scope, task2 + end end -- libgit2 0.21.2