mongoid_denormalize 0.4.0 → 0.4.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/README.md CHANGED
@@ -143,7 +143,7 @@ So, if User has_many :posts and User has_many :comments, but Comments are embedd
143
143
  Contributing
144
144
  -------
145
145
 
146
- Clone the repository and run Bundler with `bundle install` so that you can run the rake tasks.
146
+ Clone the repository and run Bundler with `bundle install`. Use `rake -T` to view available rake tasks.
147
147
 
148
148
  Contributors
149
149
  -------
@@ -1,3 +1,4 @@
1
+ require 'mongoid_denormalize/version'
1
2
  require File.dirname(__FILE__) + '/railties/railtie' if defined?(Rails::Railtie)
2
3
 
3
4
  # = Mongoid::Denormalize
@@ -103,7 +104,8 @@ module Mongoid::Denormalize
103
104
 
104
105
  def assign_and_save(obj, assignments, prefix)
105
106
  attributes_hash = Hash[assignments.collect do |assignment|
106
- if self.changed_attributes.has_key?(assignment[:source_field])
107
+ if self.changed_attributes.has_key?(assignment[:source_field].to_s) ||
108
+ self.changed_attributes.has_key?(assignment[:source_field].to_sym)
107
109
  ["#{prefix}_#{assignment[:source_field]}", assignment[:value]]
108
110
  end
109
111
  end]
@@ -111,7 +113,7 @@ module Mongoid::Denormalize
111
113
  unless attributes_hash.empty?
112
114
  # The more succinct update_attributes(changes, :without_protection => true) requires Mongoid 3.0.0,
113
115
  # but we only require 2.1.9
114
- obj.assign_attributes(attributes_hash, :without_protection => true)
116
+ obj.write_attributes(attributes_hash, false)
115
117
  obj.save(:validate => false)
116
118
  end
117
119
  end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Denormalize
3
+ VERSION = '0.4.1'
4
+ end
5
+ end
@@ -0,0 +1,150 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Denormalize do
4
+ before(:each) do
5
+ @post = Post.create!(:title => "Blog post", :body => "Lorem ipsum...", :created_at => Time.parse("Jan 1 2010 12:00"))
6
+ @user = User.create!(:name => "John Doe", :email => "john@doe.com", :post => @post, :location => [1, 1], :nickname => "jdoe")
7
+ @comment = @post.comments.create(:body => "This is the comment", :user => @user)
8
+ @post.reload #neccessary for older versions of Mongoid
9
+ @moderated_comment = @post.comments.create(:body => "This is a moderated comment", :moderator => @user)
10
+ @article = @user.articles.create!(:title => "Article about Lorem", :body => "Lorem ipsum...", :created_at => Time.parse("Jan 1 2010 12:00"))
11
+
12
+ @user.comments << @comment
13
+ @user.moderated_comments << @moderated_comment
14
+ @user.save
15
+ @user.reload
16
+ @other_user = User.create!(:name => "Bill")
17
+ end
18
+
19
+ context "denormalize from" do
20
+ it "should define multiple fields for association" do
21
+ @post.fields.should have_key "user_name"
22
+ @post.fields.should have_key "user_email"
23
+ @post.fields.should have_key "user_location"
24
+ end
25
+
26
+ it "should default to string field type for associated fields" do
27
+ @post.fields["user_name"].type.should eql String
28
+ end
29
+
30
+ it "should allow setting the field type for associated fields" do
31
+ @comment.fields["post_created_at"].type.should eql Time
32
+ end
33
+
34
+ it "should allow multiple declarations for the same association" do
35
+ @comment.fields.should have_key "user_name"
36
+ @comment.fields.should have_key "user_email"
37
+ end
38
+
39
+ it "should denormalize fields without specified type" do
40
+ @comment.user_name.should eql @user.name
41
+ @comment.user_email.should eql @user.email
42
+ @post.user_name.should eql @user.name
43
+ @post.user_email.should eql @user.email
44
+ end
45
+
46
+ it "should denormalize fields with specified type" do
47
+ @comment.post_created_at.should eql @post.created_at
48
+
49
+ @post.user_location.should eql @user.location
50
+ end
51
+
52
+ it "should use fresh values from database where possible" do
53
+ @other_post = Post.create!(:title => "My first blog post")
54
+ @other_post.update_attribute(:user_id, @user.id)
55
+ @other_post.user_name.should eql @user.name
56
+ end
57
+
58
+ it "should update denormalized values if attribute is changed" do
59
+ @user.update_attributes(:name => "Bob Doe", :location => [4, 4])
60
+
61
+ @post.reload #needed for old versions of Mongoid
62
+ @post.user_location.should eql @user.location
63
+
64
+ @comment.reload #needed for old versions of Mongoid
65
+ @comment.user_name.should eql @user.name
66
+ end
67
+
68
+ it "should update denormalized values if object is changed" do
69
+ @other_user = User.create!(:name => "Bill", :email => "bill@doe.com")
70
+
71
+ @comment.user = @other_user
72
+ @comment.save!
73
+
74
+ @comment.user_name.should eql @other_user.name
75
+ @comment.user_email.should eql @other_user.email
76
+ end
77
+ end
78
+
79
+ context "denormalize to" do
80
+ it "should push denormalized fields to one-to-one association" do
81
+ @user.name = "Elvis"
82
+ @user.save!
83
+
84
+ @post.reload #needed for old versions of Mongoid
85
+ @post.user_name.should eql "Elvis"
86
+ end
87
+
88
+ it "should push denormalized fields to one-to-many association" do
89
+ @post.created_at = Time.parse("Jan 1 2011 12:00")
90
+ @post.save!
91
+
92
+ @comment.reload #needed for old versions of Mongoid
93
+ @comment.post_created_at.should eql Time.parse("Jan 1 2011 12:00")
94
+ end
95
+
96
+ it "should push denormalized fields to association using inverse_of class name" do
97
+ @user.update_attributes(:name => "Bob Doe", :email => "bob@doe.com")
98
+ @user.save!
99
+
100
+ @article.reload #needed for old versions of Mongoid
101
+ @article.author_name.should eql "Bob Doe"
102
+ @article.author_email.should eql "bob@doe.com"
103
+ end
104
+
105
+ it "should push to overriden field names" do
106
+ @user.nickname = "jonsey"
107
+ @user.save!
108
+
109
+ @moderated_comment.reload
110
+ @moderated_comment.mod_nickname.should eql "jonsey"
111
+ end
112
+
113
+ it "shouldn't make superfluous saves" do
114
+ @comment.should_not_receive(:save)
115
+ @post.save!
116
+ end
117
+ end
118
+
119
+ context "rake task" do
120
+ it "should correct inconsistent denormalizations on regular documents" do
121
+ if ::Mongoid::VERSION < '3'
122
+ Post.collection.update({ '_id' => @post.id }, { '$set' => { 'user_name' => 'Clint Eastwood' } })
123
+ else
124
+ @post.set(:user_name, 'Clint Eastwood')
125
+ end
126
+
127
+ @post.reload #needed for old versions of Mongoid
128
+ @post.user_name.should eq 'Clint Eastwood'
129
+
130
+ Rake::Task["db:denormalize"].invoke
131
+ Rake::Task["db:denormalize"].reenable
132
+
133
+ @post.reload
134
+ @post.user_name.should eql @user.name
135
+ end
136
+
137
+ it "should correct inconsistent denormalizations on referenced embedded documents" do
138
+ @rake_user = User.create!(:name => "Johnny Depp", :email => "johnny@depp.com")
139
+ @rake_comment = @post.comments.create!(:body => "Depp's comment", :user => @rake_user)
140
+
141
+ @rake_user.update_attributes!(:name => "J. Depp")
142
+
143
+ Rake::Task["db:denormalize"].invoke
144
+ Rake::Task["db:denormalize"].reenable
145
+
146
+ @post.reload
147
+ @post.comments.last.user_name.should eql @rake_user.name
148
+ end
149
+ end
150
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_denormalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,56 +11,8 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: mongoid_denormalize
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
14
  - !ruby/object:Gem::Dependency
31
15
  name: rake
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: bson_ext
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :runtime
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: jeweler
64
16
  requirement: !ruby/object:Gem::Requirement
65
17
  none: false
66
18
  requirements:
@@ -116,10 +68,12 @@ extra_rdoc_files:
116
68
  - README.md
117
69
  files:
118
70
  - lib/mongoid_denormalize.rb
71
+ - lib/mongoid_denormalize/version.rb
119
72
  - lib/railties/denormalize.rake
120
73
  - lib/railties/railtie.rb
121
74
  - LICENSE
122
75
  - README.md
76
+ - spec/mongoid_denormalize_spec.rb
123
77
  homepage: http://github.com/logandk/mongoid_denormalize
124
78
  licenses: []
125
79
  post_install_message:
@@ -132,9 +86,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
86
  - - ! '>='
133
87
  - !ruby/object:Gem::Version
134
88
  version: '0'
135
- segments:
136
- - 0
137
- hash: 4386387219584124888
138
89
  required_rubygems_version: !ruby/object:Gem::Requirement
139
90
  none: false
140
91
  requirements:
@@ -143,8 +94,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
94
  version: '0'
144
95
  requirements: []
145
96
  rubyforge_project:
146
- rubygems_version: 1.8.24
97
+ rubygems_version: 1.8.25
147
98
  signing_key:
148
99
  specification_version: 3
149
100
  summary: Mongoid denormalization helper.
150
- test_files: []
101
+ test_files:
102
+ - spec/mongoid_denormalize_spec.rb