diff --git a/app/controllers/my_profile/tasks_controller.rb b/app/controllers/my_profile/tasks_controller.rb index 9e3c268..db9c061 100644 --- a/app/controllers/my_profile/tasks_controller.rb +++ b/app/controllers/my_profile/tasks_controller.rb @@ -1,11 +1,12 @@ class TasksController < MyProfileController protect 'perform_task', :profile - + def index - @filter = params[:filter_type].blank? ? nil : params[:filter_type] + @filter_type = params[:filter_type].presence + @filter_text = params[:filter_text].presence @task_types = Task.pending_types_for(profile) - @tasks = Task.to(profile).without_spam.pending.of(@filter).order_by('created_at', 'asc').paginate(:per_page => Task.per_page, :page => params[:page]) + @tasks = Task.pending_all(profile, @filter_type, @filter_text).order_by('created_at', 'asc').paginate(:per_page => Task.per_page, :page => params[:page]) @failed = params ? params[:failed] : {} end diff --git a/app/models/task.rb b/app/models/task.rb index 0620e84..c2c8955 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -239,6 +239,10 @@ class Task < ActiveRecord::Base scope :opened, :conditions => { :status => [Task::Status::ACTIVE, Task::Status::HIDDEN] } scope :of, lambda { |type| conditions = type ? "type LIKE '#{type}'" : "1=1"; {:conditions => [conditions]} } scope :order_by, lambda { |attribute, ord| {:order => "#{attribute} #{ord}"} } + scope :like, lambda { |field, value| where("LOWER(#{field}) LIKE ?", "%#{value.downcase}%") if value} + scope :pending_all, lambda { |profile, filter_type, filter_text| + self.to(profile).without_spam.pending.of(filter_type).like('data', filter_text) + } scope :to, lambda { |profile| environment_condition = nil diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index d5da684..e43d328 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -21,11 +21,24 @@ <% end %> +<%= form_tag '#', :method => 'get' do %> + <%= field_set_tag _('Filter'), :class => 'filter_fields' do %> +
+ <%= labelled_select(_('Type of task')+': ', :filter_type, :first, :last, @filter_type, type_collection, {:id => 'filter-type'}) %> +
++ <%= labelled_text_field(_("Text filter")+': ', :filter_text, nil, {:id => 'filter-text',:value => @filter_text}) %> +
++ <%= submit_button(:search, _('Search')) %> +
+ <% end %> +<% end %> + <% if @tasks.empty? %>- <%= labelled_select(_('Filter')+': ', :filter_type, :first, :last, @filter, type_collection, :onchange => "document.location.href = '?filter_type='+this.value")%> + <%= _('No pending tasks for %s') % profile.name %>
- <%= _('No pending tasks for %s') % profile.name %> <% else %> <%= form_tag :action => 'close' do%> <% button_bar do %> @@ -38,14 +51,12 @@- <%= labelled_select(_('Filter')+': ', :filter_type, :first, :last, @filter, type_collection, :onchange => "document.location.href = '?filter_type='+this.value") %> -
-<%= labelled_select(_("Set all to: "), 'set-decisions', 'first', 'last', nil, [['',""],['accept',_("Accept")],['reject',_("Reject")],['skip',_("Skip")]], :id => "up-set-all-tasks-to") %>
- <% @tasks.each do |task| %> - <%= render :partial => 'task', :locals => { :task => task } %> - <% end %> + +<%= labelled_select(_("Set all to: "), 'set-decisions', 'first', 'last', nil, [['',""],['accept',_("Accept")],['reject',_("Reject")],['skip',_("Skip")]], :id => "down-set-all-tasks-to") %>
diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index f510516..458ff86 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -5,7 +5,7 @@ class TasksController; def rescue_action(e) raise e end; end class TasksControllerTest < ActionController::TestCase - noosfero_test :profile => 'testuser' + noosfero_test :profile => 'testuser' def setup @controller = TasksController.new @@ -388,6 +388,28 @@ class TasksControllerTest < ActionController::TestCase assert_includes assigns(:tasks), t3 end + should 'filter tasks by type and data content' do + class CleanHouse < Task; end + class FeedDog < Task; end + Task.stubs(:per_page).returns(3) + requestor = fast_create(Person) + t1 = CleanHouse.create!(:requestor => requestor, :target => profile, :data => {:name => 'Task Test'}) + t2 = CleanHouse.create!(:requestor => requestor, :target => profile) + t3 = FeedDog.create!(:requestor => requestor, :target => profile) + + get :index, :filter_type => t1.type, :filter_text => 'test' + + assert_includes assigns(:tasks), t1 + assert_not_includes assigns(:tasks), t2 + assert_not_includes assigns(:tasks), t3 + + get :index + + assert_includes assigns(:tasks), t1 + assert_includes assigns(:tasks), t2 + assert_includes assigns(:tasks), t3 + end + should 'return tasks ordered accordingly and limited by pages' do time = Time.now person = fast_create(Person) -- libgit2 0.21.2