fixturex 0.1.1 → 0.1.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
  SHA256:
3
- metadata.gz: 517987007a6e6ca80e06f5f2159c45ebdd81e6824c223219cfd55f6a1a07ebec
4
- data.tar.gz: 0ae40367ec829d9adb8292ad32d61da2344c3afd8928696298764f33a219368e
3
+ metadata.gz: 969e8e5573328295b3bd63b0ec6ec2bef3f3462a613009f44ac5f8dfc9ba5c7d
4
+ data.tar.gz: 02c925a94649f82fb08c7d5c1b555b10a95546054db19a864216717239da213d
5
5
  SHA512:
6
- metadata.gz: 165341e0beb555401e373a8c3314cfd5bfd5020aaf4cab69ed27726a8ee1aa2ce73bbdc98b6235c8d428e1fb70b8dd382a0e0b3f832fefc1005d3c2db714ed6e
7
- data.tar.gz: 00a86359a92354f61caeacf69cdc1b7cccc800df9141a27aed9903d83152555824b60a76c77b3b7ec28bc25e0c0d4603bcef4534485255244dcdf338576e27e1
6
+ metadata.gz: 6477664978fff4a5ee9ceee56decf5c96ceadacba17f02c81f15adde91e1678b72b4bf183b64ddfd3333396cf5a3691c1015f0095c9e93f69e9bc29fe051f53f
7
+ data.tar.gz: d7f085be682df75a9dd849c7dd1473d97d56bea48090738def17095ec11f409fb23c43e32e33516f6c4158e546130de460ee3b332ebd83ee4db67045d8da9e71
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fixturex (0.1.0)
4
+ fixturex (0.1.2)
5
5
  rails (>= 6.0)
6
6
 
7
7
  GEM
@@ -75,7 +75,7 @@ GEM
75
75
  diff-lcs (1.4.4)
76
76
  docile (1.4.0)
77
77
  erubi (1.10.0)
78
- globalid (0.5.2)
78
+ globalid (1.0.0)
79
79
  activesupport (>= 5.0)
80
80
  i18n (1.8.10)
81
81
  concurrent-ruby (~> 1.0)
@@ -84,9 +84,9 @@ GEM
84
84
  nokogiri (>= 1.5.9)
85
85
  mail (2.7.1)
86
86
  mini_mime (>= 0.1.1)
87
- marcel (1.0.1)
87
+ marcel (1.0.2)
88
88
  method_source (1.0.0)
89
- mini_mime (1.1.1)
89
+ mini_mime (1.1.2)
90
90
  mini_portile2 (2.6.1)
91
91
  minitest (5.14.4)
92
92
  nio4r (2.5.8)
@@ -169,12 +169,12 @@ GEM
169
169
  simplecov_json_formatter (~> 0.1)
170
170
  simplecov-html (0.12.3)
171
171
  simplecov_json_formatter (0.1.3)
172
- sprockets (4.0.2)
172
+ sprockets (4.1.1)
173
173
  concurrent-ruby (~> 1.0)
174
174
  rack (> 1, < 3)
175
- sprockets-rails (3.2.2)
176
- actionpack (>= 4.0)
177
- activesupport (>= 4.0)
175
+ sprockets-rails (3.4.2)
176
+ actionpack (>= 5.2)
177
+ activesupport (>= 5.2)
178
178
  sprockets (>= 3.0.0)
179
179
  sqlite3 (1.4.2)
180
180
  thor (1.1.0)
@@ -11,30 +11,29 @@ module Fixturex
11
11
  class ModelFixtures
12
12
  Fixture = Struct.new(:name, :path, :attributes)
13
13
 
14
- def self.load(class_name)
15
- fixtures_paths(class_name).each_with_object([]) do |path, acc|
14
+ def self.load(model_class)
15
+ fixtures_paths(model_class).each_with_object([]) do |path, acc|
16
16
  fixtures = YAML.load_file(path)
17
17
  fixtures.select! do |_name, attributes|
18
18
  # if fixture has `type` - STI - then we only want type == class_name
19
- attributes['type'].nil? || attributes['type'] == class_name
19
+ attributes['type'].nil? || attributes['type'] == model_class.name
20
20
  end
21
21
  acc.concat(fixtures.map { |name, attributes| Fixture.new(name, path, attributes) })
22
22
  end
23
23
  end
24
24
 
25
- def self.fixtures_paths(class_name)
25
+ def self.fixtures_paths(model_class)
26
26
  fixtures_paths = []
27
- klass = class_name.constantize
28
27
  # TODO: is there a better way to find out fixtures root directory?
29
28
  fixtures_root = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
30
29
 
31
- while klass < ActiveRecord::Base
32
- fixture_file = "#{klass.to_s.tableize}.yml"
30
+ while model_class < ActiveRecord::Base
31
+ fixture_file = "#{model_class.to_s.tableize}.yml"
33
32
  path = File.join(fixtures_root, *fixture_file.split('/'))
34
33
 
35
34
  fixtures_paths << path if File.exist?(path)
36
35
 
37
- klass = klass.superclass
36
+ model_class = model_class.superclass
38
37
  end
39
38
  fixtures_paths
40
39
  end
@@ -68,10 +67,12 @@ module Fixturex
68
67
 
69
68
  private
70
69
 
70
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
71
71
  def nested_fixtures_locations(parent_fixture_model_class, parent_fixture_name)
72
72
  associations_for_nested_models(parent_fixture_model_class).each_with_object([]) do |association, acc|
73
73
  belongs_to_attribute = belongs_to_attribute_for_association(association)
74
- model_fixtures = self.class.cache[association.class_name] ||= ModelFixtures.load(association.class_name)
74
+ child_model_class = association_model_class(association)
75
+ model_fixtures = self.class.cache[association.class_name] ||= ModelFixtures.load(child_model_class)
75
76
 
76
77
  model_fixtures.each do |fixture|
77
78
  next if fixture.attributes.fetch(belongs_to_attribute, '').to_s.sub(/ .*/, '') != parent_fixture_name
@@ -82,6 +83,18 @@ module Fixturex
82
83
  end
83
84
  end
84
85
  end
86
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
87
+
88
+ def association_model_class(association)
89
+ class_name = association.class_name
90
+
91
+ unless Object.const_defined?(class_name)
92
+ namespace = association.active_record.name.split('::')[0..-2].join('::')
93
+ class_name = "#{namespace}::#{class_name}"
94
+ end
95
+
96
+ class_name.constantize
97
+ end
85
98
 
86
99
  def fixture_already_collected(list, fixture)
87
100
  list.find do |tree_entry|
@@ -98,7 +111,10 @@ module Fixturex
98
111
  if association.type.present?
99
112
  association.type.sub('_type', '')
100
113
  else
101
- association.active_record.name.tableize.singularize
114
+ child_model_class = association_model_class(association)
115
+ child_model_class.reflect_on_all_associations(:belongs_to).find do |belongs_to_association|
116
+ belongs_to_association.class_name == association.active_record.name
117
+ end.name.to_s
102
118
  end
103
119
  end
104
120
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fixturex
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixturex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - artemave
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-27 00:00:00.000000000 Z
11
+ date: 2022-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails