m4dbi 0.8.4 → 0.8.5

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.
@@ -122,6 +122,7 @@ module M4DBI
122
122
  values = keys.map { |key| hash[ key ] }
123
123
  value_placeholders = values.map { |v| '?' }.join( ',' )
124
124
  rec = nil
125
+ num_inserted = 0
125
126
 
126
127
  dbh.transaction do |dbh_|
127
128
  if keys.empty? && defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === dbh.driver
@@ -142,23 +143,9 @@ module M4DBI
142
143
  end
143
144
  if ! pk_hash.empty?
144
145
  rec = self.one_where( pk_hash )
145
- if hooks[:active]
146
- hooks[:after_create].each do |block|
147
- hooks[:active] = false
148
- block.yield rec
149
- hooks[:active] = true
150
- end
151
- end
152
146
  else
153
147
  begin
154
148
  rec = last_record( dbh_ )
155
- if hooks[:active]
156
- hooks[:after_create].each do |block|
157
- hooks[:active] = false
158
- block.yield rec
159
- hooks[:active] = true
160
- end
161
- end
162
149
  rescue NoMethodError => e
163
150
  # ignore
164
151
  #puts "not implemented: #{e.message}"
@@ -167,6 +154,14 @@ module M4DBI
167
154
  end
168
155
  end
169
156
 
157
+ if hooks[:active] && num_inserted > 0
158
+ hooks[:after_create].each do |block|
159
+ hooks[:active] = false
160
+ block.yield rec
161
+ hooks[:active] = true
162
+ end
163
+ end
164
+
170
165
  rec
171
166
  end
172
167
 
@@ -1,3 +1,3 @@
1
1
  module M4DBI
2
- VERSION = '0.8.4'
2
+ VERSION = '0.8.5'
3
3
  end
@@ -686,6 +686,16 @@ describe 'A M4DBI::Model subclass' do
686
686
  a = Author.create(name: 'theauthor')
687
687
  $test.should.equal 2
688
688
  a.name.should.equal 'different name'
689
+
690
+ class Post < M4DBI::Model( :posts ); end
691
+ class Author < M4DBI::Model( :authors )
692
+ after_create do |author|
693
+ Post.create(author_id: author.id, text: 'foobar')
694
+ end
695
+ end
696
+ n = Post.count
697
+ a = Author.create(name: 'theauthor')
698
+ Post.count.should == n+1
689
699
  end
690
700
 
691
701
  it 'provides a means to remove all after_create hooks' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: m4dbi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.5
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-07-01 00:00:00.000000000 Z
12
+ date: 2012-07-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: metaid