schema_associations 1.2.6 → 1.2.7

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
- SHA1:
3
- metadata.gz: c080f0a0fd14c82a61269dc1eb6e1e111ed1c331
4
- data.tar.gz: 6e0a834bf340751c07ec3614ca34a712c35e884e
2
+ SHA256:
3
+ metadata.gz: 44cb70a92c74e94032088d96228cf1622875c341c4e4f5b9d7563786dde6cf0d
4
+ data.tar.gz: 2a8fa7848d6fc166fc648ed761e075a508b14ed9b31b4dd64364bb76ae221205
5
5
  SHA512:
6
- metadata.gz: 06b8e5871f6cb1ee9751571a739a05560c2f5f6d787450e3c69d893e3fa2cb43875778e1f7022c6d5436baba937ddbb29e903bba0de1798904bb7743c9fa3328
7
- data.tar.gz: 4101db526873abab3e101e0bf8852f55372c272fe9c116169243a676d278455defe4c18b752c6fcf71d6ba99c54154084fe8dc851b792bb2af8aa5b860d8ddc4
6
+ metadata.gz: 78104847107eef1060cbb84245730fe60e2c900f1416b969cfb9d3e2359db705d53a150f9e3435422cee7d67c7ae4cca4cfedf8fbdbe73f642c12a2da794a68a
7
+ data.tar.gz: cbe12ce462e1faeb6db6f45fd0fc902a61348e65093ade4ca453943b855928a16cc5b67324fc3b8dc358e697ef4af3da5b69eb37d7a15f5d4fc586dd901da3cf
@@ -13,6 +13,12 @@ gemfile:
13
13
  - gemfiles/activerecord-5.0/Gemfile.mysql2
14
14
  - gemfiles/activerecord-5.0/Gemfile.postgresql
15
15
  - gemfiles/activerecord-5.0/Gemfile.sqlite3
16
+ - gemfiles/activerecord-5.1/Gemfile.mysql2
17
+ - gemfiles/activerecord-5.1/Gemfile.postgresql
18
+ - gemfiles/activerecord-5.1/Gemfile.sqlite3
19
+ - gemfiles/activerecord-5.2/Gemfile.mysql2
20
+ - gemfiles/activerecord-5.2/Gemfile.postgresql
21
+ - gemfiles/activerecord-5.2/Gemfile.sqlite3
16
22
  env: POSTGRESQL_DB_USER=postgres MYSQL_DB_USER=travis
17
23
  addons:
18
24
  postgresql: '9.4'
data/README.md CHANGED
@@ -205,7 +205,7 @@ If your forward relation is named "parent", SchemaAssociations names the
205
205
  reverse relation "child" or "children". That is, if you have:
206
206
 
207
207
  ```ruby
208
- create_table :nodes
208
+ create_table :nodes do |t|
209
209
  t.integer :parent_id # schema_plus assumes it's a reference to this table
210
210
  end
211
211
  ```
@@ -225,14 +225,14 @@ For modularity in your tables and classes, you might use a common prefix for
225
225
  related objects. For example, you may have widgets each of which has a color, and each widget might have one frob that has a top color and a bottom color--all from the same set of colors.
226
226
 
227
227
  ```ruby
228
- create_table :widget_colors |t|
228
+ create_table :widget_colors do |t|
229
229
  end
230
230
 
231
231
  create_table :widgets do |t|
232
232
  t.integer :widget_color_id
233
233
  end
234
234
 
235
- create_table :widget_frobs
235
+ create_table :widget_frobs do |t|
236
236
  t.integer :widget_id, index: :unique
237
237
  t.integer :top_widget_color_id, references: :widget_colors
238
238
  t.integer :bottom_widget_color_id, references: :widget_colors
@@ -420,6 +420,8 @@ SchemaAssociations is tested on all combinations of:
420
420
  <!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->
421
421
  * ruby **2.3.1** with activerecord **4.2**, using **mysql2**, **postgresql** or **sqlite3**
422
422
  * ruby **2.3.1** with activerecord **5.0**, using **mysql2**, **postgresql** or **sqlite3**
423
+ * ruby **2.3.1** with activerecord **5.1**, using **mysql2**, **postgresql** or **sqlite3**
424
+ * ruby **2.3.1** with activerecord **5.2**, using **mysql2**, **postgresql** or **sqlite3**
423
425
 
424
426
  <!-- SCHEMA_DEV: MATRIX - end -->
425
427
 
@@ -454,6 +456,10 @@ Code coverage results will be in coverage/index.html -- it should be at 100% cov
454
456
 
455
457
  ## Release notes:
456
458
 
459
+ ### 1.2.7
460
+
461
+ * add in auto deferring of has_* :through associations manually defined on the model so they work in AR 5.1+
462
+
457
463
  ### 1.2.6
458
464
 
459
465
  * Support for AR5 (Rails 5).
@@ -2,9 +2,9 @@ require "pathname"
2
2
  eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)
3
3
 
4
4
  platform :ruby do
5
- gem "pg"
5
+ gem "pg", "< 1"
6
6
  end
7
7
 
8
8
  platform :jruby do
9
9
  gem 'activerecord-jdbcpostgresql-adapter'
10
- end
10
+ end
@@ -1,3 +1,3 @@
1
1
  eval File.read File.expand_path('../../Gemfile.base', __FILE__)
2
2
 
3
- gem "activerecord", "~> 5.0.0"
3
+ gem "activerecord", "~> 5.0.1"
@@ -2,9 +2,9 @@ require "pathname"
2
2
  eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)
3
3
 
4
4
  platform :ruby do
5
- gem "pg"
5
+ gem "pg", "< 1"
6
6
  end
7
7
 
8
8
  platform :jruby do
9
9
  gem 'activerecord-jdbcpostgresql-adapter'
10
- end
10
+ end
@@ -0,0 +1,3 @@
1
+ eval File.read File.expand_path('../../Gemfile.base', __FILE__)
2
+
3
+ gem "activerecord", "~> 5.1.0"
@@ -0,0 +1,10 @@
1
+ require "pathname"
2
+ eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)
3
+
4
+ platform :ruby do
5
+ gem "mysql2"
6
+ end
7
+
8
+ platform :jruby do
9
+ gem 'activerecord-jdbcmysql-adapter'
10
+ end
@@ -0,0 +1,10 @@
1
+ require "pathname"
2
+ eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)
3
+
4
+ platform :ruby do
5
+ gem "pg", "< 1"
6
+ end
7
+
8
+ platform :jruby do
9
+ gem 'activerecord-jdbcpostgresql-adapter'
10
+ end
@@ -0,0 +1,10 @@
1
+ require "pathname"
2
+ eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)
3
+
4
+ platform :ruby do
5
+ gem "sqlite3"
6
+ end
7
+
8
+ platform :jruby do
9
+ gem 'activerecord-jdbcsqlite3-adapter', '>=1.3.0.beta2'
10
+ end
@@ -0,0 +1,3 @@
1
+ eval File.read File.expand_path('../../Gemfile.base', __FILE__)
2
+
3
+ gem "activerecord", ">= 5.2.0.beta0", "< 5.3"
@@ -0,0 +1,10 @@
1
+ require "pathname"
2
+ eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)
3
+
4
+ platform :ruby do
5
+ gem "mysql2"
6
+ end
7
+
8
+ platform :jruby do
9
+ gem 'activerecord-jdbcmysql-adapter'
10
+ end
@@ -0,0 +1,10 @@
1
+ require "pathname"
2
+ eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)
3
+
4
+ platform :ruby do
5
+ gem "pg"
6
+ end
7
+
8
+ platform :jruby do
9
+ gem 'activerecord-jdbcpostgresql-adapter'
10
+ end
@@ -0,0 +1,10 @@
1
+ require "pathname"
2
+ eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)
3
+
4
+ platform :ruby do
5
+ gem "sqlite3"
6
+ end
7
+
8
+ platform :jruby do
9
+ gem 'activerecord-jdbcsqlite3-adapter', '>=1.3.0.beta2'
10
+ end
@@ -15,6 +15,11 @@ module SchemaAssociations
15
15
 
16
16
  module ClassMethods
17
17
 
18
+ def reflections(*args)
19
+ _load_schema_associations_associations
20
+ super
21
+ end
22
+
18
23
  def reflect_on_association(*args)
19
24
  _load_schema_associations_associations
20
25
  super
@@ -57,7 +62,6 @@ module SchemaAssociations
57
62
  # schema_associations :concise_names => false, :except_type => :has_and_belongs_to_many
58
63
  # end
59
64
  #
60
- #
61
65
  def schema_associations(opts={})
62
66
  @schema_associations_config = SchemaAssociations.config.merge({:auto_create => true}.merge(opts))
63
67
  end
@@ -66,6 +70,17 @@ module SchemaAssociations
66
70
  @schema_associations_config ||= SchemaAssociations.config.dup
67
71
  end
68
72
 
73
+ %i[has_many has_one].each do |m|
74
+ define_method(m) do |name, *args|
75
+ if @schema_associations_associations_loaded
76
+ super name, *args
77
+ else
78
+ @schema_associations_deferred_associations ||= []
79
+ @schema_associations_deferred_associations.push({macro: m, name: name, args: args})
80
+ end
81
+ end
82
+ end
83
+
69
84
  private
70
85
 
71
86
  def _load_schema_associations_associations
@@ -73,6 +88,8 @@ module SchemaAssociations
73
88
  return if abstract_class?
74
89
  return unless schema_associations_config.auto_create?
75
90
 
91
+ @schema_associations_associations_loaded = :loading
92
+
76
93
  reverse_foreign_keys.each do | foreign_key |
77
94
  if foreign_key.from_table =~ /^#{table_name}_(.*)$/ || foreign_key.from_table =~ /^(.*)_#{table_name}$/
78
95
  other_table = $1
@@ -90,6 +107,14 @@ module SchemaAssociations
90
107
  _define_association(:belongs_to, foreign_key)
91
108
  end
92
109
 
110
+ (@schema_associations_deferred_associations || []).each do |a|
111
+ argstr = a[:args].inspect[1...-1] + ' # deferred association'
112
+ _create_association(a[:macro], a[:name], argstr, *a[:args])
113
+ end
114
+ if instance_variable_defined? :@schema_associations_deferred_associations
115
+ remove_instance_variable :@schema_associations_deferred_associations
116
+ end
117
+
93
118
  @schema_associations_associations_loaded = true
94
119
  end
95
120
 
@@ -159,7 +184,6 @@ module SchemaAssociations
159
184
  end
160
185
  end
161
186
 
162
-
163
187
  def _determine_association_names(reference_name, referencing_name, references_name)
164
188
 
165
189
  references_concise = _concise_name(references_name, referencing_name)
@@ -1,3 +1,3 @@
1
1
  module SchemaAssociations
2
- VERSION = "1.2.6"
2
+ VERSION = "1.2.7"
3
3
  end
@@ -3,6 +3,8 @@ ruby:
3
3
  activerecord:
4
4
  - 4.2
5
5
  - 5.0
6
+ - 5.1
7
+ - 5.2
6
8
  db:
7
9
  - mysql2
8
10
  - postgresql
@@ -7,6 +7,8 @@ describe ActiveRecord::Base do
7
7
  remove_all_models
8
8
  end
9
9
 
10
+ let(:pk_type) { Gem::Requirement.new('< 5.1').satisfied_by?(::ActiveRecord.version) ? :integer : :bigint }
11
+
10
12
  context "in basic case" do
11
13
  before(:each) do
12
14
  create_tables(
@@ -568,6 +570,33 @@ describe ActiveRecord::Base do
568
570
  end
569
571
  end
570
572
 
573
+ context 'defining has_many through associations' do
574
+ before(:each) do
575
+ create_tables(
576
+ "users", {}, {},
577
+ "posts", {}, { :user_id => { foreign_key: true}},
578
+ "comments", {}, { :post_id => { foreign_key: true}},
579
+ )
580
+ class Post < ActiveRecord::Base; end
581
+ class Comment < ActiveRecord::Base; end
582
+ class User < ActiveRecord::Base
583
+ has_many :comments, :through => :posts
584
+ end
585
+ end
586
+
587
+ it 'should not error when accessing the through association' do
588
+ reflection = User.reflect_on_association(:posts)
589
+ expect(reflection).not_to be_nil
590
+
591
+ reflection = User.reflect_on_association(:comments)
592
+ expect(reflection).not_to be_nil
593
+ expect(reflection.macro).to eq(:has_many)
594
+ expect(reflection.options[:through]).to eq(:posts)
595
+
596
+ expect { User.new.comments }.to_not raise_error
597
+ end
598
+ end
599
+
571
600
  context "regarding existing methods" do
572
601
  before(:each) do
573
602
  create_tables(
@@ -702,7 +731,7 @@ describe ActiveRecord::Base do
702
731
  table_defs.each_slice(3) do |table_name, opts, columns_with_options|
703
732
  ActiveRecord::Migration.create_table table_name, opts do |t|
704
733
  columns_with_options.each_pair do |column, options|
705
- coltype = options.delete(:coltype) || :integer
734
+ coltype = options.delete(:coltype) || pk_type
706
735
  t.send coltype, column, options
707
736
  end
708
737
  end
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.6
4
+ version: 1.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronen Barzel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-09-11 00:00:00.000000000 Z
12
+ date: 2018-12-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: schema_plus_foreign_keys
@@ -136,6 +136,14 @@ files:
136
136
  - gemfiles/activerecord-5.0/Gemfile.mysql2
137
137
  - gemfiles/activerecord-5.0/Gemfile.postgresql
138
138
  - gemfiles/activerecord-5.0/Gemfile.sqlite3
139
+ - gemfiles/activerecord-5.1/Gemfile.base
140
+ - gemfiles/activerecord-5.1/Gemfile.mysql2
141
+ - gemfiles/activerecord-5.1/Gemfile.postgresql
142
+ - gemfiles/activerecord-5.1/Gemfile.sqlite3
143
+ - gemfiles/activerecord-5.2/Gemfile.base
144
+ - gemfiles/activerecord-5.2/Gemfile.mysql2
145
+ - gemfiles/activerecord-5.2/Gemfile.postgresql
146
+ - gemfiles/activerecord-5.2/Gemfile.sqlite3
139
147
  - init.rb
140
148
  - lib/schema_associations.rb
141
149
  - lib/schema_associations/active_record/associations.rb
@@ -164,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
172
  version: '0'
165
173
  requirements: []
166
174
  rubyforge_project: schema_associations
167
- rubygems_version: 2.5.1
175
+ rubygems_version: 2.7.8
168
176
  signing_key:
169
177
  specification_version: 4
170
178
  summary: ActiveRecord extension that automatically (DRY) creates associations based