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 | 3 | protect 'perform_task', :profile |
| 4 | 4 | |
| 5 | 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 | 7 | @filter_text = params[:filter_text].blank? ? nil : params[:filter_text] |
| 8 | 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 | 10 | @failed = params ? params[:failed] : {} |
| 11 | 11 | end |
| 12 | 12 | |
| ... | ... | @@ -67,8 +67,9 @@ class TasksController < MyProfileController |
| 67 | 67 | end |
| 68 | 68 | |
| 69 | 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 | 74 | render :json => result.map { |task| {:label => task.data[:name], :value => task.data[:name]} } |
| 74 | 75 | end | ... | ... |
app/models/task.rb
| ... | ... | @@ -240,6 +240,9 @@ class Task < ActiveRecord::Base |
| 240 | 240 | scope :of, lambda { |type| conditions = type ? "type LIKE '#{type}'" : "1=1"; {:conditions => [conditions]} } |
| 241 | 241 | scope :order_by, lambda { |attribute, ord| {:order => "#{attribute} #{ord}"} } |
| 242 | 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 | 247 | scope :to, lambda { |profile| |
| 245 | 248 | environment_condition = nil |
| ... | ... | @@ -251,6 +254,7 @@ class Task < ActiveRecord::Base |
| 251 | 254 | { :conditions => [environment_condition, profile_condition].compact.join(' OR ') } |
| 252 | 255 | } |
| 253 | 256 | |
| 257 | + | |
| 254 | 258 | def self.pending_types_for(profile) |
| 255 | 259 | Task.to(profile).pending.select('distinct type').map { |t| [t.class.name, t.title] } |
| 256 | 260 | end | ... | ... |
app/views/tasks/index.html.erb
| ... | ... | @@ -25,16 +25,10 @@ |
| 25 | 25 | |
| 26 | 26 | <%= field_set_tag _('Filter'), :class => 'filter_fields' do %> |
| 27 | 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 | 29 | </p> |
| 30 | 30 | <p> |
| 31 | 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 | 32 | </p> |
| 39 | 33 | <p> |
| 40 | 34 | <%= submit_button(:search, _('Search')) %> |
| ... | ... | @@ -42,7 +36,9 @@ |
| 42 | 36 | <% end %> |
| 43 | 37 | <% end %> |
| 44 | 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 | 42 | <% else %> |
| 47 | 43 | <%= form_tag :action => 'close' do%> |
| 48 | 44 | <% button_bar do %> | ... | ... |
public/javascripts/tasks.js
| ... | ... | @@ -45,5 +45,21 @@ |
| 45 | 45 | $('.task_title').css('margin-right', $('.task_decisions').width()+'px'); |
| 46 | 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 | 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 | 5 | |
| 6 | 6 | class TasksControllerTest < ActionController::TestCase |
| 7 | 7 | |
| 8 | - noosfero_test :profile => 'testuser' | |
| 8 | + noosfero_test :profile => 'testuser' | |
| 9 | 9 | |
| 10 | 10 | def setup |
| 11 | 11 | @controller = TasksController.new |
| ... | ... | @@ -30,6 +30,37 @@ class TasksControllerTest < ActionController::TestCase |
| 30 | 30 | assert assigns(:tasks) |
| 31 | 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 | 64 | should 'list pending tasks without spam' do |
| 34 | 65 | requestor = fast_create(Person) |
| 35 | 66 | task_spam = Task.create!(:requestor => requestor, :target => profile, :spam => true) | ... | ... |