mongoid-publishable 0.0.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.
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+ require "mongoid/publishable/queuing"
3
+
4
+ describe Mongoid::Publishable::Queuing do
5
+ subject do
6
+ QueuingController.new
7
+ end
8
+
9
+ describe "#deserialize_publishing_queue" do
10
+ it "should make the queue accessible via #publishing_queue" do
11
+ subject.send(:deserialize_publishing_queue)
12
+ expect(subject.publishing_queue).to respond_to :<<
13
+ end
14
+ end
15
+
16
+ describe "#serialize_publishing_queue" do
17
+ it "should dump the data data to the session" do
18
+ data = { :one => "two" }
19
+ subject.send(:deserialize_publishing_queue)
20
+ subject.publishing_queue.should_receive(:dump).and_return(data)
21
+ subject.send(:serialize_publishing_queue)
22
+ expect(subject.session[:publishing_queue]).to eq data
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,133 @@
1
+ require "spec_helper"
2
+ require "multi_json"
3
+ require "mongoid/publishable/unpublished_object"
4
+
5
+ describe Mongoid::Publishable::UnpublishedObject do
6
+ let(:model) { PublishableObject.new }
7
+
8
+ describe "::deserialize_from_session" do
9
+ it "should initialize with data" do
10
+ subject = Mongoid::Publishable::UnpublishedObject.deserialize_from_session("data")
11
+ expect(subject).to be_kind_of Mongoid::Publishable::UnpublishedObject
12
+ end
13
+ end
14
+
15
+ describe "::initialize" do
16
+ subject do
17
+ Mongoid::Publishable::UnpublishedObject.new(params)
18
+ end
19
+ let(:params) { Hash.new }
20
+
21
+ context "with a model" do
22
+ let(:params) do
23
+ { model: model }
24
+ end
25
+
26
+ it "should not raise an error" do
27
+ expect { subject }.not_to raise_error
28
+ end
29
+ end
30
+
31
+ context "with data" do
32
+ let(:params) do
33
+ { data: %Q({ class_name: "Item", id: 123 }) }
34
+ end
35
+
36
+ it "should not raise an error" do
37
+ expect { subject }.not_to raise_error
38
+ end
39
+ end
40
+
41
+ context "with no model or data" do
42
+ it "should raise an argument error" do
43
+ expect { subject }.to raise_error(ArgumentError)
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "#serialize_for_session" do
49
+ context "with a model" do
50
+ subject do
51
+ Mongoid::Publishable::UnpublishedObject.new(model: model)
52
+ end
53
+
54
+ it "should return a string of JSON" do
55
+ json = MultiJson.load(subject.serialize_for_session)
56
+ expect(json["class_name"]).to eq model.class.name
57
+ expect(json["id"].to_s).to eq model.id.to_s
58
+ end
59
+ end
60
+
61
+ context "with data" do
62
+ subject do
63
+ data = %Q({ "class_name": "Item", "id": "123" })
64
+ Mongoid::Publishable::UnpublishedObject.new(data: data)
65
+ end
66
+
67
+ it "should return a string of JSON" do
68
+ json = MultiJson.load(subject.serialize_for_session)
69
+ expect(json["class_name"]).to eq "Item"
70
+ expect(json["id"].to_s).to eq "123"
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "#params" do
76
+ context "with data" do
77
+ subject do
78
+ data = %Q({ "class_name": "Item", "id": "123" })
79
+ Mongoid::Publishable::UnpublishedObject.new(data: data).params
80
+ end
81
+
82
+ it "should return a string of JSON" do
83
+ expect(subject["class_name"]).to eq "Item"
84
+ expect(subject["id"].to_s).to eq "123"
85
+ end
86
+ end
87
+ end
88
+
89
+ describe "#respond_to_missing?" do
90
+ subject do
91
+ Mongoid::Publishable::UnpublishedObject.new(model: model)
92
+ end
93
+
94
+ it "should return true if the model accepts the method" do
95
+ expect(subject.respond_to_missing?(:id)).to be_true
96
+ end
97
+ end
98
+
99
+ describe "#method_missing" do
100
+ subject do
101
+ Mongoid::Publishable::UnpublishedObject.new(model: model)
102
+ end
103
+
104
+ it "should delegate to the model" do
105
+ expect(subject.id).to eq model.id
106
+ end
107
+ end
108
+
109
+ describe "#source_object" do
110
+ context "with data" do
111
+ subject do
112
+ data = %Q({ "class_name": "PublishableObject", "id": "123" })
113
+ Mongoid::Publishable::UnpublishedObject.new(data: data)
114
+ end
115
+
116
+ it "should find the model" do
117
+ model = mock("model")
118
+ PublishableObject.should_receive(:find).with("123").and_return(model)
119
+ expect(subject.source_object).to eq model
120
+ end
121
+ end
122
+
123
+ context "with a model" do
124
+ subject do
125
+ Mongoid::Publishable::UnpublishedObject.new(model: model)
126
+ end
127
+
128
+ it "should return the model" do
129
+ expect(subject.source_object).to eq model
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,215 @@
1
+ require "spec_helper"
2
+ require "mongoid/publishable"
3
+
4
+ describe Mongoid::Publishable do
5
+ let(:user) { mock("user", id: 123) }
6
+
7
+ describe "a class that includes the module" do
8
+ subject { PublishableObject }
9
+
10
+ before(:each) do
11
+ @publisher_column = subject.publisher_column
12
+ @publisher_foreign_key = subject.publisher_foreign_key
13
+ end
14
+
15
+ after(:each) do
16
+ subject.publisher_column @publisher_column
17
+ subject.publisher_foreign_key @publisher_foreign_key
18
+ end
19
+
20
+ it "should include Mongoid::Publishable::Callbacks" do
21
+ expect(subject.included_modules).to include Mongoid::Publishable::Callbacks
22
+ end
23
+
24
+ describe "::publisher_column" do
25
+ it "should return :user_id by default" do
26
+ expect(subject.publisher_column).to eq :user_id
27
+ end
28
+
29
+ it "should be overridable" do
30
+ subject.publisher_column :author_id
31
+ expect(subject.publisher_column).to eq :author_id
32
+ subject.publisher_column :user_id
33
+ end
34
+ end
35
+
36
+ describe "::publisher_foreign_key" do
37
+ it "should return :id by default" do
38
+ expect(subject.publisher_foreign_key).to eq :id
39
+ end
40
+
41
+ it "should be overridable" do
42
+ subject.publisher_foreign_key :username
43
+ expect(subject.publisher_foreign_key).to eq :username
44
+ subject.publisher_column :user_id
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "an instance of a class that includes the module" do
50
+ subject { PublishableObject.new }
51
+
52
+ describe "#publisher_column" do
53
+ it "should return :user_id by default" do
54
+ expect(subject.publisher_column).to eq :user_id
55
+ end
56
+ end
57
+
58
+ describe "#publisher_column=" do
59
+ it "should be override #publisher_column" do
60
+ subject.publisher_column = :author_id
61
+ expect(subject.publisher_column).to eq :author_id
62
+ end
63
+ end
64
+
65
+ describe "#publisher_foreign_key" do
66
+ it "should return :id by default" do
67
+ expect(subject.publisher_foreign_key).to eq :id
68
+ end
69
+ end
70
+
71
+ describe "#publisher_foreign_key=" do
72
+ it "should be override #publisher_foreign_key" do
73
+ subject.publisher_foreign_key = :username
74
+ expect(subject.publisher_foreign_key).to eq :username
75
+ end
76
+ end
77
+
78
+ describe "#persist_and_publish" do
79
+ context "when persisted" do
80
+ before(:each) do
81
+ subject.stub(:save).and_return(true)
82
+ end
83
+
84
+ context "without a publisher" do
85
+ it "should return false" do
86
+ expect(subject.persist_and_publish).to be_false
87
+ end
88
+ end
89
+
90
+ context "with a publisher" do
91
+ it "should return true" do
92
+ expect(subject.persist_and_publish(user)).to be_true
93
+ end
94
+ end
95
+ end
96
+
97
+ context "when not persisted" do
98
+ before(:each) do
99
+ subject.stub(:save).and_return(false)
100
+ end
101
+
102
+ it "should return false" do
103
+ expect(subject.persist_and_publish(user)).to be_false
104
+ end
105
+
106
+ it "should still update the publisher" do
107
+ subject.persist_and_publish(user)
108
+ expect(subject.user_id).to eq user.id
109
+ end
110
+ end
111
+ end
112
+
113
+ describe "#persist_and_publish!" do
114
+ context "when persisted" do
115
+ before(:each) do
116
+ subject.stub(:persisted?).and_return(true)
117
+ subject.stub(:save).and_return(true)
118
+ end
119
+
120
+ context "but not published" do
121
+ it "should raise an exception" do
122
+ expect {
123
+ subject.persist_and_publish!(nil)
124
+ }.to raise_error(Mongoid::Publishable::UnpublishedError)
125
+ end
126
+ end
127
+
128
+ context "and published" do
129
+ it "should return true" do
130
+ expect(subject.persist_and_publish!(user)).to be_true
131
+ end
132
+ end
133
+ end
134
+
135
+ context "when it fails validation" do
136
+ before(:each) do
137
+ subject.stub(:save).and_return(false)
138
+ end
139
+
140
+ it "should return false" do
141
+ expect(subject.persist_and_publish!(user)).to be_false
142
+ end
143
+ end
144
+ end
145
+
146
+ describe "#publish_via" do
147
+ it "should set the publisher on the object" do
148
+ subject.publish_via(user)
149
+ expect(subject.user_id).to eq user.id
150
+ end
151
+ end
152
+
153
+ describe "#publish_via!" do
154
+ it "should set the publisher on the object and persist" do
155
+ subject.should_receive(:save).and_return(true)
156
+ expect(subject.publish_via!(user)).to be_true
157
+ expect(subject.user_id).to eq user.id
158
+ end
159
+ end
160
+
161
+ describe "#published?" do
162
+ context "when not persisted" do
163
+ it "should return false" do
164
+ expect(subject.published?).to be_false
165
+ end
166
+ end
167
+
168
+ context "when persisted" do
169
+ before(:each) do
170
+ subject.stub(:persisted?).and_return(true)
171
+ end
172
+
173
+ context "without a publisher" do
174
+ it "should return false" do
175
+ expect(subject.published?).to be_false
176
+ end
177
+ end
178
+
179
+ context "with a publisher" do
180
+ it "should return true" do
181
+ subject.publish_via(user)
182
+ expect(subject.published?).to be_true
183
+ end
184
+ end
185
+ end
186
+ end
187
+
188
+ describe "#requires_publishing?" do
189
+ context "when persisted" do
190
+ before(:each) do
191
+ subject.stub(:persisted?).and_return(true)
192
+ end
193
+
194
+ context "and the publisher is set" do
195
+ it "should return false" do
196
+ subject.publish_via(user)
197
+ expect(subject.requires_publishing?).to be_false
198
+ end
199
+ end
200
+
201
+ context "and the publisher is not set" do
202
+ it "should return true" do
203
+ expect(subject.requires_publishing?).to be_true
204
+ end
205
+ end
206
+ end
207
+
208
+ context "when not persisted" do
209
+ it "should return false" do
210
+ expect(subject.requires_publishing?).to be_false
211
+ end
212
+ end
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,11 @@
1
+ require "simplecov"
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ require "mongoid"
7
+
8
+ lib = File.expand_path("../../lib", __FILE__)
9
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
10
+
11
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f }
@@ -0,0 +1,8 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |c|
3
+ c.syntax = :expect
4
+ end
5
+
6
+ config.color_enabled = true
7
+ config.order = :random
8
+ end
@@ -0,0 +1,6 @@
1
+ require "mongoid/publishable/callbacks"
2
+
3
+ class CallbackableObject
4
+ def self.after_save(*args); end
5
+ include Mongoid::Publishable::Callbacks
6
+ end
@@ -0,0 +1,8 @@
1
+ require "mongoid/publishable"
2
+
3
+ class PublishableObject
4
+ include Mongoid::Document
5
+ include Mongoid::Publishable
6
+
7
+ belongs_to :user, class_name: "Publisher"
8
+ end
@@ -0,0 +1,3 @@
1
+ class Publisher
2
+ include Mongoid::Document
3
+ end
@@ -0,0 +1,8 @@
1
+ require "mongoid/publishable/queuing"
2
+
3
+ class QueuingController
4
+ def self.before_filter(*args); end
5
+ def self.after_filter(*args); end
6
+ def session; @session ||= {}; end
7
+ include Mongoid::Publishable::Queuing
8
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-publishable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Townsend
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: multi_json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: A mixin for Mongoid document models allowing for publishing them after
95
+ authentication
96
+ email:
97
+ - ryan@ryantownsend.co.uk
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - .rvmrc
104
+ - .travis.yml
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/mongoid/.DS_Store
110
+ - lib/mongoid/publishable.rb
111
+ - lib/mongoid/publishable/callback.rb
112
+ - lib/mongoid/publishable/callback_collection.rb
113
+ - lib/mongoid/publishable/callbacks.rb
114
+ - lib/mongoid/publishable/queue.rb
115
+ - lib/mongoid/publishable/queuing.rb
116
+ - lib/mongoid/publishable/unpublished_error.rb
117
+ - lib/mongoid/publishable/unpublished_object.rb
118
+ - lib/mongoid/publishable/version.rb
119
+ - lib/mongoid_publishable.rb
120
+ - mongoid_publishable.gemspec
121
+ - spec/.DS_Store
122
+ - spec/mongoid/publishable/callback_collection_spec.rb
123
+ - spec/mongoid/publishable/callback_spec.rb
124
+ - spec/mongoid/publishable/callbacks_spec.rb
125
+ - spec/mongoid/publishable/queue_spec.rb
126
+ - spec/mongoid/publishable/queuing_spec.rb
127
+ - spec/mongoid/publishable/unpublished_object_spec.rb
128
+ - spec/mongoid/publishable_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/support/env.rb
131
+ - spec/support/models/callbackable_object.rb
132
+ - spec/support/models/publishable_object.rb
133
+ - spec/support/models/publisher.rb
134
+ - spec/support/models/queuing_controller.rb
135
+ homepage: https://github.com/ryantownsend/mongoid-publishable
136
+ licenses: []
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 1.8.23
156
+ signing_key:
157
+ specification_version: 3
158
+ summary: A mixin for Mongoid document models allowing for publishing them after authentication
159
+ test_files:
160
+ - spec/.DS_Store
161
+ - spec/mongoid/publishable/callback_collection_spec.rb
162
+ - spec/mongoid/publishable/callback_spec.rb
163
+ - spec/mongoid/publishable/callbacks_spec.rb
164
+ - spec/mongoid/publishable/queue_spec.rb
165
+ - spec/mongoid/publishable/queuing_spec.rb
166
+ - spec/mongoid/publishable/unpublished_object_spec.rb
167
+ - spec/mongoid/publishable_spec.rb
168
+ - spec/spec_helper.rb
169
+ - spec/support/env.rb
170
+ - spec/support/models/callbackable_object.rb
171
+ - spec/support/models/publishable_object.rb
172
+ - spec/support/models/publisher.rb
173
+ - spec/support/models/queuing_controller.rb