schema_associations 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3b9c45613e71564c85a31fe61dbe4a568568a23
4
- data.tar.gz: c45c59c0dab8f9e87e396ef90199c6d4f5f6f852
3
+ metadata.gz: 395a9d8c36949f31bba7558d51be3dda54be2bfb
4
+ data.tar.gz: c6807a2b495005257d0d749de2216625e5332899
5
5
  SHA512:
6
- metadata.gz: febbd2e3214f1bd063d68f0e9f93c0a12da75acbe7aefec616892015992cdb2afc4a5a7c31db8bb17200a49032a4daf2f2f10d14247f0018f1f10ee56a101513
7
- data.tar.gz: 7e83f3bf6be38cd776896fa58b1ff21f39cbb1c5b20bff219c9b1dc3fa181ba5a5bcaa36d38acf7f09ed287a8ae8310b7d175a39f5189d8c1963c98311aa47b3
6
+ metadata.gz: d4535711d17dc740000c135b41a42816d3dc5be8c0ad616c292870c43e2948b96132e914fd43a964ac3f6646aeb61e58f78c879b8d06353eafaf8638b12eb568
7
+ data.tar.gz: 112912f68d8a1f3b20151ac5799fd7f997d63b32ad2386c9464158105a2292fd9cf7b7bf3df924a838378b32739a135ebb7405daa047a193b75ec8034e7bc685
data/README.md CHANGED
@@ -353,6 +353,10 @@ Code coverage results will be in coverage/index.html -- it should be at 100% cov
353
353
 
354
354
  ## Release notes:
355
355
 
356
+ ### 1.2.2
357
+
358
+ * Bug fix (Rails workaround) for STI: propagate associations to subclasses, since Rails might not, depending on the load order.
359
+
356
360
  ### 1.2.1
357
361
 
358
362
  * Works with Rails 4.1
@@ -114,6 +114,9 @@ module SchemaAssociations
114
114
 
115
115
  names = _determine_association_names(column_name.sub(/_id$/, ''), referencing_name, references_name)
116
116
 
117
+ argstr = ""
118
+
119
+
117
120
  case macro
118
121
  when :has_and_belongs_to_many
119
122
  name = names[:has_many]
@@ -145,20 +148,33 @@ module SchemaAssociations
145
148
  opts[:order] = :position
146
149
  else
147
150
  scope_block = lambda { order :position }
151
+ argstr += "-> { order :position }, "
148
152
  end
149
153
  end
150
154
  end
151
155
  end
156
+ argstr += opts.inspect[1...-1]
152
157
  if (_filter_association(macro, name) && !_method_exists?(name))
153
- logger.info "[schema_associations] #{self.name || self.table_name.classify}.#{macro} #{name.inspect}, #{opts.inspect[1...-1]}"
154
158
  if ::ActiveRecord::VERSION::MAJOR.to_i < 4
155
- send macro, name, opts.dup
159
+ _create_association(macro, name, argstr, opts.dup)
156
160
  else
157
- send macro, name, scope_block, opts.dup
161
+ _create_association(macro, name, argstr, scope_block, opts.dup)
158
162
  end
159
163
  end
160
164
  end
161
165
 
166
+ def _create_association(macro, name, argstr, *args)
167
+ logger.info "[schema_associations] #{self.name || self.table_name.classify}.#{macro} #{name.inspect}, #{argstr}"
168
+ send macro, name, *args
169
+ case
170
+ when respond_to?(:subclasses) then subclasses
171
+ when respond_to?(:descendants) then descendants
172
+ end.each do |subclass|
173
+ subclass.send :_create_association, macro, name, argstr, *args
174
+ end
175
+ end
176
+
177
+
162
178
  def _determine_association_names(reference_name, referencing_name, references_name)
163
179
 
164
180
  references_concise = _concise_name(references_name, referencing_name)
@@ -1,3 +1,3 @@
1
1
  module SchemaAssociations
2
- VERSION = "1.2.1"
2
+ VERSION = "1.2.2"
3
3
  end
@@ -614,16 +614,26 @@ describe ActiveRecord::Base do
614
614
  before(:each) do
615
615
  create_tables(
616
616
  "posts", {}, {},
617
- "comments", {}, { :post_id => {}, :type => {coltype: :string} }
617
+ "comments", {}, { :post_id => {}, :type => {coltype: :string} },
618
+ "citers", {}, {},
619
+ "citations", {}, { :comment_id => {}, :citer_id => {}}
618
620
  )
619
621
  class Post < ActiveRecord::Base ; end
620
622
  class Comment < ActiveRecord::Base ; end
623
+ class Citation < ActiveRecord::Base ; end
621
624
  class SubComment < Comment ; end
625
+ class OwnComment < Comment
626
+ has_one :citer, :through => :citations
627
+ end
622
628
  end
623
629
 
624
630
  it "defines association for subclass" do
625
631
  expect(SubComment.reflect_on_association(:post)).not_to be_nil
626
632
  end
633
+
634
+ it "defines association for subclass that has its own associations" do
635
+ expect(OwnComment.reflect_on_association(:post)).not_to be_nil
636
+ end
627
637
  end
628
638
 
629
639
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_associations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronen Barzel