npverni-will_sort 0.1.0

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.
Files changed (5) hide show
  1. data/README.rdoc +2 -0
  2. data/Rakefile +12 -0
  3. data/lib/will_sort.rb +65 -0
  4. data/will_sort.gemspec +31 -0
  5. metadata +63 -0
data/README.rdoc ADDED
@@ -0,0 +1,2 @@
1
+ = WillSearch
2
+ doc
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('will_sort','0.1.0') do |p|
6
+ p.description = "A wrapper for handeling sort_by params"
7
+ p.url = "http://github.com/npverni/will_sort"
8
+ p.author = "Nathan Verni"
9
+ p.email = "npverni@gmail.com"
10
+ p.ignore_pattern = ["/tmp/*","script/*"]
11
+ p.development_dependencies = []
12
+ end
data/lib/will_sort.rb ADDED
@@ -0,0 +1,65 @@
1
+ module WillSort
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def will_sort(params)
10
+ get_search_order(params,self.default_sort_by,self.default_sort_dir)
11
+ end
12
+
13
+ def get_search_conditions(params)
14
+ conditions = {}
15
+ self.searchable_fields.each do |p|
16
+ if self.param_exists(params,p)
17
+ conditions[p] = params[p]
18
+ end
19
+ end
20
+ conditions
21
+ end
22
+
23
+ def get_search_order(params,default_sort_by,default_dir)
24
+ sort_by = safe_sort_param_or_default(params[:sort_by],default_sort_by)
25
+ dir = safe_sort_dir_or_default(params[:dir],default_dir)
26
+ order = "#{sort_by} #{dir}"
27
+ return order
28
+ end
29
+
30
+ def safe_sort_param_or_default(param,default)
31
+ sortable_fields = self.sortable_fields
32
+ if param.blank?
33
+ return default
34
+ else
35
+ if sortable_fields.include?(param)
36
+ return param
37
+ else
38
+ return default
39
+ end
40
+ end
41
+ end
42
+
43
+ def safe_sort_dir_or_default(param,default)
44
+ sortable_dirs = ['asc','desc']
45
+ if param.blank?
46
+ return default
47
+ else
48
+ if sortable_dirs.include?(param)
49
+ return param
50
+ else
51
+ return default
52
+ end
53
+ end
54
+ end
55
+
56
+ def param_exists(params,param)
57
+ params[param] && !params[param].blank?
58
+ end
59
+
60
+ end
61
+ end
62
+
63
+ class ActiveRecord::Base
64
+ include WillSort
65
+ end
data/will_sort.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{will_sort}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Nathan Verni"]
9
+ s.date = %q{2008-12-04}
10
+ s.description = %q{A wrapper for handeling sort_by params}
11
+ s.email = %q{npverni@gmail.com}
12
+ s.extra_rdoc_files = ["lib/will_sort.rb", "README.rdoc"]
13
+ s.files = ["lib/will_sort.rb", "Manifest", "Rakefile", "README.rdoc", "will_sort.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/npverni/will_sort}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Will_sort", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{will_sort}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{A wrapper for handeling sort_by params}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: npverni-will_sort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Verni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-04 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A wrapper for handeling sort_by params
17
+ email: npverni@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/will_sort.rb
24
+ - README.rdoc
25
+ files:
26
+ - lib/will_sort.rb
27
+ - Manifest
28
+ - Rakefile
29
+ - README.rdoc
30
+ - will_sort.gemspec
31
+ has_rdoc: true
32
+ homepage: http://github.com/npverni/will_sort
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --line-numbers
36
+ - --inline-source
37
+ - --title
38
+ - Will_sort
39
+ - --main
40
+ - README.rdoc
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "1.2"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: will_sort
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: A wrapper for handeling sort_by params
62
+ test_files: []
63
+