mongoid-publishable 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Procfile ADDED
@@ -0,0 +1 @@
1
+ mongo: mongod --config /usr/local/etc/mongod.conf
data/README.md CHANGED
@@ -12,7 +12,7 @@ Ever wanted to allow your users to create something (or somethings) before authe
12
12
  Add this line to your application's Gemfile:
13
13
 
14
14
  ```ruby
15
- gem "mongoid-publishable"
15
+ gem "mongoid-publishable", require: "mongoid/publishable"
16
16
  ```
17
17
 
18
18
  And then execute:
@@ -53,6 +53,8 @@ end
53
53
  In your controllers, or anywhere you save the objects, you can swap out your `save` calls for `persist_and_publish!` calls, this method accepts an optional user. If none is passed, or the object that you do pass is nil, it'll raise an exception, so you can handle your authentication there:
54
54
 
55
55
  ```ruby
56
+ require: "mongoid/publishable"
57
+
56
58
  class ReviewsController < ApplicationController
57
59
  include Mongoid::Publishable::Queuing
58
60
 
@@ -80,6 +82,8 @@ end
80
82
  An alternative without the exception handling would be:
81
83
 
82
84
  ```ruby
85
+ require: "mongoid/publishable"
86
+
83
87
  class ReviewsController < ApplicationController
84
88
  include Mongoid::Publishable::Queuing
85
89
 
@@ -108,6 +112,8 @@ end
108
112
  The advantage to the former style (using exceptions) is that you can handle them globally in your ApplicationController using this code:
109
113
 
110
114
  ```ruby
115
+ require: "mongoid/publishable"
116
+
111
117
  class ApplicationController < ActionController::Base
112
118
  include Mongoid::Publishable::Queuing
113
119
 
@@ -126,7 +132,11 @@ end
126
132
  The publishing queue is stored in the user's session. After authentication, you'll want to call `publish_via` on the queue, which will then publish all the objects it contains. Here's an example:
127
133
 
128
134
  ```ruby
135
+ require: "mongoid/publishable"
136
+
129
137
  class UserSessionsController < ApplicationController
138
+ include Mongoid::Publishable::Queuing
139
+
130
140
  def create
131
141
  @user = User.authenticate(params[:user])
132
142
  if @user
@@ -1,4 +1,4 @@
1
- require "multi_json"
1
+ require "mongoid/nested_serialization"
2
2
 
3
3
  module Mongoid
4
4
  module Publishable
@@ -17,18 +17,22 @@ module Mongoid
17
17
  end
18
18
  end
19
19
 
20
+ # returns the data needed for object retrieval
20
21
  def serialize_for_session
21
22
  @serialized_data ||= serialize_object_for_session
22
23
  end
23
24
 
24
- def params
25
- MultiJson.load(@serialized_data)
25
+ # returns the retrieved object
26
+ def source_object
27
+ @source_object ||= load_source_object_from_params
26
28
  end
27
29
 
30
+ # delegation to the source object
28
31
  def respond_to_missing?(method, include_private = false)
29
32
  source_object.respond_to?(method) || super
30
33
  end
31
34
 
35
+ # delegation to the source object
32
36
  def method_missing(method, *args, &block)
33
37
  if respond_to_missing?(method)
34
38
  source_object.send(method, *args, &block)
@@ -37,51 +41,13 @@ module Mongoid
37
41
  end
38
42
  end
39
43
 
40
- def source_object
41
- @source_object ||= load_source_object_from_params
42
- end
43
-
44
44
  private
45
45
  def load_source_object_from_params
46
- data = params
47
- # load the top level object
48
- object = data["class_name"].constantize.find(data["id"])
49
- # if we have embedded stuff
50
- while data["embedded"]
51
- # work on the next level down
52
- data = data["embedded"]
53
- # find the nested object
54
- object = object.send(data["association"]).find(data["id"])
55
- end
56
- # once at the bottom, return the object
57
- object
46
+ Mongoid::NestedSerialization::Finder.find(@serialized_data)
58
47
  end
59
48
 
60
49
  def serialize_object_for_session
61
- # start at the bottom
62
- object = source_object
63
- result = nil
64
- # work the way up the embeds
65
- while object.embedded?
66
- # select the relation for the parent object
67
- parent_relation = relations.select do |k,v|
68
- v.macro == :embedded_in && v.class_name == object._parent.class.name
69
- end.values.first
70
- # wrap the the result
71
- result = {
72
- embedded: {
73
- association: parent_relation.inverse_of,
74
- embedded: result && result[:embedded],
75
- id: object.id
76
- }
77
- }
78
- # now act on the parent
79
- object = object._parent
80
- end
81
- # when at the top level, store the class/id/result
82
- result = { class_name: object.class.name, id: object.id, embedded: result && result[:embedded] }
83
- # convert the result to JSON
84
- MultiJson.dump(result)
50
+ source_object.finder_json
85
51
  end
86
52
  end
87
53
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Publishable
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -19,8 +19,9 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_dependency "mongoid"
22
- s.add_dependency "multi_json"
22
+ s.add_dependency "mongoid-nested-serialization"
23
23
  s.add_development_dependency "rake"
24
24
  s.add_development_dependency "rspec"
25
+ s.add_development_dependency "foreman"
25
26
  s.add_development_dependency "simplecov"
26
27
  end
@@ -1,5 +1,4 @@
1
1
  require "spec_helper"
2
- require "multi_json"
3
2
  require "mongoid/publishable/unpublished_object"
4
3
 
5
4
  describe Mongoid::Publishable::UnpublishedObject do
@@ -57,12 +56,6 @@ describe Mongoid::Publishable::UnpublishedObject do
57
56
  Mongoid::Publishable::UnpublishedObject.new(model: model)
58
57
  end
59
58
 
60
- it "should return a string of JSON" do
61
- json = MultiJson.load(subject.serialize_for_session)
62
- expect(json["class_name"]).to eq model.class.name
63
- expect(json["id"].to_s).to eq model.id.to_s
64
- end
65
-
66
59
  it "should deserialise back into the object" do
67
60
  model.save
68
61
  data = subject.serialize_for_session
@@ -76,15 +69,6 @@ describe Mongoid::Publishable::UnpublishedObject do
76
69
  Mongoid::Publishable::UnpublishedObject.new(model: nested_model)
77
70
  end
78
71
 
79
- it "should return a string of JSON" do
80
- json = MultiJson.load(subject.serialize_for_session)
81
- expect(json["class_name"]).to eq nested_model._parent.class.name
82
- expect(json["id"].to_s).to eq nested_model._parent.id.to_s
83
- expect(json["embedded"]["id"]).to eq nested_model.id.to_s
84
- expect(json["embedded"]["association"]).to eq "nested_objects"
85
- expect(json["embedded"]["embedded"]).to be_nil
86
- end
87
-
88
72
  it "should deserialise back into the object" do
89
73
  nested_model.save
90
74
  data = subject.serialize_for_session
@@ -92,33 +76,6 @@ describe Mongoid::Publishable::UnpublishedObject do
92
76
  expect(reloaded_subject.source_object).to eq subject.source_object
93
77
  end
94
78
  end
95
-
96
- context "with data" do
97
- subject do
98
- data = %Q({ "class_name": "Item", "id": "123" })
99
- Mongoid::Publishable::UnpublishedObject.new(data: data)
100
- end
101
-
102
- it "should return a string of JSON" do
103
- json = MultiJson.load(subject.serialize_for_session)
104
- expect(json["class_name"]).to eq "Item"
105
- expect(json["id"].to_s).to eq "123"
106
- end
107
- end
108
- end
109
-
110
- describe "#params" do
111
- context "with data" do
112
- subject do
113
- data = %Q({ "class_name": "Item", "id": "123" })
114
- Mongoid::Publishable::UnpublishedObject.new(data: data).params
115
- end
116
-
117
- it "should return a string of JSON" do
118
- expect(subject["class_name"]).to eq "Item"
119
- expect(subject["id"].to_s).to eq "123"
120
- end
121
- end
122
79
  end
123
80
 
124
81
  describe "#respond_to_missing?" 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.0.4
4
+ version: 0.0.5
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-18 00:00:00.000000000 Z
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -28,7 +28,7 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: multi_json
31
+ name: mongoid-nested-serialization
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: foreman
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'
78
94
  - !ruby/object:Gem::Dependency
79
95
  name: simplecov
80
96
  requirement: !ruby/object:Gem::Requirement
@@ -104,6 +120,7 @@ files:
104
120
  - .travis.yml
105
121
  - Gemfile
106
122
  - LICENSE.txt
123
+ - Procfile
107
124
  - README.md
108
125
  - Rakefile
109
126
  - lib/mongoid/.DS_Store
@@ -148,15 +165,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
165
  - - ! '>='
149
166
  - !ruby/object:Gem::Version
150
167
  version: '0'
168
+ segments:
169
+ - 0
170
+ hash: -2209708699863841011
151
171
  required_rubygems_version: !ruby/object:Gem::Requirement
152
172
  none: false
153
173
  requirements:
154
174
  - - ! '>='
155
175
  - !ruby/object:Gem::Version
156
176
  version: '0'
177
+ segments:
178
+ - 0
179
+ hash: -2209708699863841011
157
180
  requirements: []
158
181
  rubyforge_project:
159
- rubygems_version: 1.8.23
182
+ rubygems_version: 1.8.24
160
183
  signing_key:
161
184
  specification_version: 3
162
185
  summary: A mixin for Mongoid document models allowing for publishing them after authentication