From 7bcd1c3138b3b3f16987e6b2daa17a19ae13bba4 Mon Sep 17 00:00:00 2001 From: Michel Felipe Date: Wed, 28 Jan 2015 17:15:15 -0300 Subject: [PATCH] Filter tasks by text on task list --- app/controllers/my_profile/tasks_controller.rb | 7 ++++--- app/models/task.rb | 4 ++++ app/views/tasks/index.html.erb | 27 +++++++++++++++++++-------- test/functional/tasks_controller_test.rb | 24 +++++++++++++++++++++++- 4 files changed, 50 insertions(+), 12 deletions(-) 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 @@