From 1cf2660cda3664b993b65300d7d5646fc639e0e4 Mon Sep 17 00:00:00 2001 From: Pavel Chipiga Date: Thu, 8 Mar 2012 02:32:12 +0200 Subject: [PATCH] Fix search order. Refs #255 --- app/controllers/search_controller.rb | 2 +- app/models/group.rb | 1 + app/models/platform.rb | 1 + app/models/user.rb | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 109dd1f0f..f174a20bc 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -20,6 +20,6 @@ class SearchController < ApplicationController def find_collection(type) var = :"@#{type}" - instance_variable_set var, type.classify.constantize.search(params[:query]).paginate(:page => params[:page]) unless instance_variable_defined?(var) + instance_variable_set var, type.classify.constantize.search(params[:query]).search_order.paginate(:page => params[:page]) unless instance_variable_defined?(var) end end diff --git a/app/models/group.rb b/app/models/group.rb index 4e0ce3981..ada4880a1 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -17,6 +17,7 @@ class Group < ActiveRecord::Base validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ } 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 :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')} diff --git a/app/models/platform.rb b/app/models/platform.rb index fb0ee7f63..5399e9765 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -26,6 +26,7 @@ class Platform < ActiveRecord::Base after_destroy lambda { umount_directory_for_rsync unless hidden? } after_update :update_owner_relation + scope :search_order, order("CHAR_LENGTH(name) ASC") scope :search, lambda {|q| where("name ILIKE ?", "%#{q}%").open} scope :by_visibilities, lambda {|v| where(:visibility => v)} scope :open, where(:visibility => 'open') diff --git a/app/models/user.rb b/app/models/user.rb index d1dd648be..babf50819 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -42,6 +42,7 @@ class User < ActiveRecord::Base attr_readonly :uname attr_accessor :login + scope :search_order, order("CHAR_LENGTH(uname) ASC") scope :search, lambda {|q| where("uname ILIKE ?", "%#{q}%")} after_create lambda { self.create_notifier }