context_aware_scope 0.0.3 → 0.1.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/VERSION +1 -1
- data/context_aware_scope.gemspec +3 -1
- data/lib/context_aware_scope.rb +8 -1
- data/test/models.rb +5 -1
- data/test/units/default_scope_test.rb +41 -0
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/context_aware_scope.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{context_aware_scope}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Simplificator", "Fabio Kuhn"]
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"test/database.yml",
|
26
26
|
"test/models.rb",
|
27
27
|
"test/test_helper.rb",
|
28
|
+
"test/units/default_scope_test.rb",
|
28
29
|
"test/units/lambda_scope_test.rb",
|
29
30
|
"test/units/normal_scope_test.rb",
|
30
31
|
"test/units/readme_sample_test.rb"
|
@@ -37,6 +38,7 @@ Gem::Specification.new do |s|
|
|
37
38
|
s.test_files = [
|
38
39
|
"test/models.rb",
|
39
40
|
"test/test_helper.rb",
|
41
|
+
"test/units/default_scope_test.rb",
|
40
42
|
"test/units/lambda_scope_test.rb",
|
41
43
|
"test/units/normal_scope_test.rb",
|
42
44
|
"test/units/readme_sample_test.rb"
|
data/lib/context_aware_scope.rb
CHANGED
@@ -17,7 +17,8 @@ module ContextAwareScope
|
|
17
17
|
recursive_context = recursive_context.keep_merge(@context)
|
18
18
|
recursive_context
|
19
19
|
else
|
20
|
-
|
20
|
+
# add default scope context if on active_record
|
21
|
+
default_scoping.inject(@context){|context, scope| context.keep_merge(scope[:find][:context])}
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
@@ -35,6 +36,12 @@ module ContextAwareScope
|
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
39
|
+
class ActiveRecord::Base
|
40
|
+
def context
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
38
45
|
class ActiveRecord::NamedScope::Scope
|
39
46
|
include ContextAwareScope
|
40
47
|
end
|
data/test/models.rb
CHANGED
@@ -14,7 +14,10 @@ class Customer < ActiveRecord::Base
|
|
14
14
|
end
|
15
15
|
|
16
16
|
class City < ActiveRecord::Base
|
17
|
-
|
17
|
+
default_scope :order => 'cities.name ASC', :context => {:order => 'name asc'}
|
18
|
+
named_scope :basel, :conditions => {:name => 'Basel'}, :context => {:name => 'Basel'}
|
19
|
+
|
20
|
+
has_many :customers
|
18
21
|
end
|
19
22
|
|
20
23
|
class Product < ActiveRecord::Base
|
@@ -64,6 +67,7 @@ def seed_models
|
|
64
67
|
order2 = Order.create(:customer => customer1, :product_name => 'old lamp', :price => 5, :purchased_at => '2010-07-22')
|
65
68
|
order3 = Order.create(:customer => customer2, :product_name => 'camel', :price => 1200, :purchased_at => '2010-07-24')
|
66
69
|
product = Product.create(:name => 'Bracelet', :price => 324, :created_at => 1.day.ago)
|
70
|
+
product = Product.create(:name => 'Carpet', :price => 12, :created_at => 1.day.ago)
|
67
71
|
end
|
68
72
|
|
69
73
|
def cleanup_database
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DefaultScopeTest < Test::Unit::TestCase
|
4
|
+
context 'cities' do
|
5
|
+
setup do
|
6
|
+
seed_models
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'with a default scope' do
|
10
|
+
setup do
|
11
|
+
@cities = City.scoped({})
|
12
|
+
end
|
13
|
+
|
14
|
+
should 'should have results' do
|
15
|
+
assert_equal 2, @cities.count
|
16
|
+
end
|
17
|
+
|
18
|
+
should 'have context on list' do
|
19
|
+
assert_equal ActiveRecord::NamedScope::Scope, @cities.class
|
20
|
+
assert_equal Hash, @cities.context.class
|
21
|
+
assert_equal ({:order => 'name asc'}), @cities.context
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'with a default scope and a named scope' do
|
26
|
+
setup do
|
27
|
+
@cities = City.basel.scoped({})
|
28
|
+
end
|
29
|
+
|
30
|
+
should 'should have results' do
|
31
|
+
assert_equal 1, @cities.count
|
32
|
+
end
|
33
|
+
|
34
|
+
should 'have context on list' do
|
35
|
+
assert_equal ActiveRecord::NamedScope::Scope, @cities.class
|
36
|
+
assert_equal Hash, @cities.context.class
|
37
|
+
assert_equal ({:order => 'name asc', :name => 'Basel'}), @cities.context
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: context_aware_scope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.3
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Simplificator
|
@@ -82,6 +82,7 @@ files:
|
|
82
82
|
- test/database.yml
|
83
83
|
- test/models.rb
|
84
84
|
- test/test_helper.rb
|
85
|
+
- test/units/default_scope_test.rb
|
85
86
|
- test/units/lambda_scope_test.rb
|
86
87
|
- test/units/normal_scope_test.rb
|
87
88
|
- test/units/readme_sample_test.rb
|
@@ -122,6 +123,7 @@ summary: Add context to named_scopes in ActiveRecord
|
|
122
123
|
test_files:
|
123
124
|
- test/models.rb
|
124
125
|
- test/test_helper.rb
|
126
|
+
- test/units/default_scope_test.rb
|
125
127
|
- test/units/lambda_scope_test.rb
|
126
128
|
- test/units/normal_scope_test.rb
|
127
129
|
- test/units/readme_sample_test.rb
|