scoped_from 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source 'http://rubygems.org'
1
+ source :rubygems
2
2
 
3
3
  gemspec
data/Gemfile.lock CHANGED
@@ -1,53 +1,53 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scoped_from (0.6.0)
4
+ scoped_from (0.6.1)
5
5
  activerecord (~> 3.2.0)
6
6
  activesupport (~> 3.2.0)
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
- actionpack (3.2.1)
12
- activemodel (= 3.2.1)
13
- activesupport (= 3.2.1)
11
+ actionpack (3.2.3)
12
+ activemodel (= 3.2.3)
13
+ activesupport (= 3.2.3)
14
14
  builder (~> 3.0.0)
15
15
  erubis (~> 2.7.0)
16
16
  journey (~> 1.0.1)
17
17
  rack (~> 1.4.0)
18
- rack-cache (~> 1.1)
18
+ rack-cache (~> 1.2)
19
19
  rack-test (~> 0.6.1)
20
20
  sprockets (~> 2.1.2)
21
- activemodel (3.2.1)
22
- activesupport (= 3.2.1)
21
+ activemodel (3.2.3)
22
+ activesupport (= 3.2.3)
23
23
  builder (~> 3.0.0)
24
- activerecord (3.2.1)
25
- activemodel (= 3.2.1)
26
- activesupport (= 3.2.1)
27
- arel (~> 3.0.0)
24
+ activerecord (3.2.3)
25
+ activemodel (= 3.2.3)
26
+ activesupport (= 3.2.3)
27
+ arel (~> 3.0.2)
28
28
  tzinfo (~> 0.3.29)
29
- activesupport (3.2.1)
29
+ activesupport (3.2.3)
30
30
  i18n (~> 0.6)
31
31
  multi_json (~> 1.0)
32
- arel (3.0.0)
32
+ arel (3.0.2)
33
33
  builder (3.0.0)
34
34
  diff-lcs (1.1.3)
35
35
  erubis (2.7.0)
36
36
  hike (1.2.1)
37
37
  i18n (0.6.0)
38
- journey (1.0.1)
39
- json (1.6.5)
40
- multi_json (1.0.4)
38
+ journey (1.0.3)
39
+ json (1.7.1)
40
+ multi_json (1.3.4)
41
41
  rack (1.4.1)
42
- rack-cache (1.1)
42
+ rack-cache (1.2)
43
43
  rack (>= 0.4)
44
44
  rack-ssl (1.3.2)
45
45
  rack
46
46
  rack-test (0.6.1)
47
47
  rack (>= 1.0)
48
- railties (3.2.1)
49
- actionpack (= 3.2.1)
50
- activesupport (= 3.2.1)
48
+ railties (3.2.3)
49
+ actionpack (= 3.2.3)
50
+ activesupport (= 3.2.3)
51
51
  rack-ssl (~> 1.3.2)
52
52
  rake (>= 0.8.7)
53
53
  rdoc (~> 3.4)
@@ -55,35 +55,35 @@ GEM
55
55
  rake (0.9.2.2)
56
56
  rdoc (3.12)
57
57
  json (~> 1.4)
58
- rspec (2.8.0)
59
- rspec-core (~> 2.8.0)
60
- rspec-expectations (~> 2.8.0)
61
- rspec-mocks (~> 2.8.0)
62
- rspec-core (2.8.0)
63
- rspec-expectations (2.8.0)
64
- diff-lcs (~> 1.1.2)
65
- rspec-mocks (2.8.0)
66
- rspec-rails (2.8.1)
58
+ rspec (2.10.0)
59
+ rspec-core (~> 2.10.0)
60
+ rspec-expectations (~> 2.10.0)
61
+ rspec-mocks (~> 2.10.0)
62
+ rspec-core (2.10.0)
63
+ rspec-expectations (2.10.0)
64
+ diff-lcs (~> 1.1.3)
65
+ rspec-mocks (2.10.1)
66
+ rspec-rails (2.10.1)
67
67
  actionpack (>= 3.0)
68
68
  activesupport (>= 3.0)
69
69
  railties (>= 3.0)
70
- rspec (~> 2.8.0)
71
- sprockets (2.1.2)
70
+ rspec (~> 2.10.0)
71
+ sprockets (2.1.3)
72
72
  hike (~> 1.2)
73
73
  rack (~> 1.0)
74
74
  tilt (~> 1.1, != 1.3.0)
75
- sqlite3 (1.3.5)
75
+ sqlite3 (1.3.6)
76
76
  sqlite3-ruby (1.3.3)
77
77
  sqlite3 (>= 1.3.3)
78
78
  thor (0.14.6)
79
79
  tilt (1.3.3)
80
- tzinfo (0.3.31)
80
+ tzinfo (0.3.33)
81
81
 
82
82
  PLATFORMS
83
83
  ruby
84
84
 
85
85
  DEPENDENCIES
86
- rspec (~> 2.8.0)
87
- rspec-rails (~> 2.8.0)
86
+ rspec (~> 2.10.0)
87
+ rspec-rails (~> 2.10.0)
88
88
  scoped_from!
89
89
  sqlite3-ruby
data/README.mdown CHANGED
@@ -5,20 +5,12 @@ Provides a simple mapping between scopes and controller parameters for
5
5
 
6
6
  ## Installation
7
7
 
8
- ### As a gem
9
-
10
8
  Just add this into your `Gemfile`:
11
9
 
12
10
  gem 'scoped_from'
13
11
 
14
12
  Then, just run `bundle install`.
15
13
 
16
- ### As a plugin
17
-
18
- From your application path, execute:
19
-
20
- rails plugin install git://github.com/alexistoulotte/scoped_from.git
21
-
22
14
  ## Example
23
15
 
24
16
  First, a model with some scopes:
@@ -146,4 +138,4 @@ Then into a view:
146
138
  ## Executing test suite
147
139
 
148
140
  This project is fully tested with [Rspec 2](http://github.com/rspec/rspec).
149
- Just run `rake` (after a `bundle install`).
141
+ Just run `bundle exec rake` (after a `bundle install`).
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.6.1
@@ -1,37 +1,37 @@
1
1
  module ScopedFrom
2
2
 
3
3
  module ActiveRecord
4
-
4
+
5
5
  extend ActiveSupport::Concern
6
-
6
+
7
7
  included do |base|
8
8
  base.class_attribute(:scope_arities)
9
9
  base.scope_arities = ActiveSupport::HashWithIndifferentAccess.new
10
10
  end
11
-
11
+
12
12
  module ClassMethods
13
-
13
+
14
14
  def scope(name, scope_options, &block)
15
15
  super
16
16
  scope_arities[name] = scope_options.is_a?(Proc) ? scope_options.arity : -1
17
17
  end
18
-
18
+
19
19
  def scope_with_one_argument?(name)
20
20
  scope_arities[name] == 1
21
21
  end
22
-
22
+
23
23
  def scope_without_argument?(name)
24
24
  [-1, 0].include?(scope_arities[name])
25
25
  end
26
-
26
+
27
27
  def scoped_from(params, options = {})
28
- query_class = "#{name}Query".constantize rescue nil
28
+ query_class = "#{name}Query".safe_constantize
29
29
  query_class = Query unless query_class.is_a?(Class) && query_class.ancestors.include?(Query)
30
30
  query_class.new(self, params, options).scope
31
31
  end
32
-
32
+
33
33
  end
34
-
34
+
35
35
  end
36
-
36
+
37
37
  end
@@ -1,13 +1,13 @@
1
1
  module ScopedFrom
2
-
2
+
3
3
  class Query
4
4
 
5
5
  FALSE_VALUES = %w( false no n off 0 ).freeze
6
6
  ORDER_DIRECTIONS = %w( asc desc ).freeze
7
7
  TRUE_VALUES = %w( true yes y on 1 ).freeze
8
-
8
+
9
9
  attr_reader :params
10
-
10
+
11
11
  # Available options are: - :only : to restrict to specified keys.
12
12
  # - :except : to ignore specified keys.
13
13
  def initialize(scope, params, options = {})
@@ -15,15 +15,15 @@ module ScopedFrom
15
15
  @options = options
16
16
  self.params = params
17
17
  end
18
-
18
+
19
19
  def order_column
20
20
  parse_order(params['order'])[:column]
21
21
  end
22
-
22
+
23
23
  def order_direction
24
24
  parse_order(params['order'])[:direction]
25
25
  end
26
-
26
+
27
27
  def scope
28
28
  scope = @scope
29
29
  params.each do |name, value|
@@ -33,9 +33,9 @@ module ScopedFrom
33
33
  end
34
34
  decorate_scope(scope)
35
35
  end
36
-
36
+
37
37
  protected
38
-
38
+
39
39
  def decorate_scope(scope)
40
40
  return scope if scope.respond_to?(:query)
41
41
  def scope.query
@@ -44,16 +44,16 @@ module ScopedFrom
44
44
  scope.instance_variable_set('@__query', self)
45
45
  scope
46
46
  end
47
-
47
+
48
48
  def false?(value)
49
49
  FALSE_VALUES.include?(value.to_s.strip.downcase)
50
50
  end
51
-
51
+
52
52
  def order_to_sql(value)
53
53
  order = parse_order(value)
54
54
  "#{order[:column]} #{order[:direction].upcase}" if order.present?
55
55
  end
56
-
56
+
57
57
  def params=(params)
58
58
  params = params.params if params.is_a?(self.class)
59
59
  params = CGI.parse(params.to_s) unless params.is_a?(Hash)
@@ -82,14 +82,14 @@ module ScopedFrom
82
82
  @params.slice!(*[@options[:only]].flatten) if @options[:only].present?
83
83
  @params.except!(*[@options[:except]].flatten) if @options[:except].present?
84
84
  end
85
-
85
+
86
86
  def parse_order(value)
87
87
  column, direction = value.to_s.split(/[\.:\s]+/, 2)
88
88
  direction = direction.to_s.downcase
89
89
  direction = ORDER_DIRECTIONS.first unless ORDER_DIRECTIONS.include?(direction)
90
90
  @scope.column_names.include?(column) ? { :column => column, :direction => direction } : {}
91
91
  end
92
-
92
+
93
93
  def parse_orders(values)
94
94
  [].tap do |orders|
95
95
  values.each do |value|
@@ -98,7 +98,7 @@ module ScopedFrom
98
98
  end
99
99
  end
100
100
  end
101
-
101
+
102
102
  def scoped(scope, name, value)
103
103
  if name.to_s == 'order'
104
104
  scope.order(order_to_sql(value))
@@ -112,11 +112,11 @@ module ScopedFrom
112
112
  scope
113
113
  end
114
114
  end
115
-
115
+
116
116
  def true?(value)
117
117
  TRUE_VALUES.include?(value.to_s.strip.downcase)
118
118
  end
119
-
119
+
120
120
  end
121
-
121
+
122
122
  end
data/lib/scoped_from.rb CHANGED
@@ -6,15 +6,15 @@ require 'active_support/concern'
6
6
  require 'active_support/core_ext/object/to_query'
7
7
 
8
8
  module ScopedFrom
9
-
9
+
10
10
  class << self
11
-
11
+
12
12
  def version
13
13
  @@version ||= File.read(File.expand_path(File.dirname(__FILE__) + '/../VERSION')).strip.freeze
14
14
  end
15
-
15
+
16
16
  end
17
-
17
+
18
18
  end
19
19
 
20
20
  lib_path = File.expand_path(File.dirname(__FILE__) + '/scoped_from')
data/scoped_from.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.add_dependency 'activerecord', '~> 3.2.0'
19
19
  s.add_dependency 'activesupport', '~> 3.2.0'
20
20
 
21
- s.add_development_dependency 'rspec', '~> 2.8.0'
22
- s.add_development_dependency 'rspec-rails', '~> 2.8.0'
21
+ s.add_development_dependency 'rspec', '~> 2.10.0'
22
+ s.add_development_dependency 'rspec-rails', '~> 2.10.0'
23
23
  s.add_development_dependency 'sqlite3-ruby'
24
24
  end
data/spec/mocks/user.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class User < ActiveRecord::Base
2
-
2
+
3
3
  scope :enabled, where(:enabled => true)
4
4
  scope :search, lambda { |pattern|
5
5
  where('firstname LIKE ? OR lastname LIKE ?', "%#{pattern}%", "%#{pattern}%")
@@ -10,5 +10,5 @@ class User < ActiveRecord::Base
10
10
  scope :latest, lambda {
11
11
  where('created_at >= ?', 1.week.ago)
12
12
  }
13
-
13
+
14
14
  end
@@ -8,86 +8,86 @@ describe ScopedFrom::ActiveRecord do
8
8
  User.should be_scope_with_one_argument(:search)
9
9
  User.should be_scope_with_one_argument('search')
10
10
  end
11
-
11
+
12
12
  it 'is false if scope has no argument' do
13
13
  User.should_not be_scope_with_one_argument(:latest)
14
14
  User.should_not be_scope_with_one_argument('latest')
15
15
  end
16
-
16
+
17
17
  it 'is false if scope has more than one argument' do
18
18
  User.should_not be_scope_with_one_argument(:created_between)
19
19
  User.should_not be_scope_with_one_argument('created_between')
20
20
  end
21
-
21
+
22
22
  it 'is false if scope is not a proc' do
23
23
  User.should_not be_scope_with_one_argument(:enabled)
24
24
  User.should_not be_scope_with_one_argument('enabled')
25
25
  end
26
-
26
+
27
27
  it 'is false if scope does not exist' do
28
28
  User.should_not be_scope_with_one_argument(:foo)
29
29
  User.should_not be_scope_with_one_argument('foo')
30
30
  end
31
-
31
+
32
32
  end
33
-
33
+
34
34
  describe 'scope_without_argument?' do
35
-
35
+
36
36
  it 'is true if scope has no argument' do
37
37
  User.should be_scope_without_argument(:latest)
38
38
  User.should be_scope_without_argument('latest')
39
39
  end
40
-
40
+
41
41
  it 'is true if scope is not a proc' do
42
42
  User.should be_scope_without_argument(:enabled)
43
43
  User.should be_scope_without_argument('enabled')
44
44
  end
45
-
45
+
46
46
  it 'is false if scope has one argument' do
47
47
  User.should_not be_scope_without_argument(:search)
48
48
  User.should_not be_scope_without_argument('search')
49
49
  end
50
-
50
+
51
51
  it 'is false if scope has more than one argument' do
52
52
  User.should_not be_scope_without_argument(:created_between)
53
53
  User.should_not be_scope_without_argument('created_between')
54
54
  end
55
-
55
+
56
56
  it 'is false if scope does not exist' do
57
57
  User.should_not be_scope_without_argument(:foo)
58
58
  User.should_not be_scope_without_argument('foo')
59
59
  end
60
-
60
+
61
61
  end
62
-
62
+
63
63
  describe '#scoped_from' do
64
-
64
+
65
65
  it 'just build a new query and return its scope' do
66
66
  query = mock(:query)
67
67
  query.should_receive(:scope).and_return(42)
68
68
  ScopedFrom::Query.should_receive(:new).with(User, 'foo', :except => 'bam').and_return(query)
69
69
  User.scoped_from('foo', :except => 'bam').should == 42
70
70
  end
71
-
71
+
72
72
  it 'build scopes' do
73
73
  User.scoped_from(:search => 'jane').should == [users(:jane)]
74
74
  User.scoped_from(:search => 'john').should == [users(:john)]
75
75
  end
76
-
76
+
77
77
  it 'can be chained with other scopes' do
78
78
  User.scoped_from(:search => 'jane').should == [users(:jane)]
79
79
  User.enabled.scoped_from(:search => 'jane').should == []
80
80
  end
81
-
81
+
82
82
  it 'can be used with order as parameter' do
83
83
  User.scoped_from(:order => 'firstname').first.should == users(:jane)
84
84
  User.scoped_from(:order => 'firstname.desc').first.should == users(:john)
85
85
  end
86
-
86
+
87
87
  it 'builds a ScopedFrom::Query' do
88
88
  User.scoped_from({}).query.class.should be(ScopedFrom::Query)
89
89
  end
90
-
90
+
91
91
  it 'builds a ScopedFrom::Query if #{RecordClassName}Query is not defined' do
92
92
  Post.scoped_from({}).query.class.should be(ScopedFrom::Query)
93
93
  Object.const_defined?('PostQuery').should be_false
@@ -95,27 +95,27 @@ describe ScopedFrom::ActiveRecord do
95
95
  PostQuery
96
96
  }.to raise_error(NameError, 'uninitialized constant PostQuery')
97
97
  end
98
-
98
+
99
99
  it 'builds a #{Class}Query if #{RecordClassName}Query is defined and is a ScopedFrom::Query' do
100
100
  Comment.scoped_from({}).query.class.should be(CommentQuery)
101
101
  Comment.where(:foo => 'bar').scoped_from({}).query.class.should be(CommentQuery)
102
102
  CommentQuery.should be_a(Class)
103
103
  CommentQuery.ancestors.should include(ScopedFrom::Query)
104
104
  end
105
-
105
+
106
106
  it 'builds a ScopedFrom::Query if #{RecordClassName}Query is defined but not a subclass of ScopedFrom::Query' do
107
107
  User.scoped_from({}).query.class.should be(ScopedFrom::Query)
108
108
  Object.const_defined?('UserQuery').should be_true
109
109
  UserQuery.should be_a(Class)
110
110
  UserQuery.ancestors.should_not include(ScopedFrom::Query)
111
111
  end
112
-
112
+
113
113
  it 'builds a ScopedFrom::Query if #{RecordClassName}Query is defined but is a module' do
114
114
  Vote.scoped_from({}).query.class.should be(ScopedFrom::Query)
115
115
  Object.const_defined?('VoteQuery').should be_true
116
116
  VoteQuery.should be_a(Module)
117
117
  end
118
-
118
+
119
119
  end
120
-
120
+
121
121
  end
@@ -1,188 +1,188 @@
1
- # encoding: UTF-8
1
+ # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
5
  describe ScopedFrom::Query do
6
-
6
+
7
7
  def query(scope = User, params = {}, options = {})
8
8
  ScopedFrom::Query.new(scope, params, options)
9
9
  end
10
-
10
+
11
11
  describe '#false?' do
12
-
12
+
13
13
  it 'is true if false is given' do
14
14
  query.send(:false?, false).should be_true
15
15
  end
16
-
16
+
17
17
  it 'is true if "false" is given' do
18
18
  query.send(:false?, 'false').should be_true
19
19
  query.send(:false?, 'False').should be_true
20
20
  end
21
-
21
+
22
22
  it 'is true if "0" is given' do
23
23
  query.send(:false?, '0').should be_true
24
24
  end
25
-
25
+
26
26
  it 'is true if "off" is given' do
27
27
  query.send(:false?, 'off').should be_true
28
28
  query.send(:false?, 'OFF ').should be_true
29
29
  end
30
-
30
+
31
31
  it 'is true if "no" is given' do
32
32
  query.send(:false?, 'no').should be_true
33
33
  query.send(:false?, ' No ').should be_true
34
34
  end
35
-
35
+
36
36
  it 'is true if "n" is given' do
37
37
  query.send(:false?, 'n').should be_true
38
38
  query.send(:false?, 'N ').should be_true
39
39
  end
40
-
40
+
41
41
  it 'is false if true is given' do
42
42
  query.send(:false?, true).should be_false
43
43
  end
44
-
44
+
45
45
  it 'is false if "true" is given' do
46
46
  query.send(:false?, 'true').should be_false
47
47
  query.send(:false?, 'TrUe').should be_false
48
48
  end
49
-
49
+
50
50
  it 'is false if "1" is given' do
51
51
  query.send(:false?, '1').should be_false
52
52
  end
53
-
53
+
54
54
  it 'is false if "on" is given' do
55
55
  query.send(:false?, "on").should be_false
56
56
  query.send(:false?, "On").should be_false
57
57
  end
58
-
58
+
59
59
  it 'is false otherwise' do
60
60
  query.send(:false?, 42).should be_false
61
61
  query.send(:false?, 'bam').should be_false
62
62
  end
63
-
63
+
64
64
  end
65
-
65
+
66
66
  describe '#initialize' do
67
-
67
+
68
68
  it 'invokes #scoped method on specified scope' do
69
69
  User.should_receive(:scoped)
70
70
  ScopedFrom::Query.new(User, {})
71
71
  end
72
-
72
+
73
73
  end
74
-
74
+
75
75
  describe '#order_column' do
76
-
76
+
77
77
  it 'is column specified into "order" parameter' do
78
78
  query(User, :order => 'firstname').order_column.should == 'firstname'
79
79
  query(User, :order => 'lastname.desc').order_column.should == 'lastname'
80
80
  end
81
-
81
+
82
82
  it 'is nil if column does not exist' do
83
83
  query(User, :order => 'foo').order_column.should be_nil
84
84
  end
85
-
85
+
86
86
  it 'is nil if "order" param is not specified' do
87
87
  query(User, :search => 'foo').order_column.should be_nil
88
88
  end
89
-
89
+
90
90
  end
91
-
91
+
92
92
  describe '#order_direction' do
93
-
93
+
94
94
  it 'is direction specified into "order" parameter' do
95
95
  query(User, :order => 'firstname.asc').order_direction.should == 'asc'
96
96
  query(User, :order => 'firstname.desc').order_direction.should == 'desc'
97
97
  end
98
-
98
+
99
99
  it 'is "asc" if direction is not specified' do
100
100
  query(User, :order => 'firstname').order_direction.should == 'asc'
101
101
  end
102
-
102
+
103
103
  it 'is "asc" if direction is invalid' do
104
104
  query(User, :order => 'firstname.foo').order_direction.should == 'asc'
105
105
  end
106
-
106
+
107
107
  it 'is direction even specified in another case' do
108
108
  query(User, :order => 'firstname.ASc').order_direction.should == 'asc'
109
109
  query(User, :order => 'firstname.DeSC').order_direction.should == 'desc'
110
110
  end
111
-
111
+
112
112
  it 'is nil if column does not exist' do
113
113
  query(User, :order => 'foo.desc').order_direction.should be_nil
114
114
  end
115
-
115
+
116
116
  it 'is nil if "order" param is not specified' do
117
117
  query(User, :search => 'foo').order_direction.should be_nil
118
118
  end
119
-
119
+
120
120
  end
121
-
121
+
122
122
  describe '#params' do
123
-
123
+
124
124
  it 'returns params specified at initialization' do
125
125
  query(User, :search => 'foo', 'enabled' => true).params.should == { 'search' => 'foo', 'enabled' => true }
126
126
  end
127
-
127
+
128
128
  it 'returns an hash with indifferent access' do
129
129
  query(User, 'search' => 'bar').params.should be_a(ActiveSupport::HashWithIndifferentAccess)
130
130
  query(User, 'search' => 'bar').params[:search].should == 'bar'
131
131
  query(User, :search => 'bar').params['search'].should == 'bar'
132
132
  end
133
-
133
+
134
134
  it 'can be converted to query string' do
135
135
  query(User, :search => ['foo', 'bar'], 'enabled' => '1').params.to_query.should == 'enabled=true&search%5B%5D=foo&search%5B%5D=bar'
136
136
  end
137
-
137
+
138
138
  end
139
-
139
+
140
140
  describe '#params=' do
141
-
141
+
142
142
  it 'does not fails if nil is given' do
143
143
  query(User, nil).params.should == {}
144
144
  end
145
-
145
+
146
146
  it 'removes values that are not scopes' do
147
147
  query(User, :foo => 'bar', 'search' => 'foo', :enabled => true).params.should == { 'search' => 'foo', 'enabled' => true }
148
148
  end
149
-
149
+
150
150
  it 'is case sensitive' do
151
151
  query(User, 'Enabled' => true, "SEARCH" => 'bar').params.should be_empty
152
152
  end
153
-
153
+
154
154
  it 'parse query string' do
155
155
  query(User, 'search=foo%26baz&latest=true').params.should == { 'search' => 'foo&baz', 'latest' => true }
156
156
  end
157
-
157
+
158
158
  it 'removes blank values from query string' do
159
159
  query(User, 'search=baz&toto=&bar=%20').params.should == { 'search' => 'baz' }
160
160
  end
161
-
161
+
162
162
  it 'unescapes UTF-8 chars' do
163
163
  query(User, 'search=%C3%A9').params.should == { 'search' => 'é' }
164
164
  end
165
-
165
+
166
166
  it 'can have multiple values (from hash)' do
167
167
  query(User, :search => ['bar', 'baz']).params.should == { 'search' => ['bar', 'baz'] }
168
168
  end
169
-
169
+
170
170
  it 'can have multiple values (from query string)' do
171
171
  query(User, 'search=bar&search=baz').params.should == { 'search' => ['bar', 'baz'] }
172
172
  end
173
-
173
+
174
174
  it 'converts value to true (or remove it) if scope takes no argument' do
175
175
  query(User, :latest => 'y').params.should == { 'latest' => true }
176
176
  query(User, :latest => 'no').params.should == {}
177
177
  end
178
-
178
+
179
179
  it 'converts value to true (or false) if column is a boolean one' do
180
180
  query(User, :admin => 'y').params.should == { 'admin' => true }
181
181
  query(User, :admin => 'False').params.should == { 'admin' => false }
182
182
  query(User, :admin => 'bar').params.should == {}
183
183
  query(User, :admin => ['y', false]).params.should == {}
184
184
  end
185
-
185
+
186
186
  it 'converts array value to true (or remove it) if scope takes no argument' do
187
187
  query(User, :latest => true).params.should == { 'latest' => true }
188
188
  query(User, :latest => ['Yes']).params.should == { 'latest' => true }
@@ -190,144 +190,144 @@ describe ScopedFrom::Query do
190
190
  query(User, :latest => ['no', nil]).params.should == {}
191
191
  query(User, :latest => ['fo']).params.should == {}
192
192
  end
193
-
193
+
194
194
  it 'flats array' do
195
195
  query(User, :search => [nil, ['bar', '', 'foo', ["\n ", 'baz']]]).params.should == { 'search' => [nil, 'bar', '', 'foo', "\n ", 'baz'] }
196
196
  end
197
-
197
+
198
198
  it 'change array with a single value in one value' do
199
199
  query(User, :search => ['bar']).params.should == { 'search' => 'bar' }
200
200
  end
201
-
201
+
202
202
  it 'does not modify given hash' do
203
203
  hash = { :search => 'foo', :enabled => '1', :bar => 'foo' }
204
204
  query(User, hash)
205
205
  hash.should == { :search => 'foo', :enabled => '1', :bar => 'foo' }
206
206
  end
207
-
207
+
208
208
  it 'does not modify given array' do
209
209
  items = ['bar', 'foo', nil]
210
210
  query(User, :search => items)
211
211
  items.should == ['bar', 'foo', nil]
212
212
  end
213
-
213
+
214
214
  it 'accepts :only option' do
215
215
  query(User, { :search => 'bar', :enabled => 'true' }, :only => [:search]).params.should == { 'search' => 'bar' }
216
216
  query(User, { :search => 'bar', :enabled => 'true' }, :only => 'search').params.should == { 'search' => 'bar' }
217
217
  query(User, { :search => 'bar', :firstname => 'Jane', :enabled => 'true' }, :only => 'search').params.should == { 'search' => 'bar' }
218
218
  query(User, { :search => 'bar', :firstname => 'Jane', :enabled => 'true' }, :only => ['search', :firstname]).params.should == { 'search' => 'bar', 'firstname' => 'Jane' }
219
219
  end
220
-
220
+
221
221
  it 'accepts :except option' do
222
222
  query(User, { :search => 'bar', :enabled => true }, :except => [:search]).params.should == { 'enabled' => true }
223
223
  query(User, { :search => 'bar', :enabled => true }, :except => 'search').params.should == { 'enabled' => true }
224
224
  query(User, { :search => 'bar', :firstname => 'Jane', :enabled => true }, :except => 'search').params.should == { 'enabled' => true, 'firstname' => 'Jane' }
225
225
  query(User, { :search => 'bar', :firstname => 'Jane', :enabled => true }, :except => ['search', :firstname]).params.should == { 'enabled' => true }
226
226
  end
227
-
227
+
228
228
  it 'accepts a query instance' do
229
229
  query(User, query(User, :search => 'toto')).params.should == { 'search' => 'toto' }
230
230
  end
231
-
231
+
232
232
  it 'preserve blank values' do
233
233
  query(User, { :search => "\n ", 'enabled' => true }).params.should == { 'search' => "\n ", 'enabled' => true }
234
234
  end
235
-
235
+
236
236
  it 'preserve blank values from array' do
237
237
  query(User, { 'search' => ["\n ", 'toto', 'titi'] }).params.should == { 'search' => ["\n ", 'toto', 'titi'] }
238
238
  query(User, { 'search' => [] }).params.should == {}
239
239
  end
240
-
240
+
241
241
  it 'also preserve blank on query string' do
242
242
  query(User, 'search=%20&enabled=true&search=foo').params.should == { 'search' => [' ', 'foo'], 'enabled' => true }
243
243
  end
244
-
244
+
245
245
  it 'includes column values' do
246
246
  query(User, 'firstname' => 'Jane', 'foo' => 'bar').params.should == { 'firstname' => 'Jane' }
247
247
  query(User, :firstname => 'Jane', 'foo' => 'bar').params.should == { 'firstname' => 'Jane' }
248
248
  end
249
-
249
+
250
250
  it 'exclude column values if :exclude_columns option is specified' do
251
251
  query(User, { :enabled => true, 'firstname' => 'Jane', 'foo' => 'bar' }, :exclude_columns => true).params.should == { 'enabled' => true }
252
252
  query(User, { :enabled => true, :firstname => 'Jane', :foo => 'bar' }, :exclude_columns => true).params.should == { 'enabled' => true }
253
253
  end
254
-
254
+
255
255
  it 'scopes have priority on columns' do
256
256
  query(User, :enabled => false).params.should == {}
257
257
  end
258
-
258
+
259
259
  it 'maps an "order"' do
260
260
  query(User, { 'order' => 'firstname.asc' }).params.should == { 'order' => 'firstname.asc' }
261
261
  end
262
-
262
+
263
263
  it 'does not map "order" if column is invalid' do
264
264
  query(User, { 'order' => 'foo.asc' }).params.should == {}
265
265
  end
266
-
266
+
267
267
  it 'use "asc" order direction by default' do
268
268
  query(User, { 'order' => 'firstname' }).params.should == { 'order' => 'firstname.asc' }
269
269
  end
270
-
270
+
271
271
  it 'use "asc" order direction if invalid' do
272
272
  query(User, { 'order' => 'firstname.bar' }).params.should == { 'order' => 'firstname.asc' }
273
273
  end
274
-
274
+
275
275
  it 'use "desc" order direction if specified' do
276
276
  query(User, { 'order' => 'firstname.desc' }).params.should == { 'order' => 'firstname.desc' }
277
277
  end
278
-
278
+
279
279
  it 'order direction is case insensitive' do
280
280
  query(User, { 'order' => 'firstname.Asc' }).params.should == { 'order' => 'firstname.asc' }
281
281
  query(User, { 'order' => 'firstname.DESC' }).params.should == { 'order' => 'firstname.desc' }
282
282
  end
283
-
283
+
284
284
  it 'order can be specified as symbol' do
285
285
  query(User, { :order => 'firstname.desc' }).params.should == { 'order' => 'firstname.desc' }
286
286
  end
287
-
287
+
288
288
  it "order is case sensitive" do
289
289
  query(User, { 'Order' => 'firstname.desc' }).params.should == {}
290
290
  end
291
-
291
+
292
292
  it 'many order can be specified' do
293
293
  query(User, { 'order' => ['firstname.Asc', 'lastname.DESC'] }).params.should == { 'order' => ['firstname.asc', 'lastname.desc'] }
294
294
  query(User, { 'order' => ['firstname.Asc', 'firstname.desc'] }).params.should == { 'order' => 'firstname.asc' }
295
295
  query(User, { 'order' => ['firstname.Asc', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.asc', 'lastname.desc'] }
296
296
  query(User, { 'order' => ['firstname.Asc', 'foo', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.asc', 'lastname.desc'] }
297
297
  end
298
-
298
+
299
299
  it 'order can be delimited by a space' do
300
300
  query(User, { 'order' => 'firstname ASC' }).params.should == { 'order' => 'firstname.asc' }
301
301
  end
302
-
302
+
303
303
  it 'order can be delimited by any white space' do
304
304
  query(User, { 'order' => "firstname\nASC" }).params.should == { 'order' => 'firstname.asc' }
305
305
  query(User, { 'order' => "firstname\t ASC" }).params.should == { 'order' => 'firstname.asc' }
306
306
  end
307
-
307
+
308
308
  it 'order can be delimited by a ":"' do
309
309
  query(User, { 'order' => "firstname:ASC" }).params.should == { 'order' => 'firstname.asc' }
310
310
  end
311
-
311
+
312
312
  it 'order can be delimited by more than one delimiter' do
313
313
  query(User, { 'order' => "firstname :. ASC" }).params.should == { 'order' => 'firstname.asc' }
314
314
  end
315
-
315
+
316
316
  end
317
-
317
+
318
318
  describe '#scope' do
319
-
319
+
320
320
  it 'does not execute any query' do
321
321
  User.should_not_receive(:connection)
322
322
  query(User, :enabled => true).scope
323
323
  end
324
-
324
+
325
325
  it 'works with scopes with a lambda without arguments' do
326
326
  users(:jane).update_attribute(:created_at, 10.days.ago)
327
327
  query(User, :latest => true).scope.should == [users(:john)]
328
328
  query(User, :latest => false).scope.should == [users(:john), users(:jane)]
329
329
  end
330
-
330
+
331
331
  it 'does not modify scope specified at initialization' do
332
332
  scope = User.search('foo')
333
333
  q = query(scope, :enabled => true)
@@ -337,138 +337,138 @@ describe ScopedFrom::Query do
337
337
  }.to_not change { q.instance_variable_get('@scope') }
338
338
  }.to_not change { scope }
339
339
  end
340
-
340
+
341
341
  it 'returns scope (#scoped) specified at construction if params are empty' do
342
342
  query.scope.should_not == User
343
343
  query.scope.should == User.scoped
344
344
  end
345
-
345
+
346
346
  it 'invokes many times scope if an array is given' do
347
347
  query(User, :search => ['John', 'Doe']).scope.should == [users(:john)]
348
348
  query(User, :search => ['John', 'Done']).scope.should == []
349
349
  query(User, :search => ['John', 'Doe']).params.should == { 'search' => ['John', 'Doe'] }
350
350
  end
351
-
351
+
352
352
  it 'invokes many times scope if given twice (as string & symbol)' do
353
353
  query(User, :search => 'John', 'search' => 'Done').params['search'].size.should be(2)
354
354
  query(User, :search => 'John', 'search' => 'Done').params['search'].should include('John', 'Done')
355
-
356
-
355
+
356
+
357
357
  query(User, :search => 'John', 'search' => ['Did', 'Done']).params['search'].size.should be(3)
358
358
  query(User, :search => 'John', 'search' => ['Did', 'Done']).params['search'].should include('John', 'Did', 'Done')
359
359
  end
360
-
360
+
361
361
  it 'invokes last order if an array is given' do
362
362
  query(User, :order => ['lastname', 'firstname']).scope.should == [users(:jane), users(:john)]
363
363
  query(User, :order => ['lastname', 'firstname.desc']).scope.should == [users(:john), users(:jane)]
364
364
  query(User, :order => ['firstname.desc', 'lastname']).scope.order_values.should == ['firstname DESC', 'lastname ASC']
365
365
  end
366
-
366
+
367
367
  it 'defines #query method on returned scoped' do
368
368
  query(User).scope.should respond_to(:query)
369
369
  end
370
-
370
+
371
371
  it 'does not define #query method for future scopes' do
372
372
  query(User).scope.query.should be_present
373
373
  User.should_not respond_to(:query)
374
374
  User.scoped.should_not respond_to(:query)
375
375
  User.enabled.should_not respond_to(:query)
376
376
  end
377
-
377
+
378
378
  it 'defined #query method returns query' do
379
379
  q = query(User)
380
380
  q.scope.query.should be_a(ScopedFrom::Query)
381
381
  q.scope.query.should be(q)
382
382
  end
383
-
383
+
384
384
  end
385
-
385
+
386
386
  describe '#scoped' do
387
-
387
+
388
388
  it 'returns given scope if it has no scope with specified name' do
389
389
  query.send(:scoped, User, :foo, true).should == User
390
390
  end
391
-
391
+
392
392
  it 'returns given scope if scope takes more than 1 argument' do
393
393
  query.send(:scoped, User, :created_between, true).should == User
394
394
  end
395
-
395
+
396
396
  it 'invokes scope without arguments if scope takes no arguments' do
397
397
  query.send(:scoped, User.scoped, :enabled, true).should == [users(:john)]
398
398
  query.send(:scoped, User.scoped, :enabled, ' 1 ').should == [users(:john)]
399
399
  query.send(:scoped, User.scoped, :enabled, 'off').should == [users(:john)]
400
400
  end
401
-
401
+
402
402
  it 'invokes scope with value has argument if scope takes one argument' do
403
403
  query.send(:scoped, User.scoped, :search, 'doe').should == [users(:john), users(:jane)]
404
404
  query.send(:scoped, User.scoped, :search, 'john').should == [users(:john)]
405
405
  query.send(:scoped, User.scoped, :search, 'jane').should == [users(:jane)]
406
406
  end
407
-
407
+
408
408
  it 'scope on column conditions' do
409
409
  query.send(:scoped, User.scoped, :firstname, 'Jane').should == [users(:jane)]
410
410
  end
411
-
411
+
412
412
  it 'invokes "order"' do
413
413
  query.send(:scoped, User.scoped, :order, 'firstname.asc').should == [users(:jane), users(:john)]
414
414
  query.send(:scoped, User.scoped, :order, 'firstname.desc').should == [users(:john), users(:jane)]
415
415
  end
416
-
416
+
417
417
  end
418
-
418
+
419
419
  describe '#true?' do
420
-
420
+
421
421
  it 'is true if true is given' do
422
422
  query.send(:true?, true).should be_true
423
423
  end
424
-
424
+
425
425
  it 'is true if "true" is given' do
426
426
  query.send(:true?, 'true').should be_true
427
427
  query.send(:true?, 'True').should be_true
428
428
  end
429
-
429
+
430
430
  it 'is true if "1" is given' do
431
431
  query.send(:true?, '1').should be_true
432
432
  end
433
-
433
+
434
434
  it 'is true if "on" is given' do
435
435
  query.send(:true?, 'on').should be_true
436
436
  query.send(:true?, 'ON ').should be_true
437
437
  end
438
-
438
+
439
439
  it 'is true if "yes" is given' do
440
440
  query.send(:true?, 'yes').should be_true
441
441
  query.send(:true?, ' Yes ').should be_true
442
442
  end
443
-
443
+
444
444
  it 'is true if "y" is given' do
445
445
  query.send(:true?, 'y').should be_true
446
446
  query.send(:true?, 'Y ').should be_true
447
447
  end
448
-
448
+
449
449
  it 'is false if false is given' do
450
450
  query.send(:true?, false).should be_false
451
451
  end
452
-
452
+
453
453
  it 'is false if "false" is given' do
454
454
  query.send(:true?, 'false').should be_false
455
455
  query.send(:true?, 'FsALSE').should be_false
456
456
  end
457
-
457
+
458
458
  it 'is false if "0" is given' do
459
459
  query.send(:true?, '0').should be_false
460
460
  end
461
-
461
+
462
462
  it 'is false if "off" is given' do
463
463
  query.send(:true?, "off").should be_false
464
464
  query.send(:true?, "Off").should be_false
465
465
  end
466
-
466
+
467
467
  it 'is false otherwise' do
468
468
  query.send(:true?, 42).should be_false
469
469
  query.send(:true?, 'bam').should be_false
470
470
  end
471
-
471
+
472
472
  end
473
-
473
+
474
474
  end
@@ -1,23 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ScopedFrom do
4
-
4
+
5
5
  describe '.version' do
6
-
6
+
7
7
  it 'is a string' do
8
8
  ScopedFrom.version.should be_a(String)
9
9
  end
10
-
10
+
11
11
  it 'is with correct format' do
12
12
  ScopedFrom.version.should match(/^\d+\.\d+(\.\d+)?/)
13
13
  end
14
-
14
+
15
15
  it 'is freezed' do
16
16
  expect {
17
17
  ScopedFrom.version.gsub!('.', '#')
18
18
  }.to raise_error(/can't modify frozen string/i)
19
19
  end
20
-
20
+
21
21
  end
22
-
22
+
23
23
  end
data/spec/spec_helper.rb CHANGED
@@ -2,23 +2,21 @@ ENV["RAILS_ENV"] ||= 'test'
2
2
 
3
3
  require File.dirname(__FILE__) + '/../lib/scoped_from'
4
4
 
5
- # Support
5
+ # Support
6
6
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
7
7
 
8
8
  # Mocks
9
9
  ActiveSupport::Dependencies.autoload_paths << "#{File.dirname(__FILE__)}/mocks"
10
10
 
11
11
  RSpec.configure do |config|
12
- config.mock_with(:rspec)
13
-
14
12
  config.include(UserMacro)
15
-
13
+
16
14
  config.before(:each) do
17
15
  Comment.delete_all
18
16
  Post.delete_all
19
17
  User.delete_all
20
18
  Vote.delete_all
21
-
19
+
22
20
  create_user(:john, :firstname => 'John', :lastname => 'Doe', :enabled => true, :admin => true)
23
21
  create_user(:jane, :firstname => 'Jane', :lastname => 'Doe', :enabled => false, :admin => false)
24
22
  end
@@ -1,13 +1,13 @@
1
1
  module UserMacro
2
-
2
+
3
3
  USERS = {}
4
-
4
+
5
5
  def create_user(label, attributes)
6
6
  USERS[label] = User.create!(attributes)
7
7
  end
8
-
8
+
9
9
  def users(label)
10
10
  USERS[label]
11
11
  end
12
-
12
+
13
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoped_from
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-07 00:00:00.000000000 Z
12
+ date: 2012-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70097913921820 !ruby/object:Gem::Requirement
16
+ requirement: &70282866988720 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70097913921820
24
+ version_requirements: *70282866988720
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70097913920840 !ruby/object:Gem::Requirement
27
+ requirement: &70282866987480 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,32 +32,32 @@ dependencies:
32
32
  version: 3.2.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70097913920840
35
+ version_requirements: *70282866987480
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70097913920000 !ruby/object:Gem::Requirement
38
+ requirement: &70282866986780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
42
42
  - !ruby/object:Gem::Version
43
- version: 2.8.0
43
+ version: 2.10.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70097913920000
46
+ version_requirements: *70282866986780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec-rails
49
- requirement: &70097913919120 !ruby/object:Gem::Requirement
49
+ requirement: &70282867000940 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: 2.8.0
54
+ version: 2.10.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70097913919120
57
+ version_requirements: *70282867000940
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sqlite3-ruby
60
- requirement: &70097913918540 !ruby/object:Gem::Requirement
60
+ requirement: &70282867000520 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70097913918540
68
+ version_requirements: *70282867000520
69
69
  description: Provides a simple mapping between Active Record scopes and controller
70
70
  parameters for Ruby On Rails 3
71
71
  email: al@alweb.org
@@ -81,7 +81,6 @@ files:
81
81
  - README.mdown
82
82
  - Rakefile
83
83
  - VERSION
84
- - init.rb
85
84
  - lib/scoped_from.rb
86
85
  - lib/scoped_from/active_record.rb
87
86
  - lib/scoped_from/query.rb
@@ -113,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
112
  version: '0'
114
113
  segments:
115
114
  - 0
116
- hash: -1127072327386759365
115
+ hash: 1157272612327936896
117
116
  required_rubygems_version: !ruby/object:Gem::Requirement
118
117
  none: false
119
118
  requirements:
@@ -122,10 +121,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
121
  version: '0'
123
122
  segments:
124
123
  - 0
125
- hash: -1127072327386759365
124
+ hash: 1157272612327936896
126
125
  requirements: []
127
126
  rubyforge_project: scoped_from
128
- rubygems_version: 1.8.15
127
+ rubygems_version: 1.8.17
129
128
  signing_key:
130
129
  specification_version: 3
131
130
  summary: Mapping between scopes and parameters for Rails
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/lib/scoped_from')