dynamic_default_scoping 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -0
- data/lib/dynamic_default_scoping/version.rb +1 -1
- data/spec/models/post.rb +14 -0
- data/spec/spec_helper.rb +6 -31
- data/spec/support/connection.rb +15 -0
- data/spec/support/matchers.rb +7 -0
- data/spec/tables/posts.rb +4 -0
- data/spec/unit/dynamic_default_scoping_spec.rb +38 -52
- metadata +11 -3
data/README.rdoc
CHANGED
@@ -51,3 +51,8 @@ Here is how it should be:
|
|
51
51
|
== When there will be a normal implementation of this feature?
|
52
52
|
|
53
53
|
http://rails.lighthouseapp.com/projects/8994/tickets/1812-default_scope-cant-take-procs
|
54
|
+
|
55
|
+
== Credits
|
56
|
+
|
57
|
+
* Adam Wróbel (https://github.com/amw)
|
58
|
+
* Ivan Ukhov (https://github.com/UVSoft)
|
data/spec/models/post.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class Post < ActiveRecord::Base
|
2
|
+
scope :recent, limit(5)
|
3
|
+
|
4
|
+
include DynamicDefaultScoping
|
5
|
+
|
6
|
+
default_scope :language, lambda { |*args|
|
7
|
+
case locale = (args[0].presence || I18n.locale).to_s
|
8
|
+
when 'any', 'all'
|
9
|
+
where :locale => I18n.available_locales.map(&:to_s)
|
10
|
+
else
|
11
|
+
where :locale => locale
|
12
|
+
end
|
13
|
+
}
|
14
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -5,39 +5,14 @@ require 'active_support/all'
|
|
5
5
|
require 'active_record'
|
6
6
|
require 'dynamic_default_scoping'
|
7
7
|
|
8
|
+
%w(models support).each do |directory|
|
9
|
+
Dir[File.join(File.dirname(__FILE__), "#{ directory }/**/*.rb")].each do |file|
|
10
|
+
require file
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
8
14
|
RSpec.configure do |config|
|
9
15
|
config.before(:each) do
|
10
16
|
Post.unscoped.delete_all
|
11
17
|
end
|
12
18
|
end
|
13
|
-
|
14
|
-
ActiveRecord::Base.establish_connection \
|
15
|
-
:adapter => 'mysql2',
|
16
|
-
:hostname => 'localhost',
|
17
|
-
:database => 'dynamic_default_scoping',
|
18
|
-
:username => 'root',
|
19
|
-
:password => ''
|
20
|
-
|
21
|
-
connection = ActiveRecord::Base.connection
|
22
|
-
connection.drop_table 'posts' if connection.table_exists? 'posts'
|
23
|
-
connection.create_table 'posts' do |table|
|
24
|
-
table.string :locale
|
25
|
-
table.timestamps
|
26
|
-
end
|
27
|
-
|
28
|
-
LOCALES = [ :en, :ru ]
|
29
|
-
|
30
|
-
class Post < ActiveRecord::Base
|
31
|
-
scope :recent, limit(5)
|
32
|
-
|
33
|
-
include DynamicDefaultScoping
|
34
|
-
|
35
|
-
default_scope :language, lambda { |*args|
|
36
|
-
case locale = (args[0].presence || I18n.locale).to_s
|
37
|
-
when 'any', 'all'
|
38
|
-
where :locale => LOCALES.map(&:to_s)
|
39
|
-
else
|
40
|
-
where :locale => locale
|
41
|
-
end
|
42
|
-
}
|
43
|
-
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
ActiveRecord::Base.establish_connection \
|
2
|
+
:adapter => 'mysql2',
|
3
|
+
:hostname => 'localhost',
|
4
|
+
:database => 'dynamic_default_scoping',
|
5
|
+
:username => 'root',
|
6
|
+
:password => ''
|
7
|
+
|
8
|
+
connection = ActiveRecord::Base.connection
|
9
|
+
|
10
|
+
ActiveRecord::Base.descendants.each do |model|
|
11
|
+
table_name = model.table_name
|
12
|
+
connection.drop_table table_name if connection.table_exists? table_name
|
13
|
+
table = File.join File.dirname(__FILE__), '..', "tables/#{ table_name }.rb"
|
14
|
+
connection.instance_eval File.open(table).read if File.exist? table
|
15
|
+
end
|
@@ -1,28 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe DynamicDefaultScoping do
|
4
|
-
|
5
|
-
|
6
|
-
locale = (options[:locale].presence || I18n.locale).to_s
|
7
|
-
args.flatten.all?{ |post| post.locale == locale }.should be_true
|
4
|
+
let :locales do
|
5
|
+
[ :en, :ru ]
|
8
6
|
end
|
9
7
|
|
10
|
-
let
|
11
|
-
let(:per_locale) do
|
8
|
+
let :per_locale do
|
12
9
|
hash = {}
|
13
10
|
locales.each_with_index { |locale, i| hash[locale] = 3 + i }
|
14
11
|
hash
|
15
12
|
end
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
Post.create! :locale => locale.to_s
|
22
|
-
end
|
23
|
-
end
|
14
|
+
before do
|
15
|
+
I18n.stub(:available_locales).and_return locales
|
16
|
+
per_locale.each do |locale, count|
|
17
|
+
count.times { Post.create! :locale => locale.to_s }
|
24
18
|
end
|
19
|
+
end
|
25
20
|
|
21
|
+
describe 'when switching between locales' do
|
26
22
|
around do |block|
|
27
23
|
locales.each do |locale|
|
28
24
|
I18n.locale = locale
|
@@ -30,85 +26,75 @@ describe DynamicDefaultScoping do
|
|
30
26
|
end
|
31
27
|
end
|
32
28
|
|
33
|
-
|
29
|
+
shared_examples_for 'a well localized class' do
|
30
|
+
it 'generates the right query' do
|
31
|
+
part = if @locale.is_a? Array
|
32
|
+
%[`locale` IN (#{ @locale.map{ |l| "'#{ l }'" }.join(', ') })]
|
33
|
+
else
|
34
|
+
%[`locale` = '#{ @locale }']
|
35
|
+
end
|
36
|
+
@relation.to_sql.should include part
|
37
|
+
end
|
38
|
+
|
34
39
|
it 'finds records only in the current locale' do
|
35
|
-
|
40
|
+
[ @relation.first, @relation.all ].should have_valid_locale @locale
|
36
41
|
end
|
37
42
|
|
38
43
|
it 'counts records only in the current locale' do
|
39
|
-
|
44
|
+
count = Array.wrap(@locale).map{ |l| per_locale[l] }.sum
|
45
|
+
@relation.count.should == count
|
40
46
|
end
|
41
47
|
end
|
42
48
|
|
43
|
-
context 'when the locale is specified' do
|
44
|
-
|
45
|
-
|
46
|
-
sql = @scoped.to_sql
|
47
|
-
sql.should include %[`locale` = '#{ @locale }']
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'finds records in the specified locale' do
|
51
|
-
all = @scoped.all
|
52
|
-
all.length.should == per_locale[@locale]
|
53
|
-
should_have_valid_locale all, :locale => @locale
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'counts records in the specified locale' do
|
57
|
-
@scoped.count.should == per_locale[@locale]
|
58
|
-
end
|
49
|
+
context 'when the locale is not specified' do
|
50
|
+
before do
|
51
|
+
@relation, @locale = Post.scoped, I18n.locale
|
59
52
|
end
|
60
53
|
|
54
|
+
it_behaves_like 'a well localized class'
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when the locale is specified' do
|
61
58
|
context 'when there is another scope before' do
|
62
59
|
around do |block|
|
63
60
|
locales.each do |locale|
|
64
|
-
@locale = locale
|
65
|
-
@scoped = Post.recent.language(locale)
|
61
|
+
@relation, @locale = Post.recent.language(locale), locale
|
66
62
|
block.call
|
67
63
|
end
|
68
64
|
end
|
69
65
|
|
70
|
-
it_behaves_like 'well localized class'
|
66
|
+
it_behaves_like 'a well localized class'
|
71
67
|
end
|
72
68
|
|
73
69
|
context 'when there is another scope after' do
|
74
70
|
around do |block|
|
75
71
|
locales.each do |locale|
|
76
|
-
@locale = locale
|
77
|
-
@scoped = Post.language(locale).recent
|
72
|
+
@relation, @locale = Post.language(locale).recent, locale
|
78
73
|
block.call
|
79
74
|
end
|
80
75
|
end
|
81
76
|
|
82
|
-
it_behaves_like 'well localized class'
|
77
|
+
it_behaves_like 'a well localized class'
|
83
78
|
end
|
84
79
|
|
85
80
|
context 'when there is no another scope' do
|
86
81
|
around do |block|
|
87
82
|
locales.each do |locale|
|
88
|
-
@locale = locale
|
89
|
-
@scoped = Post.language(locale)
|
83
|
+
@relation, @locale = Post.language(locale), locale
|
90
84
|
block.call
|
91
85
|
end
|
92
86
|
end
|
93
87
|
|
94
|
-
it_behaves_like 'well localized class'
|
88
|
+
it_behaves_like 'a well localized class'
|
95
89
|
end
|
96
90
|
end
|
97
91
|
|
98
92
|
context 'when we are interested in all locales' do
|
99
|
-
|
100
|
-
|
101
|
-
it 'finds all records' do
|
102
|
-
sql = scoped.to_sql
|
103
|
-
locales.each do |locale|
|
104
|
-
sql.should =~ %r[`locale` IN \(.*'#{ locale }'.*\)]
|
105
|
-
end
|
106
|
-
scoped.length.should == per_locale.values.sum
|
93
|
+
before do
|
94
|
+
@relation, @locale = Post.language(:any), locales
|
107
95
|
end
|
108
96
|
|
109
|
-
|
110
|
-
scoped.count.should == per_locale.values.sum
|
111
|
-
end
|
97
|
+
it_behaves_like 'a well localized class'
|
112
98
|
end
|
113
99
|
end
|
114
100
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_default_scoping
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ivan Ukhov
|
@@ -84,7 +84,11 @@ files:
|
|
84
84
|
- dynamic_default_scoping.gemspec
|
85
85
|
- lib/dynamic_default_scoping.rb
|
86
86
|
- lib/dynamic_default_scoping/version.rb
|
87
|
+
- spec/models/post.rb
|
87
88
|
- spec/spec_helper.rb
|
89
|
+
- spec/support/connection.rb
|
90
|
+
- spec/support/matchers.rb
|
91
|
+
- spec/tables/posts.rb
|
88
92
|
- spec/unit/dynamic_default_scoping_spec.rb
|
89
93
|
has_rdoc: true
|
90
94
|
homepage: https://github.com/UVSoft/dynamic_default_scoping
|
@@ -121,5 +125,9 @@ signing_key:
|
|
121
125
|
specification_version: 3
|
122
126
|
summary: Make ActiveRecord::Base.default_scope take a proc and have a name.
|
123
127
|
test_files:
|
128
|
+
- spec/models/post.rb
|
124
129
|
- spec/spec_helper.rb
|
130
|
+
- spec/support/connection.rb
|
131
|
+
- spec/support/matchers.rb
|
132
|
+
- spec/tables/posts.rb
|
125
133
|
- spec/unit/dynamic_default_scoping_spec.rb
|