Permalink
Browse files

fixing merge conflict with commit from jason

  • Loading branch information...
2 parents be6da0e + 9aa2d4c commit bc412cefd566bc7b79a2034bb2b63c4c16ace110 @croaky croaky committed Jan 2, 2009
@@ -14,7 +14,6 @@ module ClassMethods
def sortable_attributes(*args)
mappings = pop_hash_from_list(args)
acceptable_columns = join_array_and_hash_keys(args, mappings)
-
define_sort_order(acceptable_columns, mappings)
end
@@ -37,7 +36,11 @@ def define_sort_order(acceptable_columns, mappings)
column = params[:sort] || 'created_on'
if params[:sort] && acceptable_columns.include?(column)
column = mappings[column.to_sym] || column
- "#{column} #{direction}"
+ if column.is_a?(Array)
+ column.map{ |col| "#{col} #{direction}" }.join(',')
+ else
+ "#{column} #{direction}"
+ end
else
"#{acceptable_columns.first} #{default_sort_direction(default)}"
end
@@ -2,9 +2,9 @@ module SortableTable
module Shoulda
def should_sort_by(attribute, options = {}, &block)
- collection = get_collection_name_from_test_name
- model_under_test = get_model_under_test_from_test_name
-
+ collection = get_collection_name_from_test_name(options[:collection])
+ model_under_test = get_model_under_test_from_test_name(options[:model_name])
+
block = block || default_sorting_block(model_under_test, attribute)
action = options[:action] || default_sorting_action
@@ -48,21 +48,22 @@ def should_display_sortable_table_header_for(*valid_sorts)
protected
- def get_collection_name_from_test_name
+ def get_collection_name_from_test_name(override)
collection = self.name.underscore.gsub(/_controller_test/, '')
collection = remove_namespacing(collection)
- collection.to_sym
+ (override || collection).to_sym
end
-
+
+ def get_model_under_test_from_test_name(override)
+ model_name = self.name.gsub(/ControllerTest/, '')
+ (override || model_name).singularize.constantize
+ end
+
def remove_namespacing(string)
string.slice!(0..string.rindex('/')) if string.include?('/')
string
end
- def get_model_under_test_from_test_name
- self.name.gsub(/ControllerTest/, '').singularize.constantize
- end
-
def default_sorting_block(model_under_test, attribute)
block = handle_boolean_attribute(model_under_test, attribute)
block ||= attribute
@@ -1,12 +1,14 @@
class UsersController < ApplicationController
-
- sortable_attributes :name,
- :age,
- :email,
- :group => "groups.name"
-
+
+ sortable_attributes :name,
+ :age,
+ :email,
+ :group => "groups.name",
+ :name_and_email => ['name', 'users.email']
+
def index
@users = User.find :all, :order => sort_order("ascending")
+ @users_dupe = @users
end
end
@@ -1,20 +1,36 @@
require File.dirname(__FILE__) + '/../test_helper'
class UsersControllerTest < ActionController::TestCase
-
+
context "enough Users to sort" do
- setup { 2.times { Factory :user } }
+ setup do
+ Factory(:user, :name => 'a')
+ Factory(:user, :name => 'b')
+ Factory(:user, :email => 'a@a.com')
+ Factory(:user, :email => 'b@b.com')
+ Factory(:user, :age => 20)
+ Factory(:user, :age => 30)
+ end
- should_sort_by_attributes(:name,
- :age,
- :email) do |sort, order|
+ should_sort_by_attributes :name, :email, :age do |sort, order|
get :index, :sort => sort, :order => order
end
+ should_sort_by :name_and_email, {} do |user|
+ "#{user.name} #{user.email}"
+ end
+
+ context 'with a non-standard instance variable name' do
+ should_sort_by :name, { :collection => 'users_dupe', :model_name => 'user' } do |user|
+ user.name
+ end
+ end
+
context "GET to #index" do
setup { get :index }
should_display_sortable_table_header_for :name, :email, :age
end
end
end
+

0 comments on commit bc412ce

Please sign in to comment.