rails_relations_fix 1.0.0
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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +11 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/rails_relations_fix/association.rb +11 -0
- data/lib/rails_relations_fix/counter_cache.rb +83 -0
- data/lib/rails_relations_fix.rb +4 -0
- data/rails_relations_fix.gemspec +53 -0
- data/test/helper.rb +44 -0
- data/test/test_rails_relations_fix.rb +48 -0
- metadata +68 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Stanisław Kolarzowski
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
= rails_relations_fix
|
2
|
+
|
3
|
+
This is fix for 2 rails bugs:
|
4
|
+
|
5
|
+
* Counter_cache is not updated in polymorhpic assocation https://rails.lighthouseapp.com/projects/8994/tickets/3005-counter_cache-not-updated-if-polymorphic-true-when-assigning-association
|
6
|
+
* destroy for association collection does not delete object from parent array https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3964-destroy-for-association-collection-does-not-delete-object-from-parent-array
|
7
|
+
|
8
|
+
== Copyright
|
9
|
+
|
10
|
+
* Used patch made by Hsume2 [http://github.com/hsume2]
|
11
|
+
* Copyright (c) 2010 Stanisław Kolarzowski. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "rails_relations_fix"
|
8
|
+
gem.summary = "Fix for 2 rails bugs"
|
9
|
+
gem.description = "Fix for 2 rails bugs. #3005 Counter_cache and polymorhic association. #3964 Not refreshing association after destroy"
|
10
|
+
gem.email = "stanislaw.kolarzowski@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/staszek/rails_relations_fix"
|
12
|
+
gem.authors = ["Stanisław Kolarzowski"]
|
13
|
+
# gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "rails_relations_fix #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Callbacks
|
3
|
+
NEW_CALLBACKS = %w(
|
4
|
+
before_add after_add before_remove after_remove
|
5
|
+
)
|
6
|
+
|
7
|
+
def self.define_callback_methods
|
8
|
+
base = ActiveRecord::Base
|
9
|
+
[:create, :update, :destroy].each do |method|
|
10
|
+
# Due to using aliast_method_chain for methods like method_name_with_callbacks they are not reading from this file.
|
11
|
+
# They need to be reloaded.
|
12
|
+
base.send :alias_method, method, "#{method}_without_callbacks".to_sym
|
13
|
+
base.send :alias_method_chain, method, :callbacks
|
14
|
+
end
|
15
|
+
base.define_callbacks *NEW_CALLBACKS
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_with_callbacks #:nodoc:
|
19
|
+
return false if callback(:before_create) == false
|
20
|
+
return false if callback(:before_add) == false if changed?
|
21
|
+
result = create_without_callbacks
|
22
|
+
callback(:after_add) if changed?
|
23
|
+
callback(:after_create)
|
24
|
+
result
|
25
|
+
end
|
26
|
+
|
27
|
+
def update_with_callbacks(*args) #:nodoc:
|
28
|
+
return false if callback(:before_update) == false
|
29
|
+
return false if callback(:before_add) == false if changed?
|
30
|
+
result = update_without_callbacks(*args)
|
31
|
+
callback(:after_add) if changed?
|
32
|
+
callback(:after_update)
|
33
|
+
result
|
34
|
+
end
|
35
|
+
|
36
|
+
def destroy_with_callbacks #:nodoc:
|
37
|
+
return false if callback(:before_destroy) == false
|
38
|
+
return false if callback(:before_remove) == false
|
39
|
+
result = destroy_without_callbacks
|
40
|
+
callback(:after_remove)
|
41
|
+
callback(:after_destroy)
|
42
|
+
result
|
43
|
+
end
|
44
|
+
|
45
|
+
def before_add() end #:nodoc:
|
46
|
+
def after_add() end #:nodoc:
|
47
|
+
def before_remove() end #:nodoc:
|
48
|
+
def after_remove() end #:nodoc:
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
module Associations
|
53
|
+
module ClassMethods
|
54
|
+
def add_counter_cache_callbacks(reflection)
|
55
|
+
cache_column = reflection.counter_cache_column
|
56
|
+
|
57
|
+
method_name = "belongs_to_counter_cache_after_add_for_#{reflection.name}".to_sym
|
58
|
+
define_method(method_name) do
|
59
|
+
|
60
|
+
association = send(reflection.name, true) rescue nil
|
61
|
+
|
62
|
+
if send("#{reflection.primary_key_name}_changed?") && send("#{reflection.primary_key_name}_change") != send("#{reflection.primary_key_name}_change").compact
|
63
|
+
association.class.increment_counter(cache_column, association.id) unless association.nil?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
after_add(method_name)
|
67
|
+
|
68
|
+
method_name = "belongs_to_counter_cache_before_remove_for_#{reflection.name}".to_sym
|
69
|
+
define_method(method_name) do
|
70
|
+
association = send(reflection.name)
|
71
|
+
association.class.decrement_counter(cache_column, association.id) unless association.nil?
|
72
|
+
end
|
73
|
+
before_remove(method_name)
|
74
|
+
|
75
|
+
module_eval(
|
76
|
+
"#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)"
|
77
|
+
)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rails_relations_fix}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Stanis\305\202aw Kolarzowski"]
|
12
|
+
s.date = %q{2010-02-15}
|
13
|
+
s.description = %q{Fix for 2 rails bugs. #3005 Counter_cache and polymorhic association. #3964 Not refreshing association after destroy}
|
14
|
+
s.email = %q{stanislaw.kolarzowski@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/rails_relations_fix.rb",
|
27
|
+
"lib/rails_relations_fix/association.rb",
|
28
|
+
"lib/rails_relations_fix/counter_cache.rb",
|
29
|
+
"rails_relations_fix.gemspec",
|
30
|
+
"test/helper.rb",
|
31
|
+
"test/test_rails_relations_fix.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/staszek/rails_relations_fix}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.5}
|
37
|
+
s.summary = %q{Fix for 2 rails bugs}
|
38
|
+
s.test_files = [
|
39
|
+
"test/helper.rb",
|
40
|
+
"test/test_rails_relations_fix.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
else
|
49
|
+
end
|
50
|
+
else
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require "mocha"
|
5
|
+
require "action_controller"
|
6
|
+
require 'active_resource'
|
7
|
+
require 'active_record'
|
8
|
+
require 'active_support'
|
9
|
+
require "active_record/base"
|
10
|
+
require "active_record/associations"
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
13
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
14
|
+
|
15
|
+
require 'rails_relations_fix'
|
16
|
+
|
17
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
18
|
+
ActiveRecord::Base.configurations = true
|
19
|
+
|
20
|
+
ActiveRecord::Schema.verbose = false
|
21
|
+
ActiveRecord::Schema.define(:version => 1) do
|
22
|
+
create_table :users do |t|
|
23
|
+
t.string :name
|
24
|
+
t.integer :movies_count
|
25
|
+
end
|
26
|
+
|
27
|
+
create_table :movies do |t|
|
28
|
+
t.string :name
|
29
|
+
t.string :subject_type
|
30
|
+
t.integer :subject_id
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
class Test::Unit::TestCase
|
36
|
+
|
37
|
+
class User < ActiveRecord::Base
|
38
|
+
has_many :movies, :as => :subject
|
39
|
+
end
|
40
|
+
|
41
|
+
class Movie < ActiveRecord::Base
|
42
|
+
belongs_to :subject, :polymorphic => true, :counter_cache => true
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestRailsRelationFix< Test::Unit::TestCase
|
4
|
+
context "RailsRelationFix tests" do
|
5
|
+
setup do
|
6
|
+
@user = User.create(:name => "John Rambo")
|
7
|
+
@titanic = @user.movies.create(:name => "Titanic")
|
8
|
+
end
|
9
|
+
|
10
|
+
context "Models testing" do
|
11
|
+
should "User has movie" do
|
12
|
+
assert_equal(@titanic, @user.movies.first)
|
13
|
+
end
|
14
|
+
|
15
|
+
should "Movie belongs to user" do
|
16
|
+
assert_equal(@user, @titanic.subject)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "Association bug tests" do
|
21
|
+
should "return empty collection after destroy its only movie" do
|
22
|
+
assert_equal([@titanic], @user.movies)
|
23
|
+
@user.movies.destroy(@titanic)
|
24
|
+
assert_equal([], @user.movies)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "Counter_cache for polymorhpic association bug test" do
|
29
|
+
should "Update movies_count column after adding movie by << or destroing" do
|
30
|
+
@user.movies << Movie.create(:name => "Matrix")
|
31
|
+
assert_equal(2, @user.reload.movies_count)
|
32
|
+
@titanic.destroy
|
33
|
+
assert_equal(1, @user.reload.movies_count)
|
34
|
+
end
|
35
|
+
|
36
|
+
should "Increment counter_cache only once when create" do
|
37
|
+
assert_equal(1, @user.reload.movies_count)
|
38
|
+
end
|
39
|
+
|
40
|
+
should "Refresh size after adding movie by << or destroing" do
|
41
|
+
@user.movies << Movie.create(:name => "Matrix")
|
42
|
+
assert_equal(2, @user.movies.size)
|
43
|
+
@titanic.destroy
|
44
|
+
assert_equal(1, @user.movies.size)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_relations_fix
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Stanis\xC5\x82aw Kolarzowski"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-15 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "Fix for 2 rails bugs. #3005 Counter_cache and polymorhic association. #3964 Not refreshing association after destroy"
|
17
|
+
email: stanislaw.kolarzowski@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- lib/rails_relations_fix.rb
|
33
|
+
- lib/rails_relations_fix/association.rb
|
34
|
+
- lib/rails_relations_fix/counter_cache.rb
|
35
|
+
- rails_relations_fix.gemspec
|
36
|
+
- test/helper.rb
|
37
|
+
- test/test_rails_relations_fix.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/staszek/rails_relations_fix
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Fix for 2 rails bugs
|
66
|
+
test_files:
|
67
|
+
- test/helper.rb
|
68
|
+
- test/test_rails_relations_fix.rb
|