Skip to content

Commit

Permalink
more refactoring of shoulda macros to small, intention-revealing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Croak committed Dec 30, 2008
1 parent c45d892 commit 608a27e
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions shoulda_macros/sortable_table.rb
Expand Up @@ -4,14 +4,9 @@ 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

unless block
block = handle_boolean_attribute(model_under_test, attribute)
block ||= attribute
end

action = options[:action]
action ||= default_sorting_action

block = block || default_sorting_block(model_under_test, attribute)
action = options[:action] || default_sorting_action

%w(ascending descending).each do |direction|
should "sort by #{attribute.to_s} #{direction}" do
Expand Down Expand Up @@ -59,20 +54,18 @@ def get_collection_name_from_test_name
collection.to_sym
end

def get_model_under_test_from_test_name
self.name.gsub(/ControllerTest/, '').singularize.constantize
end

def remove_namespacing(string)
string.slice!(0..string.rindex('/')) if string.include?('/')
string
end

def attribute_is_boolean?(model_under_test, attribute)
db_column = model_under_test.columns.select { |each|
each.name == attribute.to_s
}.first
db_column.type == :boolean
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
end

def handle_boolean_attribute(model_under_test, attribute)
Expand All @@ -81,6 +74,13 @@ def handle_boolean_attribute(model_under_test, attribute)
end
end

def attribute_is_boolean?(model_under_test, attribute)
db_column = model_under_test.columns.select { |each|
each.name == attribute.to_s
}.first
db_column.type == :boolean
end

def default_sorting_action
lambda do |sort, direction|
get :index, :sort => sort, :order => direction
Expand Down

0 comments on commit 608a27e

Please sign in to comment.