mongoid_alize 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongoid/alize/callback.rb +15 -14
- data/lib/mongoid/alize/callbacks/from/many.rb +1 -3
- data/lib/mongoid/alize/callbacks/from/one.rb +1 -3
- data/lib/mongoid/alize/from_callback.rb +2 -1
- data/lib/mongoid/alize/to_callback.rb +1 -5
- data/spec/app/models/person.rb +5 -0
- data/spec/mongoid/alize/callback_spec.rb +14 -14
- data/spec/mongoid/alize/callbacks/from/many_spec.rb +11 -10
- data/spec/mongoid/alize/callbacks/from/one_spec.rb +8 -6
- data/spec/mongoid/alize/callbacks/to/many_from_many_spec.rb +2 -2
- data/spec/mongoid/alize/callbacks/to/many_from_one_spec.rb +1 -1
- data/spec/mongoid/alize/callbacks/to/one_from_many_spec.rb +1 -1
- data/spec/mongoid/alize/callbacks/to/one_from_one_spec.rb +1 -1
- data/spec/mongoid/alize/from_callback_spec.rb +1 -1
- data/spec/mongoid/alize/mongoize_spec.rb +49 -0
- data/spec/mongoid/alize/to_callback_spec.rb +5 -5
- data/spec/spec_helper.rb +18 -9
- metadata +3 -2
@@ -2,7 +2,7 @@ module Mongoid
|
|
2
2
|
module Alize
|
3
3
|
class Callback
|
4
4
|
|
5
|
-
attr_accessor :
|
5
|
+
attr_accessor :denorm_attrs
|
6
6
|
|
7
7
|
attr_accessor :klass
|
8
8
|
attr_accessor :relation
|
@@ -14,12 +14,12 @@ module Mongoid
|
|
14
14
|
|
15
15
|
attr_accessor :debug
|
16
16
|
|
17
|
-
def initialize(_klass, _relation,
|
17
|
+
def initialize(_klass, _relation, _denorm_attrs)
|
18
18
|
self.debug = ENV["ALIZE_DEBUG"]
|
19
19
|
|
20
20
|
self.klass = _klass
|
21
21
|
self.relation = _relation
|
22
|
-
self.
|
22
|
+
self.denorm_attrs = _denorm_attrs
|
23
23
|
|
24
24
|
self.metadata = _klass.relations[_relation.to_s]
|
25
25
|
if !(self.metadata.polymorphic? &&
|
@@ -58,30 +58,31 @@ module Mongoid
|
|
58
58
|
"denormalize_#{direction}_#{relation}"
|
59
59
|
end
|
60
60
|
|
61
|
-
def
|
62
|
-
|
63
|
-
if
|
64
|
-
klass.send(:define_method,
|
65
|
-
|
61
|
+
def define_denorm_attrs
|
62
|
+
_denorm_attrs = denorm_attrs
|
63
|
+
if denorm_attrs.is_a?(Proc)
|
64
|
+
klass.send(:define_method, denorm_attrs_name) do |inverse|
|
65
|
+
_denorm_attrs.bind(self).call(inverse).map(&:to_s)
|
66
66
|
end
|
67
67
|
else
|
68
|
-
klass.send(:define_method,
|
69
|
-
|
68
|
+
klass.send(:define_method, denorm_attrs_name) do |inverse|
|
69
|
+
_denorm_attrs.map(&:to_s)
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
def
|
75
|
-
"#{callback_name}
|
74
|
+
def denorm_attrs_name
|
75
|
+
"#{callback_name}_attrs"
|
76
76
|
end
|
77
77
|
|
78
78
|
def field_values(source, options={})
|
79
79
|
extras = options[:id] ? "['_id']" : "[]"
|
80
80
|
<<-RUBY
|
81
|
-
(#{
|
81
|
+
value = (#{denorm_attrs_name}(#{source}) + #{extras}).inject({}) do |hash, name|
|
82
82
|
hash[name] = #{source}.send(name)
|
83
83
|
hash
|
84
|
-
|
84
|
+
end
|
85
|
+
value.respond_to?(:mongoize) ? value.mongoize : value
|
85
86
|
RUBY
|
86
87
|
end
|
87
88
|
|
@@ -19,13 +19,11 @@ module Mongoid
|
|
19
19
|
CALLBACK
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
22
|
+
def define_mongoid_field
|
23
23
|
ensure_field_not_defined!(prefixed_name, klass)
|
24
24
|
klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
|
25
25
|
field :#{prefixed_name}, :type => Array, :default => []
|
26
26
|
CALLBACK
|
27
|
-
|
28
|
-
define_fields_method
|
29
27
|
end
|
30
28
|
end
|
31
29
|
end
|
@@ -27,13 +27,11 @@ module Mongoid
|
|
27
27
|
CALLBACK
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
30
|
+
def define_mongoid_field
|
31
31
|
ensure_field_not_defined!(prefixed_name, klass)
|
32
32
|
klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
|
33
33
|
field :#{prefixed_name}, :type => Hash, :default => {}
|
34
34
|
CALLBACK
|
35
|
-
|
36
|
-
define_fields_method
|
37
35
|
end
|
38
36
|
end
|
39
37
|
end
|
@@ -3,7 +3,7 @@ module Mongoid
|
|
3
3
|
class ToCallback < Callback
|
4
4
|
|
5
5
|
def attach
|
6
|
-
|
6
|
+
define_denorm_attrs
|
7
7
|
|
8
8
|
define_callback
|
9
9
|
alias_callback
|
@@ -112,10 +112,6 @@ module Mongoid
|
|
112
112
|
"[self.#{relation}].flatten.compact"
|
113
113
|
end
|
114
114
|
|
115
|
-
def define_fields
|
116
|
-
define_fields_method
|
117
|
-
end
|
118
|
-
|
119
115
|
def set_callback
|
120
116
|
unless callback_attached?("save", aliased_callback_name)
|
121
117
|
klass.set_callback(:save, :after, aliased_callback_name)
|
data/spec/app/models/person.rb
CHANGED
@@ -34,7 +34,7 @@ describe Mongoid::Alize::Callback do
|
|
34
34
|
callback.inverse_klass = Person
|
35
35
|
callback.inverse_relation = :head
|
36
36
|
callback.inverse_metadata.should == Person.relations["head"]
|
37
|
-
callback.
|
37
|
+
callback.denorm_attrs.should == [:name, :created_at]
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should not set inverses for the child in a polymorphic association" do
|
@@ -83,37 +83,37 @@ describe Mongoid::Alize::Callback do
|
|
83
83
|
@callback.aliased_callback_name.should == "denormalize_spec_person"
|
84
84
|
end
|
85
85
|
|
86
|
-
it "should add
|
87
|
-
@callback.
|
86
|
+
it "should add _attrs to the callback name" do
|
87
|
+
@callback.denorm_attrs_name.should == "_denormalize_spec_person_attrs"
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
-
describe "
|
92
|
-
def
|
93
|
-
@callback.send(:
|
91
|
+
describe "#define_denorm_attrs" do
|
92
|
+
def define_denorm_attrs
|
93
|
+
@callback.send(:define_denorm_attrs)
|
94
94
|
end
|
95
95
|
|
96
|
-
describe "when
|
96
|
+
describe "when denorm_attrs is an array" do
|
97
97
|
before do
|
98
98
|
@callback = new_callback
|
99
99
|
end
|
100
100
|
|
101
|
-
it "should return the
|
102
|
-
|
101
|
+
it "should return the denorm_attrs w/ to_s applied" do
|
102
|
+
define_denorm_attrs
|
103
103
|
@head = Head.new
|
104
|
-
@head.send("
|
104
|
+
@head.send("_denormalize_spec_person_attrs", nil).should == ["name", "created_at"]
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
describe "when
|
108
|
+
describe "when denorm_attrs is a proc" do
|
109
109
|
before do
|
110
110
|
@callback = klass.new(Head, :person, lambda { |inverse| [:name, :created_at] })
|
111
111
|
end
|
112
112
|
|
113
|
-
it "should return the
|
114
|
-
|
113
|
+
it "should return the denorm_attrs w/ to_s applied" do
|
114
|
+
define_denorm_attrs
|
115
115
|
@head = Head.new
|
116
|
-
@head.send("
|
116
|
+
@head.send("_denormalize_spec_person_attrs", Person.new).should == ["name", "created_at"]
|
117
117
|
end
|
118
118
|
end
|
119
119
|
end
|
@@ -13,16 +13,16 @@ describe Mongoid::Alize::Callbacks::From::Many do
|
|
13
13
|
klass.new(*args)
|
14
14
|
end
|
15
15
|
|
16
|
-
describe "#
|
16
|
+
describe "#define_mongoid_field" do
|
17
17
|
it "should define an Array called {relation}_fields" do
|
18
18
|
callback = new_callback
|
19
|
-
callback.send(:
|
19
|
+
callback.send(:define_mongoid_field)
|
20
20
|
Head.fields["wanted_by_fields"].type.should == Array
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should default the field to empty" do
|
24
24
|
callback = new_callback
|
25
|
-
callback.send(:
|
25
|
+
callback.send(:define_mongoid_field)
|
26
26
|
Head.new.wanted_by_fields.should == []
|
27
27
|
end
|
28
28
|
|
@@ -32,7 +32,7 @@ describe Mongoid::Alize::Callbacks::From::Many do
|
|
32
32
|
end
|
33
33
|
callback = new_callback
|
34
34
|
expect {
|
35
|
-
callback.send(:
|
35
|
+
callback.send(:define_mongoid_field)
|
36
36
|
}.to raise_error(Mongoid::Alize::Errors::AlreadyDefinedField,
|
37
37
|
"wanted_by_fields is already defined on the Head model.")
|
38
38
|
end
|
@@ -62,13 +62,13 @@ describe Mongoid::Alize::Callbacks::From::Many do
|
|
62
62
|
describe "valid fields" do
|
63
63
|
before do
|
64
64
|
@callback = new_callback
|
65
|
-
@callback.send(:
|
65
|
+
@callback.send(:define_mongoid_field)
|
66
|
+
@callback.send(:define_denorm_attrs)
|
66
67
|
@callback.send(:define_callback)
|
67
68
|
end
|
68
69
|
|
69
70
|
it "should set fields from a changed relation" do
|
70
|
-
@head.
|
71
|
-
@head.should be_wanted_by_ids_changed
|
71
|
+
@head.wanted_by = [@person]
|
72
72
|
run_callback
|
73
73
|
@head.wanted_by_fields.should == [bob_fields]
|
74
74
|
end
|
@@ -83,12 +83,12 @@ describe Mongoid::Alize::Callbacks::From::Many do
|
|
83
83
|
describe "with a field that doesn't exist" do
|
84
84
|
before do
|
85
85
|
@callback = klass.new(Head, :wanted_by, [:notreal])
|
86
|
-
@callback.send(:
|
86
|
+
@callback.send(:define_mongoid_field)
|
87
87
|
@callback.send(:define_callback)
|
88
88
|
end
|
89
89
|
|
90
90
|
it "should raise a no method error" do
|
91
|
-
@head.
|
91
|
+
@head.wanted_by = [@person]
|
92
92
|
@head.wanted_by_fields.should be_nil
|
93
93
|
expect {
|
94
94
|
run_callback
|
@@ -109,7 +109,8 @@ describe Mongoid::Alize::Callbacks::From::Many do
|
|
109
109
|
@person = Person.create(:name => "Bob")
|
110
110
|
|
111
111
|
@callback = klass.new(Head, :sees, [:name])
|
112
|
-
@callback.send(:
|
112
|
+
@callback.send(:define_mongoid_field)
|
113
|
+
@callback.send(:define_denorm_attrs)
|
113
114
|
@callback.send(:define_callback)
|
114
115
|
|
115
116
|
@head.relations["sees"].should_not be_stores_foreign_key
|
@@ -13,17 +13,17 @@ describe Mongoid::Alize::Callbacks::From::One do
|
|
13
13
|
klass.new(*args)
|
14
14
|
end
|
15
15
|
|
16
|
-
describe "#
|
16
|
+
describe "#define_mongoid_field" do
|
17
17
|
describe "with an array of fields" do
|
18
18
|
it "should add a field generated from %{relation}_fields" do
|
19
19
|
callback = new_callback
|
20
|
-
callback.send(:
|
20
|
+
callback.send(:define_mongoid_field)
|
21
21
|
Head.fields["person_fields"].type.should == Hash
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should default the field to empty" do
|
25
25
|
callback = new_callback
|
26
|
-
callback.send(:
|
26
|
+
callback.send(:define_mongoid_field)
|
27
27
|
Head.new.person_fields.should == {}
|
28
28
|
end
|
29
29
|
|
@@ -33,7 +33,7 @@ describe Mongoid::Alize::Callbacks::From::One do
|
|
33
33
|
end
|
34
34
|
callback = new_callback
|
35
35
|
expect {
|
36
|
-
callback.send(:
|
36
|
+
callback.send(:define_mongoid_field)
|
37
37
|
}.to raise_error(Mongoid::Alize::Errors::AlreadyDefinedField,
|
38
38
|
"person_fields is already defined on the Head model.")
|
39
39
|
end
|
@@ -58,7 +58,8 @@ describe Mongoid::Alize::Callbacks::From::One do
|
|
58
58
|
|
59
59
|
before do
|
60
60
|
@callback = new_callback
|
61
|
-
@callback.send(:
|
61
|
+
@callback.send(:define_mongoid_field)
|
62
|
+
@callback.send(:define_denorm_attrs)
|
62
63
|
create_models
|
63
64
|
@callback.send(:define_callback)
|
64
65
|
end
|
@@ -107,7 +108,8 @@ describe Mongoid::Alize::Callbacks::From::One do
|
|
107
108
|
|
108
109
|
before do
|
109
110
|
@callback = klass.new(Person, :head, [:size])
|
110
|
-
@callback.send(:
|
111
|
+
@callback.send(:define_mongoid_field)
|
112
|
+
@callback.send(:define_denorm_attrs)
|
111
113
|
|
112
114
|
@person = Person.create
|
113
115
|
@head = Head.create(:size => 5)
|
@@ -43,7 +43,7 @@ describe Mongoid::Alize::ToCallback do
|
|
43
43
|
|
44
44
|
before do
|
45
45
|
@callback = new_callback
|
46
|
-
@callback.send(:
|
46
|
+
@callback.send(:define_denorm_attrs)
|
47
47
|
create_models
|
48
48
|
@callback.send(:define_callback)
|
49
49
|
end
|
@@ -75,7 +75,7 @@ describe Mongoid::Alize::ToCallback do
|
|
75
75
|
|
76
76
|
before do
|
77
77
|
@callback = new_callback
|
78
|
-
@callback.send(:
|
78
|
+
@callback.send(:define_denorm_attrs)
|
79
79
|
create_models
|
80
80
|
@callback.send(:define_destroy_callback)
|
81
81
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::Alize::ToCallback do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@now = Time.parse('2013-01-05T12:00:22-700')
|
7
|
+
stub(Time).now { @now }
|
8
|
+
|
9
|
+
Head.class_eval do
|
10
|
+
field :sees_fields, :type => Array, :default => []
|
11
|
+
end
|
12
|
+
Person.class_eval do
|
13
|
+
fields = [:name, :location, :created_at]
|
14
|
+
fields += [:my_date, :my_datetime] if SpecHelper.mongoid_3?
|
15
|
+
alize_to :seen_by, fields: fields
|
16
|
+
end
|
17
|
+
|
18
|
+
@head = Head.create(:sees => [@person = Person.create(sees_fields_without_id)])
|
19
|
+
@person.seen_by = @head
|
20
|
+
end
|
21
|
+
|
22
|
+
def sees_fields_without_id
|
23
|
+
fields = { "name"=> "Bob",
|
24
|
+
"location" => "Paris",
|
25
|
+
"created_at" => @now }
|
26
|
+
|
27
|
+
fields.merge!( "my_date" => @now.to_date,
|
28
|
+
"my_datetime" => @now.to_datetime ) if SpecHelper.mongoid_3?
|
29
|
+
|
30
|
+
fields
|
31
|
+
end
|
32
|
+
|
33
|
+
def sees_fields_with_id
|
34
|
+
sees_fields_without_id.merge!( "_id" => @person.id )
|
35
|
+
end
|
36
|
+
|
37
|
+
def sees_fields_mongoized
|
38
|
+
fields = sees_fields_with_id.merge!( "created_at" => @now.utc )
|
39
|
+
|
40
|
+
fields.merge!( "my_date" => @now.utc.to_date,
|
41
|
+
"my_datetime" => @now.utc ) if SpecHelper.mongoid_3?
|
42
|
+
|
43
|
+
fields
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should push the mongoized values to the relation" do
|
47
|
+
@head.sees_fields.should == [sees_fields_mongoized]
|
48
|
+
end
|
49
|
+
end
|
@@ -15,7 +15,7 @@ describe Mongoid::Alize::ToCallback do
|
|
15
15
|
|
16
16
|
def define_and_create(callback_name=:define_callback)
|
17
17
|
@callback = new_callback
|
18
|
-
@callback.send(:
|
18
|
+
@callback.send(:define_denorm_attrs)
|
19
19
|
create_models
|
20
20
|
@callback.send(callback_name)
|
21
21
|
end
|
@@ -47,10 +47,10 @@ describe Mongoid::Alize::ToCallback do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
describe "#
|
51
|
-
it "should define the
|
52
|
-
mock(@callback).
|
53
|
-
@callback.send(:
|
50
|
+
describe "#define_denorm_attrs" do
|
51
|
+
it "should define the denorm attrs method" do
|
52
|
+
mock(@callback).define_denorm_attrs
|
53
|
+
@callback.send(:define_denorm_attrs)
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,21 +1,28 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require 'looksee'
|
3
|
-
require 'awesome_print'
|
4
|
-
require 'wirble'
|
5
2
|
require 'mongoid'
|
6
3
|
|
7
|
-
|
8
|
-
|
4
|
+
unless ENV['CI']
|
5
|
+
require 'looksee'
|
6
|
+
require 'awesome_print'
|
7
|
+
require 'wirble'
|
8
|
+
end
|
9
|
+
|
10
|
+
module SpecHelper
|
11
|
+
def self.mongoid_3?
|
12
|
+
defined?(Mongoid::VERSION) && Mongoid::VERSION =~ /^3/
|
13
|
+
end
|
9
14
|
end
|
10
15
|
|
11
16
|
Mongoid.configure do |config|
|
12
|
-
if
|
17
|
+
if SpecHelper.mongoid_3?
|
13
18
|
config.connect_to("mongoid_alize_test")
|
14
|
-
Moped.logger = Logger.new(
|
15
|
-
Moped.logger.level = Logger::
|
19
|
+
Moped.logger = Logger.new($stdout)
|
20
|
+
Moped.logger.level = Logger::INFO
|
16
21
|
else
|
22
|
+
logger = Logger.new($stdout)
|
23
|
+
logger.level = Logger::INFO
|
17
24
|
config.master = Mongo::Connection.new("localhost", 27017,
|
18
|
-
:logger =>
|
25
|
+
:logger => logger).db("mongoid_alize_test")
|
19
26
|
end
|
20
27
|
end
|
21
28
|
|
@@ -26,6 +33,8 @@ Dir["#{File.dirname(__FILE__)}/helpers/*.rb"].each { |f| require f }
|
|
26
33
|
RSpec.configure do |config|
|
27
34
|
config.include(MacrosHelper)
|
28
35
|
|
36
|
+
puts "MongoidVersion - #{Mongoid::VERSION}"
|
37
|
+
|
29
38
|
config.mock_with :rr
|
30
39
|
config.before :each do
|
31
40
|
Mongoid.purge!
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_alize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
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: 2013-
|
12
|
+
date: 2013-10-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- spec/mongoid/alize/from_callback_spec.rb
|
77
77
|
- spec/mongoid/alize/instance_helpers_spec.rb
|
78
78
|
- spec/mongoid/alize/macros_spec.rb
|
79
|
+
- spec/mongoid/alize/mongoize_spec.rb
|
79
80
|
- spec/mongoid/alize/to_callback_spec.rb
|
80
81
|
- spec/mongoid_alize_spec.rb
|
81
82
|
- spec/spec_helper.rb
|