diff --git a/app/controllers/my_profile/tasks_controller.rb b/app/controllers/my_profile/tasks_controller.rb index f5f28ff..56ee3c2 100644 --- a/app/controllers/my_profile/tasks_controller.rb +++ b/app/controllers/my_profile/tasks_controller.rb @@ -3,10 +3,10 @@ class TasksController < MyProfileController protect 'perform_task', :profile def index - @filter = params[:filter_type].blank? ? nil : params[:filter_type] + @filter_type = params[:filter_type] = params[:filter_type].blank? ? nil : params[:filter_type] @filter_text = params[:filter_text].blank? ? nil : params[:filter_text] @task_types = Task.pending_types_for(profile) - @tasks = Task.to(profile).without_spam.pending.of(@filter).like('data',@filter_text).order_by('created_at', 'asc').paginate(:per_page => Task.per_page, :page => params[:page]) + @tasks = Task.pending_all(profile,params).order_by('created_at', 'asc').paginate(:per_page => Task.per_page, :page => params[:page]) @failed = params ? params[:failed] : {} end @@ -67,8 +67,9 @@ class TasksController < MyProfileController end def search_tasks - @filter = params[:filter_type].blank? ? nil : params[:filter_type] - result = Task.to(profile).without_spam.pending.of(@filter).like('data',params[:term]) + + params[:filter_type] = params[:filter_type].blank? ? nil : params[:filter_type] + result = Task.pending_all(profile,params) render :json => result.map { |task| {:label => task.data[:name], :value => task.data[:name]} } end diff --git a/app/models/task.rb b/app/models/task.rb index 3e4103b..4e35674 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -240,6 +240,9 @@ class Task < ActiveRecord::Base scope :of, lambda { |type| conditions = type ? "type LIKE '#{type}'" : "1=1"; {:conditions => [conditions]} } scope :order_by, lambda { |attribute, ord| {:order => "#{attribute} #{ord}"} } scope :like, ->(field,value) { where("LOWER(#{field}) LIKE ?", "%#{value.downcase}%") if value} + scope :pending_all, ->(profile, params){ + self.to(profile).without_spam.pending.of(params[:filter_type]).like('data', params[:filter_text]) + } scope :to, lambda { |profile| environment_condition = nil @@ -251,6 +254,7 @@ class Task < ActiveRecord::Base { :conditions => [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 129014f..62b7657 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -25,16 +25,10 @@ <%= field_set_tag _('Filter'), :class => 'filter_fields' do %>
- <%= labelled_select(_('Type of task')+': ', :filter_type, :first, :last, @filter, type_collection, {:id => 'filter-type'}) %> + <%= 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')) %> @@ -42,7 +36,9 @@ <% end %> <% end %> <% if @tasks.empty? %> - <%= _('No pending tasks for %s') % profile.name %> +
+ <%= _('No pending tasks for %s') % profile.name %> +
<% else %> <%= form_tag :action => 'close' do%> <% button_bar do %> diff --git a/public/javascripts/tasks.js b/public/javascripts/tasks.js index 52f450b..3d1f9ef 100644 --- a/public/javascripts/tasks.js +++ b/public/javascripts/tasks.js @@ -45,5 +45,21 @@ $('.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) diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 3517bba..c0b6361 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 @@ -30,6 +30,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 + get :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) -- libgit2 0.21.2