Refactor search controllers and scopes. Add search query whitespace strip. Refs #263

This commit is contained in:
Pavel Chipiga 2012-03-28 02:58:03 +03:00
parent c9f4cf0ea4
commit 9ee350b007
9 changed files with 17 additions and 18 deletions

View File

@ -5,13 +5,13 @@ class ProjectsController < ApplicationController
def index
@projects = Project.accessible_by(current_ability, :membered)
# @projects = @projects.search(params[:query]).search_order if params[:query].present?
#puts prepare_list(@projects).inspect
respond_to do |format|
format.html { @projects = @projects.recent.paginate(:page => params[:page], :per_page => 25) }
format.json { @projects = prepare_list(@projects) }
end
# @projects = @projects.search(params[:query]).search_order if params[:query]
end
def new
@ -93,7 +93,7 @@ class ProjectsController < ApplicationController
order = "#{colName[sort_col.to_i]} #{sort_dir}"
res[:total_count] = projects.count
projects = projects.where(['projects.name ILIKE ?', "%#{ params[:sSearch] }%"]) if params[:sSearch] and !params[:sSearch].empty?
projects = projects.search(params[:sSearch]).search_order if params[:sSearch].present?
res[:filtered_count] = projects.count
projects = projects.order(order)

View File

@ -19,11 +19,8 @@ class RepositoriesController < ApplicationController
end
def show
if params[:query]
@projects = @repository.projects.recent.by_name("%#{params[:query]}%").paginate :page => params[:project_page], :per_page => 30
else
@projects = @repository.projects.recent.paginate :page => params[:project_page], :per_page => 30
end
@projects = @repository.projects.recent.paginate :page => params[:project_page], :per_page => 30
@projects = @projects.search(params[:query]).search_order if params[:query].present?
end
def new
@ -91,7 +88,7 @@ class RepositoriesController < ApplicationController
@projects = @projects.paginate(:page => (params[:iDisplayStart].to_i/params[:iDisplayLength].to_i).to_i + 1, :per_page => params[:iDisplayLength])
@total_projects = @projects.count
@projects = @projects.where(['projects.name ILIKE ?', "#{params[:sSearch]}%"]) if params[:sSearch] and !params[:sSearch].empty?
@projects = @projects.search(params[:sSearch]).search_order if params[:sSearch].present?
@total_project = @projects.count
@projects = @projects.order(order)#.includes(:owner) #WTF????

View File

@ -20,6 +20,6 @@ class SearchController < ApplicationController
def find_collection(type)
var = :"@#{type}"
instance_variable_set var, type.classify.constantize.search(params[:query].strip).search_order.paginate(:page => params[:page]) unless instance_variable_defined?(var)
instance_variable_set var, type.classify.constantize.opened.search(params[:query]).search_order.paginate(:page => params[:page]) unless instance_variable_defined?(var)
end
end

View File

@ -18,7 +18,8 @@ class Group < ActiveRecord::Base
validate { errors.add(:uname, :taken) if User.where('uname LIKE ?', uname).present? }
scope :search_order, order("CHAR_LENGTH(uname) ASC")
scope :search, lambda {|q| where("uname ILIKE ?", "%#{q}%")}
scope :search, lambda {|q| where("uname ILIKE ?", "%#{q.strip}%")}
scope :opened, where('1=1')
scope :by_owner, lambda {|owner| where(:owner_id => owner.id)}
scope :by_admin, lambda {|admin| joins(:relations).where(:'relations.role' => 'admin', :'relations.target_id' => admin.id, :'relations.target_type' => 'User')}

View File

@ -27,9 +27,9 @@ class Platform < ActiveRecord::Base
after_update :update_owner_relation
scope :search_order, order("CHAR_LENGTH(name) ASC")
scope :search, lambda {|q| where("name ILIKE ?", "%#{q}%").open}
scope :search, lambda {|q| where("name ILIKE ?", "%#{q.strip}%")}
scope :by_visibilities, lambda {|v| where(:visibility => v)}
scope :open, where(:visibility => 'open')
scope :opened, where(:visibility => 'open')
scope :hidden, where(:visibility => 'hidden')
scope :main, where(:platform_type => 'main')
scope :personal, where(:platform_type => 'personal')
@ -43,7 +43,7 @@ class Platform < ActiveRecord::Base
pair = blank_pair if pair.blank?
urpmi_commands = ActiveSupport::OrderedHash.new
Platform.main.open.where(:distrib_type => APP_CONFIG['distr_types'].first).each do |pl|
Platform.main.opened.where(:distrib_type => APP_CONFIG['distr_types'].first).each do |pl|
urpmi_commands[pl.name] = {}
local_pair = pl.id != self.id ? blank_pair : pair
head = hidden? ? "http://#{local_pair[:login]}@#{local_pair[:pass]}:#{host}/private/" : "http://#{host}/downloads/"

View File

@ -31,10 +31,10 @@ class Project < ActiveRecord::Base
scope :recent, order("name ASC")
scope :search_order, order("CHAR_LENGTH(name) ASC")
scope :search, lambda {|q| by_name("%#{q}%").open}
scope :search, lambda {|q| by_name("%#{q.strip}%")}
scope :by_name, lambda {|name| where('projects.name ILIKE ?', name)}
scope :by_visibilities, lambda {|v| where(:visibility => v)}
scope :open, where(:visibility => 'open')
scope :opened, where(:visibility => 'open')
scope :addable_to_repository, lambda { |repository_id| where("projects.id NOT IN (SELECT project_to_repositories.project_id FROM project_to_repositories WHERE (project_to_repositories.repository_id = #{ repository_id }))") }
scope :automateable, where("projects.id NOT IN (SELECT auto_build_lists.project_id FROM auto_build_lists)")

View File

@ -49,7 +49,8 @@ class User < ActiveRecord::Base
attr_accessor :login
scope :search_order, order("CHAR_LENGTH(uname) ASC")
scope :search, lambda {|q| where("uname ILIKE ?", "%#{q}%")}
scope :search, lambda {|q| where("uname ILIKE ?", "%#{q.strip}%")}
scope :opened, where('1=1')
scope :banned, where(:role => 'banned')
scope :admin, where(:role => 'admin')
scope :real, where(:role => ['', nil])

View File

@ -1,4 +1,4 @@
- platforms = Platform.main.open.where(:distrib_type => APP_CONFIG['distr_types'].first).order('name ASC')
- platforms = Platform.main.opened.where(:distrib_type => APP_CONFIG['distr_types'].first).order('name ASC')
- if platforms.count > 0
.command_select

View File

@ -72,7 +72,7 @@ namespace :deploy do
task :precompile, :roles => :web, :except => { :no_release => true } do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} app/assets/ lib/assets/ vendor/assets/ | wc -l").to_i > 0
run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
run "cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile"
else
logger.info "Skipping asset pre-compilation because there were no asset changes"
end