convenient_scopes 0.6.0 → 0.7.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.
data/Rakefile CHANGED
@@ -44,7 +44,7 @@ task :test => :check_dependencies
44
44
 
45
45
  task :default => :test
46
46
 
47
- require 'rake/rdoctask'
47
+ require 'rdoc/task'
48
48
  Rake::RDocTask.new do |rdoc|
49
49
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
50
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{convenient_scopes}
8
- s.version = "0.6.0"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ivan Schneider"]
12
- s.date = %q{2011-03-22}
12
+ s.date = %q{2011-07-10}
13
13
  s.description = %q{Dynamic scopes by convention for ActiveRecord 3}
14
14
  s.email = %q{isc@massivebraingames.com}
15
15
  s.extra_rdoc_files = [
@@ -28,7 +28,8 @@ Gem::Specification.new do |s|
28
28
  "test/test_all_and_any_conditions.rb",
29
29
  "test/test_associations.rb",
30
30
  "test/test_conditions.rb",
31
- "test/test_ordering.rb"
31
+ "test/test_ordering.rb",
32
+ "test/test_search_with_hash.rb"
32
33
  ]
33
34
  s.homepage = %q{http://github.com/isc/convenient_scopes}
34
35
  s.require_paths = ["lib"]
@@ -39,7 +40,8 @@ Gem::Specification.new do |s|
39
40
  "test/test_all_and_any_conditions.rb",
40
41
  "test/test_associations.rb",
41
42
  "test/test_conditions.rb",
42
- "test/test_ordering.rb"
43
+ "test/test_ordering.rb",
44
+ "test/test_search_with_hash.rb"
43
45
  ]
44
46
 
45
47
  if s.respond_to? :specification_version then
@@ -8,6 +8,21 @@ module ConvenientScopes
8
8
  super
9
9
  end
10
10
  end
11
+
12
+ def search search_scopes
13
+ res = unscoped
14
+ search_scopes.each do |name, args|
15
+ if scopes.keys.include?(name.to_sym) || !respond_to?(name)
16
+ res = res.send name, args unless args == false
17
+ else
18
+ raise InvalidScopes
19
+ end
20
+ end
21
+ res
22
+ end
23
+
24
+ class InvalidScopes < Exception
25
+ end
11
26
 
12
27
  module Conditions
13
28
 
@@ -15,53 +30,31 @@ module ConvenientScopes
15
30
  return unless (column = match_suffix_and_column_name name, %w(equals eq is))
16
31
  lambda {|value| unscoped.where(column => value)}
17
32
  end
18
-
19
- def does_not_equal_scope name
20
- match_and_define_scope name, %w(does_not_equal doesnt_equal ne is_not), "%s != ?"
21
- end
22
-
23
- def less_than_scope name
24
- match_and_define_scope name, %w(less_than lt before), "%s < ?"
25
- end
26
-
27
- def less_than_or_equal_scope name
28
- match_and_define_scope name, %w(less_than_or_equal lte), "%s <= ?"
29
- end
30
-
31
- def greater_than_scope name
32
- match_and_define_scope name, %w(greater_than gt after), "%s > ?"
33
- end
34
-
35
- def greater_than_or_equal_scope name
36
- match_and_define_scope name, %w(greater_than_or_equal gte), "%s >= ?"
37
- end
38
-
39
- def like_scope name
40
- match_and_define_scope name, %w(like matches contains includes), "%s like ?", "%%%s%%"
41
- end
42
-
43
- def not_like_scope name
44
- suffixes = %w(not_like does_not_match doesnt_match does_not_contain doesnt_contain does_not_include doesnt_include)
45
- match_and_define_scope name, suffixes, "%s not like ?", "%%%s%%"
46
- end
47
-
48
- def begins_with_scope name
49
- match_and_define_scope name, %w(begins_with bw starts_with sw), "%s like ?", "%s%%"
50
- end
51
-
52
- def not_begin_with_scope name
53
- suffixes = %w(not_begin_with does_not_begin_with doesnt_begin_with does_not_start_with doesnt_start_with)
54
- match_and_define_scope name, suffixes, "%s not like ?", "%s%%"
55
- end
56
-
57
- def ends_with_scope name
58
- match_and_define_scope name, %w(ends_with ew), "%s like ?", "%%%s"
59
- end
60
-
61
- def not_end_with_scope name
62
- match_and_define_scope name, %w(not_end_with does_not_end_with doesnt_end_with), "%s not like ?", "%%%s"
33
+
34
+ SCOPE_DEFINITIONS = [
35
+ [%w(does_not_equal doesnt_equal ne is_not), "%s != ?"],
36
+ [%w(less_than lt before), "%s < ?"],
37
+ [%w(less_than_or_equal lte), "%s <= ?"],
38
+ [%w(greater_than gt after), "%s > ?"],
39
+ [%w(greater_than_or_equal gte), "%s >= ?"],
40
+ [%w(like matches contains includes), "%s like ?", "%%%s%%"],
41
+ [%w(not_like does_not_match doesnt_match does_not_contain doesnt_contain does_not_include doesnt_include), "%s not like ?", "%%%s%%"],
42
+ [%w(begins_with bw starts_with sw), "%s like ?", "%s%%"],
43
+ [%w(not_begin_with does_not_begin_with doesnt_begin_with does_not_start_with doesnt_start_with), "%s not like ?", "%s%%"],
44
+ [%w(ends_with ew), "%s like ?", "%%%s"],
45
+ [%w(not_end_with does_not_end_with doesnt_end_with), "%s not like ?", "%%%s"],
46
+ [%w(between), "%s >= ? AND %s < ?"]
47
+ ]
48
+
49
+ def scopes_with_values name
50
+ SCOPE_DEFINITIONS.each do |definition|
51
+ if scope_arg = (match_and_define_scope name, *definition)
52
+ return scope_arg
53
+ end
54
+ end
55
+ nil
63
56
  end
64
-
57
+
65
58
  def null_scope name
66
59
  match_and_define_scope_without_value name, %w(null nil missing), "%s is null"
67
60
  end
@@ -70,12 +63,7 @@ module ConvenientScopes
70
63
  match_and_define_scope_without_value name, %w(not_null not_nil not_missing), "%s is not null"
71
64
  end
72
65
 
73
- def between name
74
- match_and_define_scope name, %w(between), "%s >= ? AND %s < ?"
75
- end
76
-
77
66
  def boolean_column_scope name
78
- return unless column_names.include? name.to_s
79
67
  return unless boolean_column? name
80
68
  unscoped.where(name => true)
81
69
  end
@@ -83,7 +71,6 @@ module ConvenientScopes
83
71
  def negative_boolean_column_scope name
84
72
  str_name = name.to_s
85
73
  return unless str_name.gsub!(/^not_/, '')
86
- return unless column_names.include? str_name
87
74
  return unless boolean_column? str_name
88
75
  unscoped.where(str_name => false)
89
76
  end
@@ -169,7 +156,7 @@ module ConvenientScopes
169
156
  end
170
157
 
171
158
  def boolean_column? name
172
- columns.detect {|c|c.name == name.to_s}.type == :boolean
159
+ columns.detect {|c|c.name == name.to_s}.try(:type) == :boolean
173
160
  end
174
161
 
175
162
  def convert_to_scope_arg scope_data
@@ -14,10 +14,10 @@ class TestAssociations < Test::Unit::TestCase
14
14
  end
15
15
 
16
16
  should "not catch everything" do
17
- assert_raise NoMethodError do
17
+ assert_raises NoMethodError do
18
18
  User.comment_body_is('Yo')
19
19
  end
20
- assert_raise NoMethodError do
20
+ assert_raises NoMethodError do
21
21
  User.comments_synopsis_eq('Yo')
22
22
  end
23
23
  end
@@ -10,7 +10,7 @@ class TestConditions < Test::Unit::TestCase
10
10
  end
11
11
 
12
12
  should "equals scope" do
13
- assert_raise NoMethodError do
13
+ assert_raises NoMethodError do
14
14
  User.blabla_eq('Bob')
15
15
  end
16
16
  assert_equal [@bob], User.pseudo_eq('Bob')
@@ -65,6 +65,12 @@ class TestConditions < Test::Unit::TestCase
65
65
  should "boolean columns" do
66
66
  assert_equal [@bob], User.admin
67
67
  assert_equal [@slim], User.not_admin
68
+ assert_raises NoMethodError do
69
+ User.not_age
70
+ end
71
+ assert_raises NoMethodError do
72
+ User.not_shablagoo
73
+ end
68
74
  end
69
75
 
70
76
  should "not mix up scopes" do
@@ -0,0 +1,41 @@
1
+ require 'helper'
2
+
3
+ class TestSearchWithHash < Test::Unit::TestCase
4
+
5
+ context "Given two users" do
6
+ setup do
7
+ @bob = User.create :pseudo => 'Bob', :first_name => 'Robert', :age => 37,
8
+ :activated_at => 37.hours.ago, :admin => true
9
+ @slim = User.create :pseudo => 'Slim', :first_name => 'Angelo', :age => 12,
10
+ :activated_at => nil, :admin => false
11
+ end
12
+
13
+ should "search with hash" do
14
+ assert_equal [@bob], User.search(:pseudo_like => 'Bo')
15
+ assert_equal [], User.search(:pseudo_like => 'Bo', :age_is => 12)
16
+ end
17
+
18
+ should "search with hash with scopes with no argument" do
19
+ assert_equal [@bob], User.search(:admin => true)
20
+ assert_equal [@slim], User.search(:not_admin => true)
21
+ end
22
+
23
+ should "not apply the scope when the value associated is false" do
24
+ assert_equal [@bob, @slim], User.search(:admin => false)
25
+ end
26
+
27
+ should "be able to leverage existing scopes" do
28
+ User.scope :underage, User.age_lt(18)
29
+ assert_equal [@slim], User.search(:underage => true)
30
+ end
31
+
32
+ should "be safe when using search with hash" do
33
+ assert_raises ConvenientScopes::InvalidScopes do
34
+ User.search :destroy => @bob.id
35
+ end
36
+ assert_equal 2, User.count
37
+ end
38
+
39
+ end
40
+
41
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: convenient_scopes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
8
+ - 7
9
9
  - 0
10
- version: 0.6.0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ivan Schneider
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-22 00:00:00 +01:00
18
+ date: 2011-07-10 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -84,6 +84,7 @@ files:
84
84
  - test/test_associations.rb
85
85
  - test/test_conditions.rb
86
86
  - test/test_ordering.rb
87
+ - test/test_search_with_hash.rb
87
88
  has_rdoc: true
88
89
  homepage: http://github.com/isc/convenient_scopes
89
90
  licenses: []
@@ -124,3 +125,4 @@ test_files:
124
125
  - test/test_associations.rb
125
126
  - test/test_conditions.rb
126
127
  - test/test_ordering.rb
128
+ - test/test_search_with_hash.rb