Commit fabb654462285f261aa91456ea70145b0630dd80
1 parent
d6bfa0ef
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Refactor on Tasks filter and created a functional test
Showing
5 changed files
with
61 additions
and
13 deletions
Show diff stats
app/controllers/my_profile/tasks_controller.rb
| @@ -3,10 +3,10 @@ class TasksController < MyProfileController | @@ -3,10 +3,10 @@ class TasksController < MyProfileController | ||
| 3 | protect 'perform_task', :profile | 3 | protect 'perform_task', :profile |
| 4 | 4 | ||
| 5 | def index | 5 | def index |
| 6 | - @filter = params[:filter_type].blank? ? nil : params[:filter_type] | 6 | + @filter_type = params[:filter_type] = params[:filter_type].blank? ? nil : params[:filter_type] |
| 7 | @filter_text = params[:filter_text].blank? ? nil : params[:filter_text] | 7 | @filter_text = params[:filter_text].blank? ? nil : params[:filter_text] |
| 8 | @task_types = Task.pending_types_for(profile) | 8 | @task_types = Task.pending_types_for(profile) |
| 9 | - @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]) | 9 | + @tasks = Task.pending_all(profile,params).order_by('created_at', 'asc').paginate(:per_page => Task.per_page, :page => params[:page]) |
| 10 | @failed = params ? params[:failed] : {} | 10 | @failed = params ? params[:failed] : {} |
| 11 | end | 11 | end |
| 12 | 12 | ||
| @@ -67,8 +67,9 @@ class TasksController < MyProfileController | @@ -67,8 +67,9 @@ class TasksController < MyProfileController | ||
| 67 | end | 67 | end |
| 68 | 68 | ||
| 69 | def search_tasks | 69 | def search_tasks |
| 70 | - @filter = params[:filter_type].blank? ? nil : params[:filter_type] | ||
| 71 | - result = Task.to(profile).without_spam.pending.of(@filter).like('data',params[:term]) | 70 | + |
| 71 | + params[:filter_type] = params[:filter_type].blank? ? nil : params[:filter_type] | ||
| 72 | + result = Task.pending_all(profile,params) | ||
| 72 | 73 | ||
| 73 | render :json => result.map { |task| {:label => task.data[:name], :value => task.data[:name]} } | 74 | render :json => result.map { |task| {:label => task.data[:name], :value => task.data[:name]} } |
| 74 | end | 75 | end |
app/models/task.rb
| @@ -240,6 +240,9 @@ class Task < ActiveRecord::Base | @@ -240,6 +240,9 @@ class Task < ActiveRecord::Base | ||
| 240 | scope :of, lambda { |type| conditions = type ? "type LIKE '#{type}'" : "1=1"; {:conditions => [conditions]} } | 240 | scope :of, lambda { |type| conditions = type ? "type LIKE '#{type}'" : "1=1"; {:conditions => [conditions]} } |
| 241 | scope :order_by, lambda { |attribute, ord| {:order => "#{attribute} #{ord}"} } | 241 | scope :order_by, lambda { |attribute, ord| {:order => "#{attribute} #{ord}"} } |
| 242 | scope :like, ->(field,value) { where("LOWER(#{field}) LIKE ?", "%#{value.downcase}%") if value} | 242 | scope :like, ->(field,value) { where("LOWER(#{field}) LIKE ?", "%#{value.downcase}%") if value} |
| 243 | + scope :pending_all, ->(profile, params){ | ||
| 244 | + self.to(profile).without_spam.pending.of(params[:filter_type]).like('data', params[:filter_text]) | ||
| 245 | + } | ||
| 243 | 246 | ||
| 244 | scope :to, lambda { |profile| | 247 | scope :to, lambda { |profile| |
| 245 | environment_condition = nil | 248 | environment_condition = nil |
| @@ -251,6 +254,7 @@ class Task < ActiveRecord::Base | @@ -251,6 +254,7 @@ class Task < ActiveRecord::Base | ||
| 251 | { :conditions => [environment_condition, profile_condition].compact.join(' OR ') } | 254 | { :conditions => [environment_condition, profile_condition].compact.join(' OR ') } |
| 252 | } | 255 | } |
| 253 | 256 | ||
| 257 | + | ||
| 254 | def self.pending_types_for(profile) | 258 | def self.pending_types_for(profile) |
| 255 | Task.to(profile).pending.select('distinct type').map { |t| [t.class.name, t.title] } | 259 | Task.to(profile).pending.select('distinct type').map { |t| [t.class.name, t.title] } |
| 256 | end | 260 | end |
app/views/tasks/index.html.erb
| @@ -25,16 +25,10 @@ | @@ -25,16 +25,10 @@ | ||
| 25 | 25 | ||
| 26 | <%= field_set_tag _('Filter'), :class => 'filter_fields' do %> | 26 | <%= field_set_tag _('Filter'), :class => 'filter_fields' do %> |
| 27 | <p> | 27 | <p> |
| 28 | - <%= labelled_select(_('Type of task')+': ', :filter_type, :first, :last, @filter, type_collection, {:id => 'filter-type'}) %> | 28 | + <%= labelled_select(_('Type of task')+': ', :filter_type, :first, :last, @filter_type, type_collection, {:id => 'filter-type'}) %> |
| 29 | </p> | 29 | </p> |
| 30 | <p> | 30 | <p> |
| 31 | <%= labelled_text_field(_("Text filter")+': ', :filter_text, nil, {:id => 'filter-text-autocomplete',:value => @filter_text}) %> | 31 | <%= labelled_text_field(_("Text filter")+': ', :filter_text, nil, {:id => 'filter-text-autocomplete',:value => @filter_text}) %> |
| 32 | - <script type="text/javascript"> | ||
| 33 | - jQuery('#filter-text-autocomplete').autocomplete({ | ||
| 34 | - source:<%= "'/myprofile/#{profile.identifier}/tasks/search_tasks'" %>+'?filter_type='+jQuery('#filter-type').val(), | ||
| 35 | - minLength:2 | ||
| 36 | - }); | ||
| 37 | - </script> | ||
| 38 | </p> | 32 | </p> |
| 39 | <p> | 33 | <p> |
| 40 | <%= submit_button(:search, _('Search')) %> | 34 | <%= submit_button(:search, _('Search')) %> |
| @@ -42,7 +36,9 @@ | @@ -42,7 +36,9 @@ | ||
| 42 | <% end %> | 36 | <% end %> |
| 43 | <% end %> | 37 | <% end %> |
| 44 | <% if @tasks.empty? %> | 38 | <% if @tasks.empty? %> |
| 45 | - <em><%= _('No pending tasks for %s') % profile.name %></em> | 39 | + <p> |
| 40 | + <em><%= _('No pending tasks for %s') % profile.name %></em> | ||
| 41 | + </p> | ||
| 46 | <% else %> | 42 | <% else %> |
| 47 | <%= form_tag :action => 'close' do%> | 43 | <%= form_tag :action => 'close' do%> |
| 48 | <% button_bar do %> | 44 | <% button_bar do %> |
public/javascripts/tasks.js
| @@ -45,5 +45,21 @@ | @@ -45,5 +45,21 @@ | ||
| 45 | $('.task_title').css('margin-right', $('.task_decisions').width()+'px'); | 45 | $('.task_title').css('margin-right', $('.task_decisions').width()+'px'); |
| 46 | $('.task_title').css('margin-left', $('.task_arrow').width()+'px'); | 46 | $('.task_title').css('margin-left', $('.task_arrow').width()+'px'); |
| 47 | 47 | ||
| 48 | + //Autocomplete tasks by type | ||
| 49 | + $('#filter-text-autocomplete').autocomplete({ | ||
| 50 | + source:function(request,response){ | ||
| 51 | + $.ajax({ | ||
| 52 | + url:document.location.pathname+'/search_tasks', | ||
| 53 | + dataType:'json', | ||
| 54 | + data:{ | ||
| 55 | + filter_text:request.term, | ||
| 56 | + filter_type:jQuery('#filter-type').val() | ||
| 57 | + }, | ||
| 58 | + success:response | ||
| 59 | + }) | ||
| 60 | + }, | ||
| 61 | + minLength:2 | ||
| 62 | + }); | ||
| 63 | + | ||
| 48 | })(jQuery) | 64 | })(jQuery) |
| 49 | 65 |
test/functional/tasks_controller_test.rb
| @@ -5,7 +5,7 @@ class TasksController; def rescue_action(e) raise e end; end | @@ -5,7 +5,7 @@ class TasksController; def rescue_action(e) raise e end; end | ||
| 5 | 5 | ||
| 6 | class TasksControllerTest < ActionController::TestCase | 6 | class TasksControllerTest < ActionController::TestCase |
| 7 | 7 | ||
| 8 | - noosfero_test :profile => 'testuser' | 8 | + noosfero_test :profile => 'testuser' |
| 9 | 9 | ||
| 10 | def setup | 10 | def setup |
| 11 | @controller = TasksController.new | 11 | @controller = TasksController.new |
| @@ -30,6 +30,37 @@ class TasksControllerTest < ActionController::TestCase | @@ -30,6 +30,37 @@ class TasksControllerTest < ActionController::TestCase | ||
| 30 | assert assigns(:tasks) | 30 | assert assigns(:tasks) |
| 31 | end | 31 | end |
| 32 | 32 | ||
| 33 | + should 'get filtered tasks to autocomplete text field' do | ||
| 34 | + | ||
| 35 | + #Create a admin user and a simple user | ||
| 36 | + profile_admin = create_user('admin_tester').person | ||
| 37 | + Environment.default.add_admin(profile_admin) | ||
| 38 | + user = fast_create(Person,:name => 'FakeUser') | ||
| 39 | + | ||
| 40 | + #Create a task of type 'ModerateUserRegistration' | ||
| 41 | + task_data = { | ||
| 42 | + :target => Environment.default, | ||
| 43 | + :spam => false, | ||
| 44 | + :data => {:user_id => user.id,:name => user.name} | ||
| 45 | + } | ||
| 46 | + ModerateUserRegistration.create!(task_data) | ||
| 47 | + | ||
| 48 | + #Use admin user to your profile with a pending task above | ||
| 49 | + @controller.stubs(:profile).returns(profile_admin) | ||
| 50 | + login_as profile_admin.identifier | ||
| 51 | + | ||
| 52 | + #Perform a http request to 'search_task' action with params | ||
| 53 | + get :search_tasks, :filter_type =>'ModerateUserRegistration', :filter_text => 'Fak' | ||
| 54 | + | ||
| 55 | + assert_response :success | ||
| 56 | + | ||
| 57 | + #Check if json response matches with a 'FakeUser' | ||
| 58 | + json_response = ActiveSupport::JSON.decode(@response.body) | ||
| 59 | + value = json_response[0]['value'] | ||
| 60 | + | ||
| 61 | + assert_equal value, 'FakeUser' | ||
| 62 | + end | ||
| 63 | + | ||
| 33 | should 'list pending tasks without spam' do | 64 | should 'list pending tasks without spam' do |
| 34 | requestor = fast_create(Person) | 65 | requestor = fast_create(Person) |
| 35 | task_spam = Task.create!(:requestor => requestor, :target => profile, :spam => true) | 66 | task_spam = Task.create!(:requestor => requestor, :target => profile, :spam => true) |