convenient_scopes 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -16,4 +16,4 @@ Pretty much the same API as searchlogic except it's compatible with ActiveRecord
16
16
 
17
17
  == Copyright
18
18
 
19
- Copyright (c) 2010 Ivan Schneider. See LICENSE for details.
19
+ Copyright (c) 2011 Ivan Schneider. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.1
1
+ 0.8.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.7.1"
8
+ s.version = "0.8.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-07-10}
12
+ s.date = %q{2011-07-12}
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 = [
@@ -10,7 +10,7 @@ module ConvenientScopes
10
10
  end
11
11
 
12
12
  def define_scope name
13
- ([Conditions, Ordering].map(&:instance_methods).flatten.inject nil do |memo, scope_type|
13
+ (ScopeDefinitions.instance_methods.inject nil do |memo, scope_type|
14
14
  memo ||= send scope_type.to_sym, name
15
15
  end) || (association_scope name)
16
16
  end
@@ -41,7 +41,7 @@ module ConvenientScopes
41
41
 
42
42
  class InvalidScopes < Exception ; end
43
43
 
44
- module Conditions
44
+ module ScopeDefinitions
45
45
 
46
46
  SCOPE_DEFINITIONS = [
47
47
  [%w(does_not_equal doesnt_equal ne is_not), "%s != ?"],
@@ -60,7 +60,14 @@ module ConvenientScopes
60
60
 
61
61
  SCOPE_WITHOUT_VALUE_DEFINITIONS = [
62
62
  [%w(null nil missing), "%s is null"],
63
- [%w(not_null not_nil not_missing), "%s is not null"]
63
+ [%w(not_null not_nil not_missing), "%s is not null"],
64
+ [%w(blank not_present), "%s is null OR %s = ''"],
65
+ [%w(not_blank present), "%s is not null AND %s <> ''"]
66
+ ]
67
+
68
+ ORDERING_SCOPE_DEFINITIONS = [
69
+ [/^ascend_by_/, 'asc'],
70
+ [/^descend_by_/, 'desc']
64
71
  ]
65
72
 
66
73
  def scopes_with_value name
@@ -75,45 +82,33 @@ module ConvenientScopes
75
82
  end
76
83
  end
77
84
 
85
+ def ordering_scopes name
86
+ ORDERING_SCOPE_DEFINITIONS.inject nil do |memo, definition|
87
+ memo ||= match_ordering_scope name, *definition
88
+ end
89
+ end
90
+
78
91
  def equals_scope name
79
92
  return unless (column = match_suffix_and_column_name name, %w(equals eq is))
80
93
  lambda {|value| unscoped.where(column => value)}
81
94
  end
82
95
 
83
- def boolean_column_scope name
84
- return unless boolean_column? name
85
- unscoped.where(name => true)
86
- end
87
-
88
- def negative_boolean_column_scope name
96
+ def boolean_column_scopes name
89
97
  str_name = name.to_s
90
- return unless str_name.gsub!(/^not_/, '') && boolean_column?(str_name)
91
- unscoped.where(str_name => false)
92
- end
93
- end
94
-
95
- include Conditions
96
-
97
- module Ordering
98
-
99
- def ascend_by_scope name
100
- ordering_scope name, /^ascend_by_/, 'asc'
101
- end
102
-
103
- def descend_by_scope name
104
- ordering_scope name, /^descend_by_/, 'desc'
98
+ value = !str_name.gsub!(/^not_/, '')
99
+ unscoped.where(str_name => value) if boolean_column? str_name
105
100
  end
106
101
 
107
102
  end
108
103
 
109
- def ordering_scope name, prefix, direction
104
+ include ScopeDefinitions
105
+
106
+ def match_ordering_scope name, prefix, direction
110
107
  str_name = name.to_s
111
108
  return unless str_name.gsub!(prefix, '')
112
109
  determine_order_scope_data str_name, direction
113
110
  end
114
111
 
115
- include Ordering
116
-
117
112
  def association_scope name
118
113
  return unless assoc = (possible_association_for_scope name)
119
114
  next_scope = extract_next_scope name, assoc
@@ -145,7 +140,7 @@ module ConvenientScopes
145
140
  end
146
141
 
147
142
  def match_suffix_and_column_name name, suffixes
148
- str_name = name.to_s
143
+ str_name = name.to_s.dup
149
144
  regexp_str = suffixes.map {|suffix| "(_#{suffix})"}.join('|') + '$'
150
145
  return unless str_name.gsub!(Regexp.new(regexp_str), '')
151
146
  return unless column_names.include? str_name
@@ -73,6 +73,20 @@ class TestConditions < Test::Unit::TestCase
73
73
  end
74
74
  end
75
75
 
76
+ should "blank and present scopes" do
77
+ u = User.create :pseudo => ''
78
+ assert_equal [u], User.pseudo_blank
79
+ assert_equal [u], User.pseudo_not_present
80
+ u.update_attribute :pseudo, nil
81
+ assert_equal [u], User.pseudo_blank
82
+ assert_equal [u], User.pseudo_not_present
83
+ assert_equal 2, User.pseudo_not_blank.count
84
+ assert_equal 2, User.pseudo_present.count
85
+ u.update_attribute :pseudo, ''
86
+ assert_equal 2, User.pseudo_not_blank.count
87
+ assert_equal 2, User.pseudo_present.count
88
+ end
89
+
76
90
  should "not mix up scopes" do
77
91
  User.age_gt(0).pseudo_null
78
92
  sql = User.pseudo_null.to_sql
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: 1
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 7
9
- - 1
10
- version: 0.7.1
8
+ - 8
9
+ - 0
10
+ version: 0.8.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-07-10 00:00:00 +02:00
18
+ date: 2011-07-12 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency