mongoid-publishable 0.2.1 → 0.3.1

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/Gemfile CHANGED
@@ -2,3 +2,10 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in mongoid_publishable.gemspec
4
4
  gemspec
5
+
6
+ case RUBY_PLATFORM
7
+ when /darwin/
8
+ gem "rb-fsevent", require: false
9
+ when /linux/
10
+ gem "rb-inotify", require: false
11
+ end
@@ -0,0 +1,5 @@
1
+ guard "rspec" do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch("spec/spec_helper.rb") { "spec" }
5
+ end
data/README.md CHANGED
@@ -2,10 +2,12 @@
2
2
 
3
3
  Ever wanted to allow your users to create something (or somethings) before authenticating. For example, you might want to let them write a review, before you ask them to login and publish it. This is what Mongoid::Publishable handles.
4
4
 
5
- [![Build Status][2]][1]
5
+ [![Build Status][2]][1] [![Code Climate][3]][4]
6
6
 
7
7
  [1]: http://travis-ci.org/ryantownsend/mongoid-publishable
8
8
  [2]: https://secure.travis-ci.org/ryantownsend/mongoid-publishable.png?branch=master
9
+ [3]: https://codeclimate.com/badge.png
10
+ [4]: https://codeclimate.com/github/ryantownsend/mongoid-publishable
9
11
 
10
12
  ## Installation
11
13
 
@@ -168,6 +170,19 @@ class Review
168
170
  end
169
171
  ```
170
172
 
173
+ You can define custom publishing requirements:
174
+
175
+ ```ruby
176
+ class Tweet
177
+ include Mongoid::Document
178
+ include Mongoid::Publishable
179
+
180
+ publishing_conditions do |tweet|
181
+ tweet.user.has_twitter_account?
182
+ end # => should return true / false
183
+ end
184
+ ```
185
+
171
186
  ## Contributing
172
187
 
173
188
  1. Fork it
@@ -177,4 +192,8 @@ end
177
192
  5. Push to the branch (`git push origin my-new-feature`)
178
193
  6. Create new Pull Request
179
194
 
180
- After running specs, you can find a test coverage report at coverage/index.html
195
+ After running specs, you can find a test coverage report at `coverage/index.html`.
196
+
197
+ Note: you can run the test suite automatically by starting Guard, do this with the following command:
198
+
199
+ $ bundle exec guard start
data/Rakefile CHANGED
@@ -9,4 +9,4 @@ desc "Run specs"
9
9
  RSpec::Core::RakeTask.new do |t|
10
10
  t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
11
11
  # Put spec opts in a file named .rspec in root
12
- end
12
+ end
@@ -22,10 +22,20 @@ module Mongoid
22
22
  @publisher_column || :user_id
23
23
  end
24
24
 
25
+ # gets/sets the foreign key of the publisher to be stored
25
26
  def publisher_foreign_key(name = nil)
26
27
  @publisher_foreign_key = name if name
27
28
  @publisher_foreign_key || :id
28
29
  end
30
+
31
+ # gets/sets custom publishing conditions
32
+ def publishing_conditions(&block)
33
+ if block_given?
34
+ @publishing_conditions = block
35
+ else
36
+ @publishing_conditions
37
+ end
38
+ end
29
39
  end
30
40
 
31
41
  module InstanceMethods
@@ -68,8 +78,11 @@ module Mongoid
68
78
  value = publisher.send(publisher_foreign_key)
69
79
  # update this instance with the key
70
80
  self.send("#{publisher_column}=", value)
71
- # mark as just published
72
- run_after_publish_callbacks
81
+ # if this now counts as published
82
+ if pre_published?
83
+ # mark as just published
84
+ run_after_publish_callbacks
85
+ end
73
86
  end
74
87
  end
75
88
 
@@ -81,12 +94,33 @@ module Mongoid
81
94
 
82
95
  # returns boolean of whether this instance has been published
83
96
  def published?
84
- persisted? && send(publisher_column)
97
+ persisted? && has_publisher_id? && meets_custom_publishing_conditions?
98
+ end
99
+
100
+ # returns boolean of whether this instance has been published
101
+ # regardless of whether it's been persisted yet
102
+ def pre_published?
103
+ has_publisher_id? && meets_custom_publishing_conditions?
85
104
  end
86
105
 
87
106
  # returns whether this instance needs publishing (persisted, not published)
88
107
  def requires_publishing?
89
- persisted? && !send(publisher_column)
108
+ persisted? && (!has_publisher_id? || !meets_custom_publishing_conditions?)
109
+ end
110
+
111
+ # returns whether or not the publisher is present
112
+ def has_publisher_id?
113
+ !!send(publisher_column)
114
+ end
115
+
116
+ # returns true if there are no conditions, or the resulting yield is true
117
+ def meets_custom_publishing_conditions?
118
+ publishing_conditions.nil? || publishing_conditions.yield(self)
119
+ end
120
+
121
+ # returns a block with custom publishing conditions
122
+ def publishing_conditions
123
+ self.class.publishing_conditions
90
124
  end
91
125
 
92
126
  # raises an UnpublishedError containing this object as a reference
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Publishable
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
@@ -24,4 +24,5 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency "rspec"
25
25
  s.add_development_dependency "foreman"
26
26
  s.add_development_dependency "simplecov"
27
+ s.add_development_dependency "guard-rspec"
27
28
  end
@@ -183,6 +183,30 @@ describe Mongoid::Publishable do
183
183
  end
184
184
  end
185
185
  end
186
+
187
+ describe "#meets_custom_publishing_conditions?" do
188
+ context "without custom publishing conditions" do
189
+ before(:each) do
190
+ subject.stub(:publishing_conditions).and_return(nil)
191
+ end
192
+
193
+ it "should return true" do
194
+ expect(subject.meets_custom_publishing_conditions?).to be_true
195
+ end
196
+ end
197
+
198
+ context "with custom publishing conditions" do
199
+ before(:each) do
200
+ subject.class.publishing_conditions do |object|
201
+ !!object.user_id
202
+ end
203
+ end
204
+
205
+ it "should return false" do
206
+ expect(subject.meets_custom_publishing_conditions?).to be_false
207
+ end
208
+ end
209
+ end
186
210
 
187
211
  describe "#requires_publishing?" do
188
212
  context "when persisted" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-publishable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-28 00:00:00.000000000 Z
12
+ date: 2012-10-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: guard-rspec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
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
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
110
126
  description: A mixin for Mongoid document models allowing for publishing them after
111
127
  authentication
112
128
  email:
@@ -119,6 +135,7 @@ files:
119
135
  - .rvmrc
120
136
  - .travis.yml
121
137
  - Gemfile
138
+ - Guardfile
122
139
  - LICENSE.txt
123
140
  - Procfile
124
141
  - README.md
@@ -166,7 +183,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
166
183
  version: '0'
167
184
  segments:
168
185
  - 0
169
- hash: -2933550559147298580
186
+ hash: 4223141326906407503
170
187
  required_rubygems_version: !ruby/object:Gem::Requirement
171
188
  none: false
172
189
  requirements:
@@ -175,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
192
  version: '0'
176
193
  segments:
177
194
  - 0
178
- hash: -2933550559147298580
195
+ hash: 4223141326906407503
179
196
  requirements: []
180
197
  rubyforge_project:
181
198
  rubygems_version: 1.8.24