deferred_associations 0.5.4 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/Rakefile +27 -26
- data/Readme.markdown +85 -82
- data/VERSION +1 -1
- data/deferred_associations.gemspec +61 -75
- data/init.rb +1 -1
- data/lib/array_to_association_wrapper.rb +0 -0
- data/lib/deferred_associations.rb +0 -0
- data/lib/has_and_belongs_to_many_with_deferred_save.rb +142 -142
- data/lib/has_many_with_deferred_save.rb +10 -1
- data/spec/db/database.yml +21 -21
- data/spec/db/schema.rb +40 -40
- data/spec/has_and_belongs_to_many_with_deferred_save_spec.rb +236 -234
- data/spec/has_many_with_deferred_save_spec.rb +28 -1
- data/spec/models/chair.rb +0 -0
- data/spec/models/door.rb +3 -3
- data/spec/models/person.rb +15 -15
- data/spec/models/room.rb +51 -51
- data/spec/models/table.rb +0 -0
- data/spec/spec_helper.rb +47 -39
- metadata +85 -95
- data/.gitignore +0 -3
- data/.travis.yml +0 -12
- data/gemfiles/ar2.3.14.gemfile +0 -12
- data/gemfiles/ar2.3.14.gemfile.lock +0 -47
- data/gemfiles/ar3.2.3.gemfile +0 -12
- data/gemfiles/ar3.2.3.gemfile.lock +0 -96
- data/spec/.gitignore +0 -2
@@ -14,10 +14,23 @@ describe 'has_many_with_deferred_save' do
|
|
14
14
|
@room.tables.should == [@table1]
|
15
15
|
@room.tables = [@table1, @table2]
|
16
16
|
Room.find(@room.id).tables.should == [@table1] # not saved yet
|
17
|
+
@room.tables.should == [@table1, @table2]
|
18
|
+
@room.table_ids.should == [@table1.id, @table2.id]
|
17
19
|
@room.save.should be_true
|
18
20
|
Room.find(@room.id).tables.should == [@table1, @table2]
|
19
21
|
end
|
20
22
|
|
23
|
+
it 'should work with tables obj setter/getter, used twice' do
|
24
|
+
@room.tables.should == [@table1]
|
25
|
+
@room.tables = [@table1]
|
26
|
+
@room.tables = [@table1, @table2]
|
27
|
+
Room.find(@room.id).tables.should == [@table1] # not saved yet
|
28
|
+
@room.table_ids.should == [@table1.id, @table2.id]
|
29
|
+
@room.tables.should == [@table1, @table2]
|
30
|
+
@room.save.should be_true
|
31
|
+
Room.find(@room.id).tables.should == [@table1, @table2]
|
32
|
+
end
|
33
|
+
|
21
34
|
it 'should work with tables id setter/getter' do
|
22
35
|
@room.table_ids.should == [@table1.id]
|
23
36
|
@room.table_ids = [@table1.id, @table2.id]
|
@@ -26,6 +39,15 @@ describe 'has_many_with_deferred_save' do
|
|
26
39
|
Room.find(@room.id).table_ids.should == [@table1.id, @table2.id]
|
27
40
|
end
|
28
41
|
|
42
|
+
it 'should work with tables id setter/getter, used twice' do
|
43
|
+
@room.table_ids.should == [@table1.id]
|
44
|
+
@room.table_ids = [@table1.id]
|
45
|
+
@room.table_ids = [@table1.id, @table2.id]
|
46
|
+
Room.find(@room.id).table_ids.should == [@table1.id] # not saved yet
|
47
|
+
@room.save.should be_true
|
48
|
+
Room.find(@room.id).table_ids.should == [@table1.id, @table2.id]
|
49
|
+
end
|
50
|
+
|
29
51
|
it 'should work with array methods' do
|
30
52
|
@room.tables.should == [@table1]
|
31
53
|
@room.tables << @table2
|
@@ -63,7 +85,12 @@ describe 'has_many_with_deferred_save' do
|
|
63
85
|
|
64
86
|
it 'should defer association methods' do
|
65
87
|
@room.chairs.first.should == @chair1
|
66
|
-
|
88
|
+
if ar4?
|
89
|
+
@room.chairs.where(:name => "First").should == [@chair1]
|
90
|
+
else
|
91
|
+
@room.chairs.find(:all, :conditions => {:name => "First"}).should == [@chair1]
|
92
|
+
end
|
93
|
+
|
67
94
|
lambda {
|
68
95
|
@room.chairs.create(:name => "New one")
|
69
96
|
}.should raise_error(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection)
|
data/spec/models/chair.rb
CHANGED
File without changes
|
data/spec/models/door.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
class Door < ActiveRecord::Base
|
2
|
-
has_and_belongs_to_many_with_deferred_save :rooms
|
3
|
-
end
|
1
|
+
class Door < ActiveRecord::Base
|
2
|
+
has_and_belongs_to_many_with_deferred_save :rooms
|
3
|
+
end
|
data/spec/models/person.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
class Person < ActiveRecord::Base
|
2
|
-
has_and_belongs_to_many_with_deferred_save :rooms, :validate => true
|
3
|
-
|
4
|
-
|
5
|
-
validate
|
6
|
-
|
7
|
-
def extra_validation
|
8
|
-
rooms.each do |room|
|
9
|
-
this_room_unsaved = rooms_without_deferred_save.include?(room) ? 0 : 1
|
10
|
-
if room.people.size + this_room_unsaved > room.maximum_occupancy
|
11
|
-
errors.add :rooms, "This room has reached its maximum occupancy"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
1
|
+
class Person < ActiveRecord::Base
|
2
|
+
has_and_belongs_to_many_with_deferred_save :rooms, :validate => true
|
3
|
+
|
4
|
+
attr_accessor :do_extra_validation
|
5
|
+
validate :extra_validation, :if => :do_extra_validation
|
6
|
+
|
7
|
+
def extra_validation
|
8
|
+
rooms.each do |room|
|
9
|
+
this_room_unsaved = rooms_without_deferred_save.include?(room) ? 0 : 1
|
10
|
+
if room.people.size + this_room_unsaved > room.maximum_occupancy
|
11
|
+
errors.add :rooms, "This room has reached its maximum occupancy"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/models/room.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
|
-
class Room < ActiveRecord::Base
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
before_save :diff_before_module
|
8
|
-
|
9
|
-
has_and_belongs_to_many_with_deferred_save :people, :before_add => :before_adding_person
|
10
|
-
has_and_belongs_to_many :people2, :class_name => 'Person'
|
11
|
-
has_and_belongs_to_many_with_deferred_save :doors
|
12
|
-
|
13
|
-
has_many_with_deferred_save :tables
|
14
|
-
has_many_with_deferred_save :chairs, :through => :tables #TODO test compatibility with through associations
|
15
|
-
|
16
|
-
before_save :diff_after_module
|
17
|
-
|
18
|
-
validate :people_count
|
19
|
-
|
20
|
-
def people_count
|
21
|
-
if people.size > maximum_occupancy
|
22
|
-
errors.add :people, "This room has reached its maximum occupancy"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# Just in case they try to bypass our new accessor and call people_without_deferred_save directly...
|
27
|
-
# (This should never be necessary; it is for demonstration purposes only...)
|
28
|
-
def before_adding_person(person)
|
29
|
-
if self.people_without_deferred_save.size + [person].size > maximum_occupancy
|
30
|
-
raise "There are too many people in this room"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def diff_before_module
|
35
|
-
#should detect the changes
|
36
|
-
self.bs_diff_before_module = (people.size - people_without_deferred_save.size) != 0
|
37
|
-
true
|
38
|
-
end
|
39
|
-
|
40
|
-
def diff_after_module
|
41
|
-
# should not detect the changes
|
42
|
-
self.bs_diff_after_module = (people.size - people_without_deferred_save.size) != 0
|
43
|
-
true
|
44
|
-
end
|
45
|
-
|
46
|
-
def before_save
|
47
|
-
# old_style, should not detect the changes
|
48
|
-
self.bs_diff_method = (people.size - people_without_deferred_save.size) != 0
|
49
|
-
true
|
50
|
-
end
|
51
|
-
end
|
1
|
+
class Room < ActiveRecord::Base
|
2
|
+
|
3
|
+
attr_accessor :bs_diff_before_module
|
4
|
+
attr_accessor :bs_diff_after_module
|
5
|
+
attr_accessor :bs_diff_method
|
6
|
+
|
7
|
+
before_save :diff_before_module
|
8
|
+
|
9
|
+
has_and_belongs_to_many_with_deferred_save :people, :before_add => :before_adding_person
|
10
|
+
has_and_belongs_to_many :people2, :class_name => 'Person'
|
11
|
+
has_and_belongs_to_many_with_deferred_save :doors
|
12
|
+
|
13
|
+
has_many_with_deferred_save :tables
|
14
|
+
has_many_with_deferred_save :chairs, :through => :tables #TODO test compatibility with through associations
|
15
|
+
|
16
|
+
before_save :diff_after_module
|
17
|
+
|
18
|
+
validate :people_count
|
19
|
+
|
20
|
+
def people_count
|
21
|
+
if people.size > maximum_occupancy
|
22
|
+
errors.add :people, "This room has reached its maximum occupancy"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Just in case they try to bypass our new accessor and call people_without_deferred_save directly...
|
27
|
+
# (This should never be necessary; it is for demonstration purposes only...)
|
28
|
+
def before_adding_person(person)
|
29
|
+
if self.people_without_deferred_save.size + [person].size > maximum_occupancy
|
30
|
+
raise "There are too many people in this room"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def diff_before_module
|
35
|
+
#should detect the changes
|
36
|
+
self.bs_diff_before_module = (people.size - people_without_deferred_save.size) != 0
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
def diff_after_module
|
41
|
+
# should not detect the changes
|
42
|
+
self.bs_diff_after_module = (people.size - people_without_deferred_save.size) != 0
|
43
|
+
true
|
44
|
+
end
|
45
|
+
|
46
|
+
def before_save
|
47
|
+
# old_style, should not detect the changes
|
48
|
+
self.bs_diff_method = (people.size - people_without_deferred_save.size) != 0
|
49
|
+
true
|
50
|
+
end
|
51
|
+
end
|
data/spec/models/table.rb
CHANGED
File without changes
|
data/spec/spec_helper.rb
CHANGED
@@ -1,39 +1,47 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
-
plugin_test_dir = File.dirname(__FILE__)
|
3
|
-
|
4
|
-
|
5
|
-
require 'active_record'
|
6
|
-
if ActiveRecord::VERSION::STRING >= "3"
|
7
|
-
require 'logger'
|
8
|
-
else
|
9
|
-
# Workaround for https://rails.lighthouseapp.com/projects/8994/tickets/2577-when-using-activerecordassociations-outside-of-rails-a-nameerror-is-thrown
|
10
|
-
ActiveRecord::ActiveRecordError
|
11
|
-
end
|
12
|
-
|
13
|
-
require plugin_test_dir + '/../init.rb'
|
14
|
-
|
15
|
-
ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/test.log")
|
16
|
-
|
17
|
-
ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + "/db/database.yml"))
|
18
|
-
ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem")
|
19
|
-
ActiveRecord::Migration.verbose = false
|
20
|
-
load(File.join(plugin_test_dir, "db", "schema.rb"))
|
21
|
-
|
22
|
-
Dir["#{plugin_test_dir}/models/*.rb"].each {|file| require file }
|
23
|
-
|
24
|
-
RSpec.configure do |config|
|
25
|
-
config.before do
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class ActiveRecord::Base
|
30
|
-
|
31
|
-
# Compatibility method for AR 2.3.x and AR 3.2.x
|
32
|
-
def get_error attr
|
33
|
-
if errors.respond_to?(:on)
|
34
|
-
errors.on(attr)
|
35
|
-
else
|
36
|
-
errors[attr].try(:first)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
plugin_test_dir = File.dirname(__FILE__)
|
3
|
+
|
4
|
+
|
5
|
+
require 'active_record'
|
6
|
+
if ActiveRecord::VERSION::STRING >= "3"
|
7
|
+
require 'logger'
|
8
|
+
else
|
9
|
+
# Workaround for https://rails.lighthouseapp.com/projects/8994/tickets/2577-when-using-activerecordassociations-outside-of-rails-a-nameerror-is-thrown
|
10
|
+
ActiveRecord::ActiveRecordError
|
11
|
+
end
|
12
|
+
|
13
|
+
require plugin_test_dir + '/../init.rb'
|
14
|
+
|
15
|
+
ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/test.log")
|
16
|
+
|
17
|
+
ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + "/db/database.yml"))
|
18
|
+
ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem")
|
19
|
+
ActiveRecord::Migration.verbose = false
|
20
|
+
load(File.join(plugin_test_dir, "db", "schema.rb"))
|
21
|
+
|
22
|
+
Dir["#{plugin_test_dir}/models/*.rb"].each {|file| require file }
|
23
|
+
|
24
|
+
RSpec.configure do |config|
|
25
|
+
config.before do
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class ActiveRecord::Base
|
30
|
+
|
31
|
+
# Compatibility method for AR 2.3.x and AR 3.2.x
|
32
|
+
def get_error attr
|
33
|
+
if errors.respond_to?(:on)
|
34
|
+
errors.on(attr)
|
35
|
+
else
|
36
|
+
errors[attr].try(:first)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def ar2?
|
42
|
+
ActiveRecord::VERSION::STRING < "3"
|
43
|
+
end
|
44
|
+
|
45
|
+
def ar4?
|
46
|
+
ActiveRecord::VERSION::STRING >= "4"
|
47
|
+
end
|
metadata
CHANGED
@@ -1,114 +1,104 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: deferred_associations
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.5
|
4
5
|
prerelease:
|
5
|
-
version: 0.5.4
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
authors:
|
8
|
+
- Martin Koerner
|
9
|
+
- Tyler Rick
|
10
|
+
- Alessio Caiazza
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
14
|
+
date: 2013-10-25 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '0'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ! '>='
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '0'
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: rspec
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
description: ! "Makes ActiveRecord defer/postpone saving the records you add to an
|
49
|
+
habtm (has_and_belongs_to_many) or has_many\n association
|
50
|
+
until you call model.save, allowing validation in the style of normal attributes.
|
51
|
+
Additionally you\n can check inside before_save filters, if
|
52
|
+
the association was altered."
|
43
53
|
email: martin.koerner@objectfab.de
|
44
54
|
executables: []
|
45
|
-
|
46
55
|
extensions: []
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
files:
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
- spec/has_many_with_deferred_save_spec.rb
|
72
|
-
- spec/models/chair.rb
|
73
|
-
- spec/models/door.rb
|
74
|
-
- spec/models/person.rb
|
75
|
-
- spec/models/room.rb
|
76
|
-
- spec/models/table.rb
|
77
|
-
- spec/spec_helper.rb
|
56
|
+
extra_rdoc_files:
|
57
|
+
- CHANGELOG
|
58
|
+
- Readme.markdown
|
59
|
+
files:
|
60
|
+
- CHANGELOG
|
61
|
+
- Rakefile
|
62
|
+
- Readme.markdown
|
63
|
+
- VERSION
|
64
|
+
- deferred_associations.gemspec
|
65
|
+
- init.rb
|
66
|
+
- lib/array_to_association_wrapper.rb
|
67
|
+
- lib/deferred_associations.rb
|
68
|
+
- lib/has_and_belongs_to_many_with_deferred_save.rb
|
69
|
+
- lib/has_many_with_deferred_save.rb
|
70
|
+
- spec/db/database.yml
|
71
|
+
- spec/db/schema.rb
|
72
|
+
- spec/has_and_belongs_to_many_with_deferred_save_spec.rb
|
73
|
+
- spec/has_many_with_deferred_save_spec.rb
|
74
|
+
- spec/models/chair.rb
|
75
|
+
- spec/models/door.rb
|
76
|
+
- spec/models/person.rb
|
77
|
+
- spec/models/room.rb
|
78
|
+
- spec/models/table.rb
|
79
|
+
- spec/spec_helper.rb
|
78
80
|
homepage: http://github.com/MartinKoerner/deferred_associations
|
79
81
|
licenses: []
|
80
|
-
|
81
82
|
post_install_message:
|
82
|
-
rdoc_options:
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
87
|
none: false
|
88
|
-
requirements:
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
93
|
none: false
|
94
|
-
requirements:
|
95
|
-
|
96
|
-
|
97
|
-
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
98
|
requirements: []
|
99
|
-
|
100
99
|
rubyforge_project:
|
101
|
-
rubygems_version: 1.8.
|
100
|
+
rubygems_version: 1.8.24
|
102
101
|
signing_key:
|
103
102
|
specification_version: 3
|
104
103
|
summary: Makes ActiveRecord defer/postpone habtm or has_many associations
|
105
|
-
test_files:
|
106
|
-
- spec/has_and_belongs_to_many_with_deferred_save_spec.rb
|
107
|
-
- spec/has_many_with_deferred_save_spec.rb
|
108
|
-
- spec/spec_helper.rb
|
109
|
-
- spec/db/schema.rb
|
110
|
-
- spec/models/chair.rb
|
111
|
-
- spec/models/door.rb
|
112
|
-
- spec/models/person.rb
|
113
|
-
- spec/models/room.rb
|
114
|
-
- spec/models/table.rb
|
104
|
+
test_files: []
|