simple_search 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,17 +1,64 @@
1
- = simple_search
1
+ = SimpleSearch
2
2
 
3
- Description goes here.
3
+ A rather unimaginatively named gem designed to provide simple search functionality to ActiveRecord-backed
4
+ models and, more usefully, views.
4
5
 
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
6
+ Given a model Widget that belongs_to a WidgetType, shouldn't you be able to do something like this ...
7
+
8
+ <% form_for :search, @search, :html => {:method => :get} do |f| %>
9
+ <%= f.label :name %>
10
+ <%= f.text_field :name %>
11
+ <%= f.label :widget_type_name_starts_with %>
12
+ <%= f.text_field :widget_type_name_starts_with %>
13
+ <%= f.submit %>
14
+ <% end %>
15
+
16
+ ... to create a search form?
17
+
18
+ Yes. Yes, you should.
19
+
20
+ == Example
21
+
22
+ Let's say you're going to add a search form to your index page like the one above.
23
+
24
+ In your controller:
25
+
26
+ Grab the search params or set a default.
27
+
28
+ search = params[:search] || {}
29
+
30
+ Merge in any additional criteria, or add an association to the search. Here,
31
+ I only want to find Widgets that are less than a week old, and I want to be able
32
+ to search on attributes of the Widget's WidgetType as well.
33
+
34
+ @search = SimpleSearch::Search.new(
35
+ Widget,
36
+ search.merge(:created_at_from => 1.week.ago, :search_associations => :widget_type)
37
+ )
38
+
39
+ This will do a full search, and use pagination. All options are passed through to find or
40
+ paginate (will_paginate), as required.
41
+ @widgets = @search.search(:page => params[:page])
42
+
43
+ Or if you just want to count the records that would be matched.
44
+
45
+ @count = @search.count
46
+
47
+ Then in your view:
48
+
49
+ <% form_for :search, @search, :html => {:method => :get} do |f| %>
50
+ <%= f.label :name %>
51
+ <%= f.text_field :name %>
52
+ <%= f.label :widget_type_name_starts_with %>
53
+ <%= f.text_field :widget_type_name_starts_with %>
54
+ <%= f.submit %>
55
+ <% end %>
56
+
57
+ Remember, searches are idempotent so to appease the HTTP gods (and make it easy to bookmark
58
+ search results) they should be executed with GET.
59
+
60
+ That should get you started, for now.
14
61
 
15
62
  == Copyright
16
63
 
17
- Copyright (c) 2010 Ernie Miller. See LICENSE for details.
64
+ Copyright (c) 2010 {Ernie Miller}[http://metautonomo.us]. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -3,7 +3,6 @@ module SimpleSearch
3
3
  attr_reader :options, :associations
4
4
 
5
5
  def initialize(model, options = {})
6
- raise StandardError, "#{model.to_s} is not searchable" unless model.metaclass.method_defined?(:search)
7
6
  @model = model
8
7
  @associations = [*options.delete(:search_associations)].compact
9
8
  @association_objects = @associations.map do |a|
@@ -35,6 +34,26 @@ module SimpleSearch
35
34
 
36
35
  conditions.blank? ? [] : [conditions.join(" AND "), *parameters]
37
36
  end
37
+
38
+ def search(args = {})
39
+ args.merge!(
40
+ :conditions => self.conditions,
41
+ :include => (self.associations + [*args[:include]]).compact.uniq
42
+ )
43
+ if @model.respond_to?(:paginate) && args.has_key?(:page)
44
+ @model.paginate(:all, args)
45
+ else
46
+ @model.find(:all, args)
47
+ end
48
+ end
49
+
50
+ def count(args = {})
51
+ args.merge!(
52
+ :conditions => self.conditions,
53
+ :include => (self.associations + [*args[:include]]).compact.uniq
54
+ )
55
+ @model.count(args)
56
+ end
38
57
 
39
58
  private
40
59
 
data/lib/simple_search.rb CHANGED
@@ -1,2 +1 @@
1
- require 'simple_search/search'
2
- require 'simple_search/searchable'
1
+ require 'simple_search/search'
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_search}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ernie Miller"]
@@ -25,7 +25,6 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "lib/simple_search.rb",
27
27
  "lib/simple_search/search.rb",
28
- "lib/simple_search/searchable.rb",
29
28
  "simple_search.gemspec",
30
29
  "test/helper.rb",
31
30
  "test/test_simple_search.rb"
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ernie Miller
@@ -47,7 +47,6 @@ files:
47
47
  - VERSION
48
48
  - lib/simple_search.rb
49
49
  - lib/simple_search/search.rb
50
- - lib/simple_search/searchable.rb
51
50
  - simple_search.gemspec
52
51
  - test/helper.rb
53
52
  - test/test_simple_search.rb
@@ -1,30 +0,0 @@
1
- module SimpleSearch
2
- module Searchable
3
- module ClassMethods
4
- # The "s" parameter is an instance of Search, instantiated from form input.
5
- def search(s, args = {})
6
- args.merge!(
7
- :conditions => s.conditions,
8
- :include => (s.associations + [*args[:include]]).compact.uniq
9
- )
10
- if self.respond_to?(:paginate) && args.has_key?(:page)
11
- self.paginate(:all, args)
12
- else
13
- self.find(:all, args)
14
- end
15
- end
16
-
17
- def search_count(s, args = {})
18
- args.merge!(
19
- :conditions => s.conditions,
20
- :include => (s.associations + [*args[:include]]).compact.uniq
21
- )
22
- self.count(args)
23
- end
24
- end
25
-
26
- def self.included(base)
27
- base.extend(ClassMethods)
28
- end
29
- end
30
- end