diff --git a/app/controllers/my_profile/tasks_controller.rb b/app/controllers/my_profile/tasks_controller.rb index ef35414..1888ca4 100644 --- a/app/controllers/my_profile/tasks_controller.rb +++ b/app/controllers/my_profile/tasks_controller.rb @@ -14,16 +14,15 @@ class TasksController < MyProfileController @filter_text = params[:filter_text].presence @filter_responsible = params[:filter_responsible] @task_types = Task.pending_types_for(profile) - - @tasks = Task.pending_all(profile, @filter_type, @filter_text).order_by('created_at', 'asc') + @tasks = Task.pending_all(profile, @filter_type, @filter_text).order_by('created_at', 'asc').paginate(:per_page => Task.per_page, :page => params[:page]) @tasks = @tasks.where(:responsible_id => @filter_responsible.to_i != -1 ? @filter_responsible : nil) if @filter_responsible.present? @tasks = @tasks.paginate(:per_page => Task.per_page, :page => params[:page]) - @failed = params ? params[:failed] : {} @responsible_candidates = profile.members.by_role(profile.roles.reject {|r| !r.has_permission?('perform_task')}) if profile.organization? @view_only = !current_person.has_permission?(:perform_task, profile) + end def processed @@ -95,4 +94,12 @@ class TasksController < MyProfileController @ticket = Ticket.where('(requestor_id = ? or target_id = ?) and id = ?', profile.id, profile.id, params[:id]).first end + def search_tasks + filter_type = params[:filter_type].presence + filter_text = params[:filter_text].presence + result = Task.pending_all(profile,filter_type, filter_text) + + render :json => result.map { |task| {:label => task.data[:name], :value => task.data[:name]} } + end + end diff --git a/app/models/task.rb b/app/models/task.rb index cd867fa..6507bae 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -324,6 +324,7 @@ class Task < ActiveRecord::Base where [environment_condition, profile_condition].compact.join(' OR ') } + def self.pending_types_for(profile) Task.to(profile).pending.select('distinct type').map { |t| [t.class.name, t.title] } end diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index baa32e7..82ae899 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -21,25 +21,20 @@ <% 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}) %> -

- <% if profile.organization? %> -

- <%= labelled_select(_('Assigned to')+': ', :filter_responsible, :id, :name, @filter_responsible, [OpenStruct.new(:name => _('All'), :id => nil), OpenStruct.new(:name => _('Unassigned'), :id => -1)] + @responsible_candidates, :class => 'filter_responsible') %> -

+<%= form_tag '#', :method => 'post' 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-autocomplete',:value => @filter_text}) %> +

+

+ <%= submit_button(:search, _('Search')) %> +

<% end %> -

- <%= submit_button(:search, _('Search')) %> -

- <% end %> <% end %> - <% if @tasks.empty? %>

<%= _('No pending tasks for %s') % profile.name %> @@ -59,18 +54,18 @@

<%= labelled_select(_("Set all to: "), 'set-decisions', 'first', 'last', nil, [['',""],['accept',_("Accept")],['reject',_("Reject")],['skip',_("Skip")]], :id => "up-set-all-tasks-to") %>

- <% end %> + <% end %>
<%= render :partial => 'task', :collection => @tasks %>
<% unless @view_only %> -

- <%= labelled_select(_("Set all to: "), 'set-decisions', 'first', 'last', nil, [['',""],['accept',_("Accept")],['reject',_("Reject")],['skip',_("Skip")]], :id => "down-set-all-tasks-to") %> -

+

+ <%= labelled_select(_("Set all to: "), 'set-decisions', 'first', 'last', nil, [['',""],['accept',_("Accept")],['reject',_("Reject")],['skip',_("Skip")]], :id => "down-set-all-tasks-to") %> +

<% end %> - + <%= pagination_links(@tasks)%> diff --git a/public/javascripts/tasks.js b/public/javascripts/tasks.js index 069821a..6153bf1 100644 --- a/public/javascripts/tasks.js +++ b/public/javascripts/tasks.js @@ -58,6 +58,22 @@ $('.task_title').css('margin-right', $('.task_decisions').width()+'px'); $('.task_title').css('margin-left', $('.task_arrow').width()+'px'); + //Autocomplete tasks by type + $('#filter-text-autocomplete').autocomplete({ + source:function(request,response){ + $.ajax({ + url:document.location.pathname+'/search_tasks', + dataType:'json', + data:{ + filter_text:request.term, + filter_type:jQuery('#filter-type').val() + }, + success:response + }) + }, + minLength:2 + }); + })(jQuery) function change_task_responsible(el) { diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 8137ce6..290d0f8 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -4,6 +4,7 @@ require 'tasks_controller' class TasksControllerTest < ActionController::TestCase self.default_params = {profile: 'testuser'} + def setup @controller = TasksController.new @request = ActionController::TestRequest.new @@ -27,6 +28,37 @@ class TasksControllerTest < ActionController::TestCase assert assigns(:tasks) end + should 'get filtered tasks to autocomplete text field' do + + #Create a admin user and a simple user + profile_admin = create_user('admin_tester').person + Environment.default.add_admin(profile_admin) + user = fast_create(Person,:name => 'FakeUser') + + #Create a task of type 'ModerateUserRegistration' + task_data = { + :target => Environment.default, + :spam => false, + :data => {:user_id => user.id,:name => user.name} + } + ModerateUserRegistration.create!(task_data) + + #Use admin user to your profile with a pending task above + @controller.stubs(:profile).returns(profile_admin) + login_as profile_admin.identifier + + #Perform a http request to 'search_task' action with params + post :search_tasks, :filter_type =>'ModerateUserRegistration', :filter_text => 'Fak' + + assert_response :success + + #Check if json response matches with a 'FakeUser' + json_response = ActiveSupport::JSON.decode(@response.body) + value = json_response[0]['value'] + + assert_equal value, 'FakeUser' + end + should 'list pending tasks without spam' do requestor = fast_create(Person) task_spam = Task.create!(:requestor => requestor, :target => profile, :spam => true) @@ -437,13 +469,13 @@ class TasksControllerTest < ActionController::TestCase t2 = CleanHouse.create!(:requestor => requestor, :target => profile) t3 = FeedDog.create!(:requestor => requestor, :target => profile) - get :index, :filter_type => t1.type, :filter_text => 'test' + post :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 + post :index assert_includes assigns(:tasks), t1 assert_includes assigns(:tasks), t2 -- libgit2 0.21.2