search_scope 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('search_scope', '0.1.7') do |p|
5
+ Echoe.new('search_scope', '0.1.8') do |p|
6
6
  p.description = "Simplify searching a model by defining custom named_scopes."
7
7
  p.project = 'search-scope'
8
8
  p.url = "http://rubyforge.org/projects/search-scope"
data/init.rb CHANGED
@@ -1 +1 @@
1
- require 'search_score'
1
+ require 'search_scope'
data/lib/search_scope.rb CHANGED
@@ -1,22 +1,36 @@
1
1
  module SearchScope
2
2
 
3
3
  class SortScope
4
- attr_reader :name, :label, :order, :include_model
5
- def initialize(name, label, order, include_model)
6
- @name, @label, @order, @include_model = name, label, order, include_model
4
+ attr_reader :name, :label, :order, :reverse, :include_model
5
+ def initialize(name, label, order, reverse, include_model)
6
+ @name, @label, @order, @reverse, @include_model = name, label, order, reverse, include_model
7
7
  end
8
8
  end
9
9
 
10
10
  def sort_search_by(name, options={})
11
11
  options[:label] ||= name.to_s.titleize
12
- options[:order] ||= "#{table_name}.#{name.to_s}"
12
+ if options[:order].blank?
13
+ #this sets up the default order and reverse options. After the first one is set, the rest use the first sort option as sheir secondary order
14
+ options[:order] = "#{table_name}.#{name.to_s}"
15
+ options[:reverse] = "#{table_name}.#{name.to_s} DESC"
16
+ if sort_choices.first
17
+ options[:order] += ", #{sort_choices.first.order}"
18
+ options[:reverse] += ", #{sort_choices.first.order}"
19
+ #this gives you a comma separated string of includes if they exist
20
+ options[:include_model] = [options[:include_model],sort_choices.first.include_model].compact.join ',' if sort_choices.first.include_model
21
+ end
22
+ end
23
+ options[:order], options[:reverse] = options[:reverse], options[:order] if options[:reverse_orders]
24
+
25
+ # puts "***(#{self.name}).sort_search_by :#{name}, #{options.inspect}"
26
+
13
27
  raise "you must supply a Symbol for a name to new_sortable_by (#{name.inspect})" unless name.is_a? Symbol
14
28
  return if sort_choices_hash.keys.include? name.to_s
15
29
  #the first sort added becomes the default sorting for all searches
16
30
  @default_sort_choice ||= name
17
31
  #TODO put this back and get rid of the return above once I figure out how to reset the class vars when the class is reloaded
18
32
  # raise "there is already a sortable defined for the name (#{name.inspect}" if sort_choices_hash.keys.include? name.to_s
19
- sort_choices_hash[name] = SortScope.new(name, options[:label], options[:order], options[:include])
33
+ sort_choices_hash[name] = SortScope.new(name, options[:label], options[:order], options[:reverse], options[:include])
20
34
  sort_choices << sort_choices_hash[name]
21
35
  end
22
36
 
@@ -60,11 +74,17 @@ module SearchScope
60
74
  @quick_search_scopes ||= []
61
75
  end
62
76
 
63
- def sort_search_by_options(sort_by)
77
+ def sort_search_by_options(sort_by, reverse=nil)
78
+ if reverse.nil? || reverse.empty?
79
+ reverse = false
80
+ else
81
+ reverse = ! %w{ false no f n }.include?(reverse)
82
+ end
64
83
  choices_hash = sort_choices_hash[sort_by]
65
84
  return {} unless choices_hash
85
+ raise "There is no :reverse option specified for sort_search_by #{sort_by.inspect} (#{name})" if reverse && choices_hash.reverse.blank?
66
86
  hash = {
67
- :order => choices_hash.order,
87
+ :order => reverse ? choices_hash.reverse : choices_hash.order,
68
88
  :include => choices_hash.include_model,
69
89
  }
70
90
  hash
@@ -149,7 +169,7 @@ module SearchScope
149
169
 
150
170
  params[:sort_by] ||= default_sort_choice
151
171
  if params[:sort_by]
152
- aggregate_scope = aggregate_scope.scoped sort_search_by_options(params[:sort_by])
172
+ aggregate_scope = aggregate_scope.scoped sort_search_by_options(params[:sort_by], params[:sort_reverse])
153
173
  end
154
174
  if paginate
155
175
  aggregate_scope.paginate(:all, :page => params[:page])
data/search_scope.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{search_scope}
5
- s.version = "0.1.7"
5
+ s.version = "0.1.8"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Ryan Owens"]
9
- s.date = %q{2009-01-07}
9
+ s.date = %q{2009-01-20}
10
10
  s.description = %q{Simplify searching a model by defining custom named_scopes.}
11
11
  s.email = %q{ryan@infoether.com}
12
12
  s.extra_rdoc_files = ["CHANGELOG", "lib/search_scope.rb", "README"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_scope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Owens
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-07 00:00:00 -05:00
12
+ date: 2009-01-20 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15