scoped-search 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
- .bundle
1
+ .bundle
2
+ pkg
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source :rubygems
2
2
 
3
3
  gem "rspec-rails", ">= 2.0.0.beta.17"
4
4
  gem 'jeweler'
5
- gem "rails", ">= 3.0.0.beta4"
5
+ gem "rails", ">= 3.0.0.rc"
6
6
  gem "ruby-debug"
7
7
 
8
8
  gem 'sqlite3-ruby', :require => 'sqlite3'
@@ -119,6 +119,31 @@ Then in your views, you may use the order_for_scoped_search view helper like thi
119
119
  ...
120
120
  </pre>
121
121
 
122
+ h2. Issue with single Array attribute and multi parameters scopes
123
+
124
+ When you define a scope, you may define it with a single value that accepts an array, or as a multi parameters scope.
125
+ But I haven't find an easy way yet to know the number of parameters a scope accepts (if you know, please tell me!).
126
+ So, for now, if you pass an array to a scoped attribute, it will be passed as an array to the scope.
127
+ If you set attributename_multi_params to true, it will be passed as multiple params to the scope.
128
+
129
+ Example :
130
+
131
+ <pre>
132
+ class Post < ActiveRecord::Base
133
+ scope :retrieve_in_title_and_body, lambda { |a, b| where("title like ? and body like ?", "%#{a}%", "%#{b}") }
134
+ scope :retrieve_ids, lambda { |ids| where(:id => ids) }
135
+ end
136
+
137
+ # For a multi params scope
138
+ #
139
+ > search = Post.scoped_search
140
+ > search.retrieve_in_title_and_body = ["test", "whaaaa"]
141
+ > search.retrieve_in_title_and_body_multi_params = true
142
+
143
+ # For an Array param
144
+ > search.retrieve_ids = [1,2,3]
145
+ </pre>
146
+
122
147
  h2. Notes
123
148
 
124
149
  This plugin is new and under development, patches and contributions are welcome! Please fork and make pull requests, thanks!
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
@@ -18,6 +18,8 @@ class ScopedSearch
18
18
 
19
19
  @attributes_merged.each do |attribute, value|
20
20
  class_eval <<-RUBY
21
+ attr_accessor :#{attribute}_multi_params
22
+
21
23
  def #{attribute}
22
24
  @attributes[:#{attribute}]
23
25
  end
@@ -33,7 +35,13 @@ class ScopedSearch
33
35
  return model_class if attributes.empty?
34
36
  attributes.reject { |k,v| v.blank? || EXCLUDED_SCOPES_VALUES.include?(v.to_s) }.inject(model_class) do |s, k|
35
37
  if model_class.scopes.keys.include?(k.first.to_sym)
36
- k.size == 2 && SINGLE_SCOPES_VALUES.include?(k.last.to_s) ? s.send(k.first) : s.send(*k.flatten)
38
+
39
+ if k.size == 2 && SINGLE_SCOPES_VALUES.include?(k.last.to_s)
40
+ s.send(k.first)
41
+ else
42
+ multi_params?(k.first) ? s.send(*k.flatten) : s.send(k.first, k[1..-1].flatten)
43
+ end
44
+
37
45
  else
38
46
  s
39
47
  end
@@ -45,6 +53,11 @@ class ScopedSearch
45
53
  def method_missing(method_name, *args)
46
54
  build_relation.send(method_name, *args)
47
55
  end
56
+
57
+ protected
58
+ def multi_params?(attribute)
59
+ send("#{attribute}_multi_params").present?
60
+ end
48
61
  end
49
62
 
50
63
  module Helpers
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{scoped-search}
8
+ s.version = "0.5.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["slainer68"]
12
+ s.date = %q{2010-07-28}
13
+ s.description = %q{Easily implement search forms and column ordering based on your models scopes. For Rails 3, compatible with ActiveRecord and Mongoid.}
14
+ s.email = %q{slainer68@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "Gemfile",
22
+ "LICENSE",
23
+ "README.textile",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "init.rb",
27
+ "lib/scoped_search.rb",
28
+ "scoped-search.gemspec",
29
+ "spec/scoped_search_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/novagile/scoped-search}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Easily implement search forms and column ordering based on your models scopes}
37
+ s.test_files = [
38
+ "spec/scoped_search_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
48
+ else
49
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ end
55
+
@@ -4,6 +4,8 @@ require "spec_helper"
4
4
  Post.create(:title => title, :body => "blah", :published => title.include?("pipo"))
5
5
  end
6
6
 
7
+ Post.create(:title => "test multi params", :body => "this is to test multi params, whaaaa")
8
+
7
9
  describe 'ScopedSearch' do
8
10
  it 'should respond to scoped_search and return a ScopedSearch::Base object' do
9
11
  Post.should respond_to(:scoped_search)
@@ -11,10 +13,10 @@ describe 'ScopedSearch' do
11
13
  end
12
14
 
13
15
  it 'should have the same count when initializing an empty search' do
14
- Post.count.should == 6
16
+ Post.count.should == 7
15
17
 
16
18
  @search = Post.scoped_search
17
- @search.count.should == 6
19
+ @search.count.should == 7
18
20
  end
19
21
 
20
22
  describe 'initialization' do
@@ -46,7 +48,7 @@ describe 'ScopedSearch' do
46
48
 
47
49
  @search.all.should have(1).element
48
50
  @search.retrieve = nil
49
- @search.count.should == 6
51
+ @search.count.should == 7
50
52
  end
51
53
 
52
54
  it 'should be possible to search with multiple parameters' do
@@ -60,6 +62,27 @@ describe 'ScopedSearch' do
60
62
  @search.count.should == 2
61
63
  @search.all.should have(2).elements
62
64
  end
65
+
66
+ it 'should be possible to search in a multi params scope' do
67
+ @search = Post.scoped_search
68
+
69
+ @search.retrieve_in_title_and_body = ["test", "whaaaa"]
70
+ @search.retrieve_in_title_and_body_multi_params = true
71
+ @search.count.should == 1
72
+
73
+ @search = Post.scoped_search({ :retrieve_in_title_and_body => ["test", "whaaaa"], :retrieve_in_title_and_body_multi_params => "true" })
74
+ @search.count.should == 1
75
+ end
76
+
77
+ it 'should be possible to search in a scope that wants an Array' do
78
+ @search = Post.scoped_search
79
+
80
+ @search.retrieve_ids = ["3", "4"]
81
+ @search.count.should == 2
82
+
83
+ @search = Post.scoped_search({ :retrieve_ids => ["3", "4"] })
84
+ @search.count.should == 2
85
+ end
63
86
  end
64
87
 
65
88
  it 'should be possible to build the relation' do
@@ -24,8 +24,11 @@ class Post < ActiveRecord::Base
24
24
 
25
25
  scope :published, where(:published => true)
26
26
  scope :retrieve, lambda { |q| where("title like ?", "%#{q}%") }
27
+ scope :retrieve_in_title_and_body, lambda { |a,b| where("title like ? and body like ?", "%#{a}%", "%#{b}") }
28
+ scope :retrieve_ids, lambda { |ids| where(:id => ids) }
27
29
  end
28
30
 
29
31
  RSpec.configure do |config|
30
32
  config.mock_with :rspec
31
33
  end
34
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoped-search
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - slainer68
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-21 00:00:00 +02:00
18
+ date: 2010-07-28 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -52,6 +52,7 @@ files:
52
52
  - VERSION
53
53
  - init.rb
54
54
  - lib/scoped_search.rb
55
+ - scoped-search.gemspec
55
56
  - spec/scoped_search_spec.rb
56
57
  - spec/spec_helper.rb
57
58
  has_rdoc: true