denormalize-field 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -7,4 +7,5 @@ group :development do
7
7
  gem "rspec", "~> 2.8.0"
8
8
  gem "rdoc", "~> 3.12"
9
9
  gem "jeweler", "~> 1.8.4"
10
+ gem "mocha"
10
11
  end
data/Gemfile.lock CHANGED
@@ -24,7 +24,10 @@ GEM
24
24
  rake
25
25
  rdoc
26
26
  json (1.7.5)
27
+ metaclass (0.0.1)
27
28
  method_source (0.8)
29
+ mocha (0.10.4)
30
+ metaclass (~> 0.0.1)
28
31
  multi_json (1.3.6)
29
32
  pry (0.9.10)
30
33
  coderay (~> 1.0.5)
@@ -51,6 +54,7 @@ PLATFORMS
51
54
  DEPENDENCIES
52
55
  activerecord (~> 3.2.0)
53
56
  jeweler (~> 1.8.4)
57
+ mocha
54
58
  pry
55
59
  rdoc (~> 3.12)
56
60
  rspec (~> 2.8.0)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "denormalize-field"
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Len Smith"]
12
- s.date = "2012-09-24"
12
+ s.date = "2012-10-15"
13
13
  s.description = "Denormalize ActiveRecord fields for performance reasons"
14
14
  s.email = "len@barrison.com"
15
15
  s.extra_rdoc_files = [
@@ -47,6 +47,7 @@ Gem::Specification.new do |s|
47
47
  s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
48
48
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
49
49
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
50
+ s.add_development_dependency(%q<mocha>, [">= 0"])
50
51
  else
51
52
  s.add_dependency(%q<activerecord>, ["~> 3.2.0"])
52
53
  s.add_dependency(%q<pry>, [">= 0"])
@@ -54,6 +55,7 @@ Gem::Specification.new do |s|
54
55
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
55
56
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
56
57
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
58
+ s.add_dependency(%q<mocha>, [">= 0"])
57
59
  end
58
60
  else
59
61
  s.add_dependency(%q<activerecord>, ["~> 3.2.0"])
@@ -62,6 +64,7 @@ Gem::Specification.new do |s|
62
64
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
63
65
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
64
66
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
67
+ s.add_dependency(%q<mocha>, [">= 0"])
65
68
  end
66
69
  end
67
70
 
@@ -9,13 +9,17 @@ module DenormalizeFields
9
9
 
10
10
  before_save do
11
11
  _denormalized_field_name = "#{key}_#{_field_name}"
12
- self.send "#{_denormalized_field_name}=", self.send(key).send(_field_name)
12
+ if self.send(key)
13
+ self.send "#{_denormalized_field_name}=", self.send(key).send(_field_name)
14
+ end
13
15
  _original_klass = self.class
14
16
 
15
17
  _klass = key.to_s.camelize.constantize
16
18
  _klass.after_save do
17
- self.send(_original_klass.name.downcase.pluralize).each do |child|
18
- child.update_attribute _denormalized_field_name, self.send(_field_name)
19
+ if self.send "#{_field_name}_changed?"
20
+ self.send(_original_klass.name.downcase.pluralize).each do |child|
21
+ child.update_attribute _denormalized_field_name, self.send(_field_name)
22
+ end
19
23
  end
20
24
  end
21
25
  end
@@ -27,4 +27,19 @@ describe "DenormalizeField" do
27
27
  post.reload
28
28
  post.category_name.should == "Sports"
29
29
  end
30
+
31
+ it "handles nil associations" do
32
+ post.category = nil
33
+ expect { post.save! }.to_not raise_error
34
+ end
35
+
36
+ it "doesn't save the association unless the denormalized field has changed" do
37
+ post.save!
38
+ updated_at = post.updated_at
39
+ category = Category.first
40
+ Post.any_instance.expects(:update_attribute).never
41
+ category.save!
42
+ post.reload
43
+ post.updated_at.should == updated_at
44
+ end
30
45
  end
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,7 @@ require 'denormalize-field'
8
8
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
9
 
10
10
  RSpec.configure do |config|
11
+ config.mock_with :mocha
11
12
  config.before(:each) do
12
13
  Post.delete_all
13
14
  Category.delete_all
@@ -21,10 +22,11 @@ ActiveRecord::Base.connection.drop_table(:categories)
21
22
  ActiveRecord::Base.connection.drop_table(:posts)
22
23
  ActiveRecord::Base.connection.create_table(:categories) do |t|
23
24
  t.string :name
25
+ t.timestamps
24
26
  end
27
+
25
28
  ActiveRecord::Base.connection.create_table(:posts) do |t|
26
29
  t.string :category_id
27
30
  t.string :category_name
31
+ t.timestamps
28
32
  end
29
-
30
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: denormalize-field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.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-24 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: activerecord
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - ~>
108
108
  - !ruby/object:Gem::Version
109
109
  version: 1.8.4
110
+ - !ruby/object:Gem::Dependency
111
+ name: mocha
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: Denormalize ActiveRecord fields for performance reasons
111
127
  email: len@barrison.com
112
128
  executables: []
@@ -143,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
159
  version: '0'
144
160
  segments:
145
161
  - 0
146
- hash: 2097104229179456767
162
+ hash: -2310274832214155233
147
163
  required_rubygems_version: !ruby/object:Gem::Requirement
148
164
  none: false
149
165
  requirements: