mongoid-publishing_logic 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.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +17 -0
- data/LICENSE.txt +22 -0
- data/README.md +97 -0
- data/Rakefile +2 -0
- data/lib/mongoid/publishing_logic/localized.rb +69 -0
- data/lib/mongoid/publishing_logic/version.rb +5 -0
- data/lib/mongoid/publishing_logic.rb +103 -0
- data/mongoid-publishing_logic.gemspec +33 -0
- data/spec/lib/mongoid/publishing_logic/localized_spec.rb +8 -0
- data/spec/lib/mongoid/publishing_logic_spec.rb +286 -0
- data/spec/mongoid.yml +6 -0
- data/spec/spec_helper.rb +91 -0
- metadata +219 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bea998cae9c6548acd4f4d6132410099b4bc34b5
|
4
|
+
data.tar.gz: 407fac28c319e2e920e91b0c345b8db1128b1f7b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8819fc3819789fdede86197948d87b605ae5b010b8424e71bb202e7ac4609b6f803e188c01ddc9c92ebb7e5b45c0a3022b1f603b00fd004bac5d0350c19002ee
|
7
|
+
data.tar.gz: 76af3c7f5da71351f21a9a90742dff02d6ec27e146d07ebc04e7bb460dff194b411640acbe25dd790c7b2954ff68ad0cb4d1227b2a0f3ddd55816d42d8727f44
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
5
|
+
# rspec may be run, below are examples of the most common uses.
|
6
|
+
# * bundler: 'bundle exec rspec'
|
7
|
+
# * bundler binstubs: 'bin/rspec'
|
8
|
+
# * spring: 'bin/rsspec' (This will use spring if running and you have
|
9
|
+
# installed the spring binstubs per the docs)
|
10
|
+
# * zeus: 'zeus rspec' (requires the server to be started separetly)
|
11
|
+
# * 'just' rspec: 'rspec'
|
12
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
13
|
+
watch(%r{^spec/.+_spec\.rb$})
|
14
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
15
|
+
watch('spec/spec_helper.rb') { "spec" }
|
16
|
+
end
|
17
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Joakim Reinert
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# Mongoid::PublishingLogic
|
2
|
+
|
3
|
+
A set of methods and scopes for publishing logic in mongoid models
|
4
|
+
|
5
|
+
Basically a rewrite of [codegourmet/mm-publishing-logic](https://github.com/codegourmet/mm-publishing-logic) for mongoid.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'mongoid-publishing_logic'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mongoid-publishing_logic
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
class Page
|
27
|
+
include Mongoid::Document
|
28
|
+
include Mongoid::PublishingLogic
|
29
|
+
|
30
|
+
field :title, type: String
|
31
|
+
end
|
32
|
+
|
33
|
+
page = Page.create(title: 'My Awesome Page')
|
34
|
+
page.published? # => false
|
35
|
+
|
36
|
+
published_page = Page.create(published_flag: true, title: 'My Awesome Published Page')
|
37
|
+
published_page.published? # => true
|
38
|
+
|
39
|
+
Page.published.map(&:title) # => ["My Awesome Published Page"]
|
40
|
+
Page.unpublished.map(&:title) # => ["My Awesome Page"]
|
41
|
+
|
42
|
+
Mongoid::PublishingLogic.deactivate
|
43
|
+
Page.published.map(&:title) # => ["My Awesome Published Page", "My Awesome Page"]
|
44
|
+
|
45
|
+
Mongoid::PublishingLogic.activate
|
46
|
+
|
47
|
+
Page.deactivated do
|
48
|
+
Page.published.map(&:title) # => ["My Awesome Published Page", "My Awesome Page"]
|
49
|
+
end
|
50
|
+
|
51
|
+
Page.published.map(&:title) # => ["My Awesome Published Page"]
|
52
|
+
|
53
|
+
Mongoid::PublishingLogic.deactivate
|
54
|
+
|
55
|
+
Page.activated do
|
56
|
+
Page.published.map(&:title) # => ["My Awesome Published Page"]
|
57
|
+
end
|
58
|
+
|
59
|
+
Page.published.map(&:title) # => ["My Awesome Published Page", "My Awesome Page"]
|
60
|
+
|
61
|
+
soon_to_be_unpublished_page = Page.create(
|
62
|
+
title: 'My Deprecated Page',
|
63
|
+
published_flag: true,
|
64
|
+
publishing_end_date: Date.today.next_day
|
65
|
+
)
|
66
|
+
|
67
|
+
sleep(1.day)
|
68
|
+
|
69
|
+
soon_to_be_unpublished_page.published? # => false
|
70
|
+
|
71
|
+
soon_to_be_published_page = Page.create(
|
72
|
+
title: 'My Brand-New Page',
|
73
|
+
published_flag: true,
|
74
|
+
publishing_date: Date.today.next_day
|
75
|
+
)
|
76
|
+
|
77
|
+
sleep(1.day)
|
78
|
+
|
79
|
+
soon_to_be_published_page.published? # => true
|
80
|
+
```
|
81
|
+
|
82
|
+
## Caveats
|
83
|
+
|
84
|
+
Avoid directly calling `Mongoid::PublishingLogic.deactivate` or `activate`. Due
|
85
|
+
to class caching in Rails you will be facing nondeterministic behavior so that
|
86
|
+
the value can survive requests in a production environment. Instead use the
|
87
|
+
`activated` and `deactivated` methods to temporarily change the active
|
88
|
+
attribute.
|
89
|
+
|
90
|
+
## Contributing
|
91
|
+
|
92
|
+
1. Fork it ( https://github.com/jreinert/mongoid-publishing_logic/fork )
|
93
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
94
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
95
|
+
4. Write and run tests, make sure everything passes
|
96
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
97
|
+
6. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'mongoid/publishing_logic'
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module PublishingLogic::Localized
|
5
|
+
extend ::ActiveSupport::Concern
|
6
|
+
include PublishingLogic
|
7
|
+
|
8
|
+
included do
|
9
|
+
field :published_flag, type: Boolean, default: false, localize: true
|
10
|
+
field :publishing_date, type: Date, default: lambda { Date.today }, localize: true
|
11
|
+
field :publishing_end_date, type: Date, localize: true
|
12
|
+
|
13
|
+
scope :published, lambda {|locale=I18n.locale|
|
14
|
+
if PublishingLogic.active?
|
15
|
+
locale_was = I18n.locale
|
16
|
+
begin
|
17
|
+
I18n.locale = locale
|
18
|
+
where(
|
19
|
+
published_flag: true,
|
20
|
+
:$and => [
|
21
|
+
{:$or => [
|
22
|
+
{:publishing_date.exists => false},
|
23
|
+
{:publishing_date => nil},
|
24
|
+
{:publishing_date.lte => Date.today}
|
25
|
+
]},
|
26
|
+
{:$or => [
|
27
|
+
{publishing_end_date: nil},
|
28
|
+
{:publishing_end_date.gt => Date.today}
|
29
|
+
]}
|
30
|
+
]
|
31
|
+
)
|
32
|
+
ensure
|
33
|
+
I18n.locale = locale_was
|
34
|
+
end
|
35
|
+
else
|
36
|
+
all
|
37
|
+
end
|
38
|
+
}
|
39
|
+
|
40
|
+
scope :unpublished, lambda {|locale=I18n.locale|
|
41
|
+
if PublishingLogic.active?
|
42
|
+
locale_was = I18n.locale
|
43
|
+
begin
|
44
|
+
I18n.locale = locale
|
45
|
+
self.or(
|
46
|
+
{published_flag: false},
|
47
|
+
{:publishing_date.gt => Date.today},
|
48
|
+
{:publishing_end_date.lte => Date.today}
|
49
|
+
)
|
50
|
+
ensure
|
51
|
+
I18n.locale = locale_was
|
52
|
+
end
|
53
|
+
else
|
54
|
+
where(:_id.in => [])
|
55
|
+
end
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
def published?(locale=I18n.locale)
|
60
|
+
locale_was = I18n.locale
|
61
|
+
begin
|
62
|
+
I18n.locale = locale
|
63
|
+
super()
|
64
|
+
ensure
|
65
|
+
I18n.locale = locale_was
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require "mongoid/publishing_logic/version"
|
2
|
+
require "active_support/concern"
|
3
|
+
require "mongoid"
|
4
|
+
|
5
|
+
module Mongoid
|
6
|
+
module PublishingLogic
|
7
|
+
extend ::ActiveSupport::Concern
|
8
|
+
|
9
|
+
class << self
|
10
|
+
attr_accessor :active
|
11
|
+
end
|
12
|
+
|
13
|
+
@active = true
|
14
|
+
|
15
|
+
included do
|
16
|
+
unless self.include? Mongoid::Document
|
17
|
+
raise RuntimeError.new('must be included in a Mongoid model')
|
18
|
+
end
|
19
|
+
|
20
|
+
field :published_flag, type: Boolean, default: false
|
21
|
+
field :publishing_date, type: Date, default: lambda { Date.today }
|
22
|
+
field :publishing_end_date, type: Date
|
23
|
+
|
24
|
+
scope :published, lambda {
|
25
|
+
if PublishingLogic.active?
|
26
|
+
where(
|
27
|
+
published_flag: true,
|
28
|
+
:$and => [
|
29
|
+
{:$or => [
|
30
|
+
{:publishing_date.exists => false},
|
31
|
+
{:publishing_date => nil},
|
32
|
+
{:publishing_date.lte => Date.today}
|
33
|
+
]},
|
34
|
+
{:$or => [
|
35
|
+
{publishing_end_date: nil},
|
36
|
+
{:publishing_end_date.gt => Date.today}
|
37
|
+
]}
|
38
|
+
]
|
39
|
+
)
|
40
|
+
else
|
41
|
+
all
|
42
|
+
end
|
43
|
+
}
|
44
|
+
|
45
|
+
scope :unpublished, lambda {
|
46
|
+
if PublishingLogic.active?
|
47
|
+
self.or(
|
48
|
+
{published_flag: false},
|
49
|
+
{:publishing_date.gt => Date.today},
|
50
|
+
{:publishing_end_date.lte => Date.today}
|
51
|
+
)
|
52
|
+
else
|
53
|
+
where(:_id.in => [])
|
54
|
+
end
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
def published?
|
59
|
+
if PublishingLogic.active?
|
60
|
+
published_flag && (
|
61
|
+
(publishing_date.nil? || publishing_date <= Date.today) &&
|
62
|
+
(publishing_end_date.nil? || publishing_end_date > Date.today)
|
63
|
+
)
|
64
|
+
else
|
65
|
+
true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
module ModuleMethods
|
70
|
+
def active?
|
71
|
+
self.active
|
72
|
+
end
|
73
|
+
|
74
|
+
def activate
|
75
|
+
self.active = true
|
76
|
+
end
|
77
|
+
|
78
|
+
def deactivate
|
79
|
+
self.active = false
|
80
|
+
end
|
81
|
+
|
82
|
+
def deactivated(&block)
|
83
|
+
with_status(false, &block)
|
84
|
+
end
|
85
|
+
|
86
|
+
def activated(&block)
|
87
|
+
with_status(true, &block)
|
88
|
+
end
|
89
|
+
|
90
|
+
def with_status(status, &block)
|
91
|
+
status_was = active?
|
92
|
+
begin
|
93
|
+
self.active = status
|
94
|
+
yield
|
95
|
+
ensure
|
96
|
+
self.active = status_was
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
extend self::ModuleMethods
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid/publishing_logic/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid-publishing_logic"
|
8
|
+
spec.version = Mongoid::PublishingLogic::VERSION
|
9
|
+
spec.authors = ["Joakim Reinert"]
|
10
|
+
spec.email = ["mail@jreinert.com"]
|
11
|
+
spec.summary = %q{A set of methods and scopes for publishing logic in mongoid models}
|
12
|
+
spec.description = %q{Basically a rewrite of codegourmet/mm-publishing-logic for mongoid. For more information visit https://github.com/codegourmet/mm-publishing-logic.}
|
13
|
+
spec.homepage = "https://github.com/jreinert/mongoid-publishing_logic"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "mongoid", "~> 5.1"
|
22
|
+
spec.add_dependency "activesupport", "~> 4.2"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "guard-rspec"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
spec.add_development_dependency "pry-doc"
|
30
|
+
spec.add_development_dependency "pry-byebug"
|
31
|
+
spec.add_development_dependency "simplecov"
|
32
|
+
spec.add_development_dependency "database_cleaner"
|
33
|
+
end
|
@@ -0,0 +1,286 @@
|
|
1
|
+
require 'mongoid/publishing_logic'
|
2
|
+
|
3
|
+
RSpec.shared_examples "publishing logic" do
|
4
|
+
let(:publishing_logic) { described_class }
|
5
|
+
|
6
|
+
let :model_class do
|
7
|
+
klass = Class.new {
|
8
|
+
include Mongoid::Document
|
9
|
+
store_in collection: 'test_models'
|
10
|
+
}
|
11
|
+
klass.send(:include, publishing_logic)
|
12
|
+
end
|
13
|
+
|
14
|
+
before :each do
|
15
|
+
Mongoid::PublishingLogic.active = true
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
it 'raises if included in a non-mongoid model/class' do
|
20
|
+
expect { Class.new.send(:include, publishing_logic) }.to raise_error(RuntimeError)
|
21
|
+
end
|
22
|
+
|
23
|
+
def generate_records
|
24
|
+
records = []
|
25
|
+
|
26
|
+
[true, false].each do |published_flag|
|
27
|
+
Array.new(3) {|index| Date.today + (index - 1) }.each do |publishing_date|
|
28
|
+
[nil, *Array.new(3) {|index| Date.today + (index - 1)}].each do |publishing_end_date|
|
29
|
+
model = model_class.create!(
|
30
|
+
published_flag: published_flag,
|
31
|
+
publishing_date: publishing_date,
|
32
|
+
publishing_end_date: publishing_end_date
|
33
|
+
)
|
34
|
+
records << model
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
records
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'has a published scope' do
|
43
|
+
expect(model_class).to respond_to(:published)
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'published scope' do
|
47
|
+
it 'only returns records, which have published_flag set to true' do
|
48
|
+
[true, false].each do |published_flag|
|
49
|
+
model_class.create!(published_flag: published_flag)
|
50
|
+
end
|
51
|
+
|
52
|
+
model_class.published.each do |model|
|
53
|
+
expect(model.published_flag).to eq true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'only returns records, with publishing_date in the past or today' do
|
58
|
+
Array.new(3) {|index| Date.today + (index - 1)}.each do |publishing_date|
|
59
|
+
model_class.create!(published_flag: true, publishing_date: publishing_date)
|
60
|
+
end
|
61
|
+
|
62
|
+
model_class.published.each do |model|
|
63
|
+
expect(model.publishing_date).to be <= Date.today
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'only returns records, with publishing_end_date nil or greater than today' do
|
68
|
+
[nil, *Array.new(3) {|index| Date.today + (index - 1)}].each do |publishing_end_date|
|
69
|
+
model_class.create!(published_flag: true, publishing_end_date: publishing_end_date)
|
70
|
+
end
|
71
|
+
|
72
|
+
model_class.published.each do |model|
|
73
|
+
unless model.publishing_end_date.nil?
|
74
|
+
expect(model.publishing_end_date).to be > Date.today
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
it "returns all records, with published_flag set to true,\n\t" +
|
80
|
+
"publishing_date in the past or today and\n\t" +
|
81
|
+
"publishing_end_date nil or greater than today" do
|
82
|
+
|
83
|
+
records = generate_records
|
84
|
+
expected_records = records.select {|record|
|
85
|
+
record.published_flag &&
|
86
|
+
record.publishing_date <= Date.today &&
|
87
|
+
(record.publishing_end_date.nil? || record.publishing_end_date > Date.today)
|
88
|
+
}
|
89
|
+
|
90
|
+
expect(model_class.published).to match_array expected_records
|
91
|
+
end
|
92
|
+
|
93
|
+
it "returns all records if the global active flag is set to false" do
|
94
|
+
Mongoid::PublishingLogic::active = false
|
95
|
+
records = generate_records
|
96
|
+
|
97
|
+
expect(model_class.published).to match_array records
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'has an unpublished scope' do
|
102
|
+
expect(model_class).to respond_to(:unpublished)
|
103
|
+
end
|
104
|
+
|
105
|
+
describe 'unpublished scope' do
|
106
|
+
it "returns all records, with published_flag set to false\n\t" +
|
107
|
+
"or publishing_date in the future\n\t" +
|
108
|
+
"or publishing_end_date today or in the past" do
|
109
|
+
|
110
|
+
records = generate_records
|
111
|
+
expected_records = records.select {|record|
|
112
|
+
!record.published_flag ||
|
113
|
+
record.publishing_date > Date.today ||
|
114
|
+
(!record.publishing_end_date.nil? && record.publishing_end_date <= Date.today)
|
115
|
+
}
|
116
|
+
|
117
|
+
expect(model_class.unpublished).to match_array expected_records
|
118
|
+
end
|
119
|
+
|
120
|
+
it "returns an empty query object if the global active flag is set to false" do
|
121
|
+
Mongoid::PublishingLogic::active = false
|
122
|
+
generate_records
|
123
|
+
|
124
|
+
expect(model_class.unpublished).to match_array []
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe 'instance' do
|
129
|
+
let :model do
|
130
|
+
model_class.new
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'has a published flag' do
|
134
|
+
expect(model).to respond_to(:published_flag)
|
135
|
+
end
|
136
|
+
|
137
|
+
describe 'published_flag' do
|
138
|
+
it 'is false by default' do
|
139
|
+
expect(model.published_flag).to eq false
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'has a publishing date' do
|
144
|
+
expect(model).to respond_to(:publishing_date)
|
145
|
+
end
|
146
|
+
|
147
|
+
describe 'publishing_date' do
|
148
|
+
it "is today's date by default" do
|
149
|
+
expect(model.publishing_date).to eq Date.today
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'has a publishing end date' do
|
154
|
+
expect(model).to respond_to(:publishing_end_date)
|
155
|
+
end
|
156
|
+
|
157
|
+
describe 'publishing_end_date' do
|
158
|
+
it 'is nil by default' do
|
159
|
+
expect(model.publishing_end_date).to be nil
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe 'published?' do
|
164
|
+
it "returns true if the published_flag is set to true,\n\t" +
|
165
|
+
"publishing_date is today or in the past and\n\t" +
|
166
|
+
"publishing_end_date is in the future" do
|
167
|
+
records = generate_records
|
168
|
+
expected_records = records.select {|record|
|
169
|
+
record.published_flag &&
|
170
|
+
record.publishing_date <= Date.today &&
|
171
|
+
(record.publishing_end_date.nil? || record.publishing_end_date > Date.today)
|
172
|
+
}
|
173
|
+
expect(records.select(&:published?)).to match_array expected_records
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'always returns true if the global active attribute is set to false' do
|
177
|
+
Mongoid::PublishingLogic.active = false
|
178
|
+
records = generate_records
|
179
|
+
|
180
|
+
expect(records.select(&:published?)).to match_array records
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
module Mongoid
|
187
|
+
describe PublishingLogic do
|
188
|
+
it_behaves_like('publishing logic')
|
189
|
+
|
190
|
+
it 'has a writer for the attribute active' do
|
191
|
+
expect(PublishingLogic).to respond_to(:active=)
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'has an active? method which returns the current state of the active attribute' do
|
195
|
+
expect(PublishingLogic).to respond_to(:active?)
|
196
|
+
PublishingLogic.active = false
|
197
|
+
expect(PublishingLogic.active?).to be false
|
198
|
+
PublishingLogic.active = true
|
199
|
+
expect(PublishingLogic.active?).to be true
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'has activate and deactivate methods to set active attribute' do
|
203
|
+
expect(PublishingLogic).to respond_to(:activate)
|
204
|
+
expect(PublishingLogic).to respond_to(:deactivate)
|
205
|
+
|
206
|
+
expect(PublishingLogic.active?).to be true
|
207
|
+
PublishingLogic.deactivate
|
208
|
+
expect(PublishingLogic.active?).to eq false
|
209
|
+
PublishingLogic.activate
|
210
|
+
expect(PublishingLogic.active?).to eq true
|
211
|
+
end
|
212
|
+
|
213
|
+
describe '.with_status' do
|
214
|
+
it 'expects a value to set the active attribute to' do
|
215
|
+
expect { PublishingLogic.with_status }.to raise_error(ArgumentError)
|
216
|
+
end
|
217
|
+
|
218
|
+
it 'expects a block' do
|
219
|
+
expect { PublishingLogic.with_status(true) }.to raise_error(LocalJumpError)
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'sets the publishing logic active attribute to whatever value is passed to it in the codeblock' do
|
223
|
+
[true, false].each do |initial_status|
|
224
|
+
PublishingLogic.active = initial_status
|
225
|
+
|
226
|
+
[true, false].each do |status|
|
227
|
+
PublishingLogic.with_status(status) do
|
228
|
+
expect(PublishingLogic.active?).to be status
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
it 'sets the publishing logic active attribute to its previous value outside the codeblock' do
|
235
|
+
[true, false].each do |initial_status|
|
236
|
+
PublishingLogic.active = initial_status
|
237
|
+
|
238
|
+
[true, false].each do |status|
|
239
|
+
PublishingLogic.with_status(status) {}
|
240
|
+
end
|
241
|
+
expect(PublishingLogic.active?).to be initial_status
|
242
|
+
|
243
|
+
begin
|
244
|
+
PublishingLogic.with_status(status) { raise Class.new(StandardError).new }
|
245
|
+
rescue
|
246
|
+
expect(PublishingLogic.active?).to eq initial_status
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
{deactivated: false, activated: true}.each do |method, expected_value|
|
253
|
+
describe ".#{method}" do
|
254
|
+
it 'expects a block' do
|
255
|
+
expect { PublishingLogic.send(method) }.to raise_error(LocalJumpError)
|
256
|
+
end
|
257
|
+
|
258
|
+
it "turns #{expected_value ? 'on' : 'off'} publishing logic for the codeblock" do
|
259
|
+
[true, false].each do |initial_status|
|
260
|
+
PublishingLogic.active = initial_status
|
261
|
+
|
262
|
+
PublishingLogic.send(method) do
|
263
|
+
expect(PublishingLogic.active?).to eq expected_value
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'sets the active attribute to what it was before the codeblock' do
|
269
|
+
[true, false].each do |initial_status|
|
270
|
+
PublishingLogic.active = initial_status
|
271
|
+
|
272
|
+
PublishingLogic.send(method) {}
|
273
|
+
|
274
|
+
expect(PublishingLogic.active?).to eq initial_status
|
275
|
+
|
276
|
+
begin
|
277
|
+
PublishingLogic.deactivated { raise Class.new(StandardError).new }
|
278
|
+
rescue
|
279
|
+
expect(PublishingLogic.active?).to eq initial_status
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
data/spec/mongoid.yml
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
6
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
7
|
+
#
|
8
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
9
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
10
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
11
|
+
# individual file that may not need all of that loaded. Instead, make a
|
12
|
+
# separate helper file that requires this one and then use it only in the specs
|
13
|
+
# that actually need it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
require 'database_cleaner'
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
# The settings below are suggested to provide a good initial experience
|
23
|
+
# with RSpec, but feel free to customize to your heart's content.
|
24
|
+
config.before(:suite) do
|
25
|
+
Mongoid.load!("spec/mongoid.yml", :test)
|
26
|
+
DatabaseCleaner[:mongoid].strategy = :truncation
|
27
|
+
end
|
28
|
+
config.around(:each) do |example|
|
29
|
+
DatabaseCleaner[:mongoid].cleaning do
|
30
|
+
example.run
|
31
|
+
end
|
32
|
+
end
|
33
|
+
=begin
|
34
|
+
# These two settings work together to allow you to limit a spec run
|
35
|
+
# to individual examples or groups you care about by tagging them with
|
36
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
37
|
+
# get run.
|
38
|
+
config.filter_run :focus
|
39
|
+
config.run_all_when_everything_filtered = true
|
40
|
+
|
41
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
42
|
+
# file, and it's useful to allow more verbose output when running an
|
43
|
+
# individual spec file.
|
44
|
+
if config.files_to_run.one?
|
45
|
+
# Use the documentation formatter for detailed output,
|
46
|
+
# unless a formatter has already been configured
|
47
|
+
# (e.g. via a command-line flag).
|
48
|
+
config.default_formatter = 'doc'
|
49
|
+
end
|
50
|
+
|
51
|
+
# Print the 10 slowest examples and example groups at the
|
52
|
+
# end of the spec run, to help surface which specs are running
|
53
|
+
# particularly slow.
|
54
|
+
config.profile_examples = 10
|
55
|
+
|
56
|
+
# Run specs in random order to surface order dependencies. If you find an
|
57
|
+
# order dependency and want to debug it, you can fix the order by providing
|
58
|
+
# the seed, which is printed after each run.
|
59
|
+
# --seed 1234
|
60
|
+
config.order = :random
|
61
|
+
|
62
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
63
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
64
|
+
# test failures related to randomization by passing the same `--seed` value
|
65
|
+
# as the one that triggered the failure.
|
66
|
+
Kernel.srand config.seed
|
67
|
+
|
68
|
+
# rspec-expectations config goes here. You can use an alternate
|
69
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
70
|
+
# assertions if you prefer.
|
71
|
+
config.expect_with :rspec do |expectations|
|
72
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
73
|
+
# For more details, see:
|
74
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
75
|
+
expectations.syntax = :expect
|
76
|
+
end
|
77
|
+
|
78
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
79
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
80
|
+
config.mock_with :rspec do |mocks|
|
81
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
82
|
+
# For more details, see:
|
83
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
84
|
+
mocks.syntax = :expect
|
85
|
+
|
86
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
87
|
+
# a real object. This is generally recommended.
|
88
|
+
mocks.verify_partial_doubles = true
|
89
|
+
end
|
90
|
+
=end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-publishing_logic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joakim Reinert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-doc
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pry-byebug
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: simplecov
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: database_cleaner
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
description: Basically a rewrite of codegourmet/mm-publishing-logic for mongoid. For
|
168
|
+
more information visit https://github.com/codegourmet/mm-publishing-logic.
|
169
|
+
email:
|
170
|
+
- mail@jreinert.com
|
171
|
+
executables: []
|
172
|
+
extensions: []
|
173
|
+
extra_rdoc_files: []
|
174
|
+
files:
|
175
|
+
- ".gitignore"
|
176
|
+
- ".rspec"
|
177
|
+
- Gemfile
|
178
|
+
- Guardfile
|
179
|
+
- LICENSE.txt
|
180
|
+
- README.md
|
181
|
+
- Rakefile
|
182
|
+
- lib/mongoid/publishing_logic.rb
|
183
|
+
- lib/mongoid/publishing_logic/localized.rb
|
184
|
+
- lib/mongoid/publishing_logic/version.rb
|
185
|
+
- mongoid-publishing_logic.gemspec
|
186
|
+
- spec/lib/mongoid/publishing_logic/localized_spec.rb
|
187
|
+
- spec/lib/mongoid/publishing_logic_spec.rb
|
188
|
+
- spec/mongoid.yml
|
189
|
+
- spec/spec_helper.rb
|
190
|
+
homepage: https://github.com/jreinert/mongoid-publishing_logic
|
191
|
+
licenses:
|
192
|
+
- MIT
|
193
|
+
metadata: {}
|
194
|
+
post_install_message:
|
195
|
+
rdoc_options: []
|
196
|
+
require_paths:
|
197
|
+
- lib
|
198
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
199
|
+
requirements:
|
200
|
+
- - ">="
|
201
|
+
- !ruby/object:Gem::Version
|
202
|
+
version: '0'
|
203
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
204
|
+
requirements:
|
205
|
+
- - ">="
|
206
|
+
- !ruby/object:Gem::Version
|
207
|
+
version: '0'
|
208
|
+
requirements: []
|
209
|
+
rubyforge_project:
|
210
|
+
rubygems_version: 2.5.1
|
211
|
+
signing_key:
|
212
|
+
specification_version: 4
|
213
|
+
summary: A set of methods and scopes for publishing logic in mongoid models
|
214
|
+
test_files:
|
215
|
+
- spec/lib/mongoid/publishing_logic/localized_spec.rb
|
216
|
+
- spec/lib/mongoid/publishing_logic_spec.rb
|
217
|
+
- spec/mongoid.yml
|
218
|
+
- spec/spec_helper.rb
|
219
|
+
has_rdoc:
|