fixturex 0.1.0 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ea06be8bc96efed28b5178afcb2067df1f62e33ffcbab770b4e9f8981d7c4c7
4
- data.tar.gz: bdf4a376118d64737bd158430543dfa8cd4f072f5b47f22aee79f9d5faf316c5
3
+ metadata.gz: 4ad838e7a474e177400e7dc181b273ec0ee88d3a785ad94e60348a8b6bb6e8d3
4
+ data.tar.gz: d5c896e40834be325036a219fe6955a672becfc7cc67283ae37be20457c2e1a4
5
5
  SHA512:
6
- metadata.gz: a84ac2f32931c6302e3fe0f7d51e7857faf55ed82c84d333b6c9f5989737367859574e1c53a70a296bda588073e8233ad9f8b4e29b8a726b372957d75d7bdcb7
7
- data.tar.gz: df1e9d6e317c08ffb17d1d657220486d499b78a3ac3630290ff9834787b4b1c598e58f29f9fdf8e661bb0a7a5aa23b2abc7cc9b41fadbdfbc6be4967de4d3ae0
6
+ metadata.gz: b5b4b7a1068f0e9b98e4780f6cdc21e93ca1544f7934103c87a528922d98374f9503b779561ea49fc3d2c18e028b7bccb61d6b361667ce6b8bc3607b5ed1e713
7
+ data.tar.gz: 72df98e71b21abfa80403ded5d8a5549a5fd134fc24b76ff80e18d8771c46e4aee23312113a94b55b9c53b2e214a090835e03d2d333b6995c48f9e4528e63767
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.3)
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)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Fixturex [![Ruby](https://github.com/artemave/fixturex/actions/workflows/ruby.yml/badge.svg)](https://github.com/artemave/fixturex/actions/workflows/ruby.yml)
1
+ # Fixturex [![Ruby](https://github.com/artemave/fixturex/actions/workflows/ruby.yml/badge.svg)](https://github.com/artemave/fixturex/actions/workflows/ruby.yml) [![Gem Version](https://badge.fury.io/rb/fixturex.svg)](https://badge.fury.io/rb/fixturex)
2
2
 
3
3
  Rails fixtures explorer.
4
4
 
@@ -10,7 +10,7 @@ module Fixturex
10
10
 
11
11
  def line
12
12
  @line ||= File.readlines(path).index do |line|
13
- line.match?(/^#{name}:/)
13
+ line.match?(/^#{Regexp.escape(name)}:/)
14
14
  end + 1
15
15
  end
16
16
 
@@ -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
@@ -55,6 +54,10 @@ module Fixturex
55
54
  end
56
55
 
57
56
  class TreeBuilder
57
+ def self.cache
58
+ @cache ||= {}
59
+ end
60
+
58
61
  def build_dependency_tree(fixture_path, fixture_name)
59
62
  TreeEntry.new(
60
63
  FixtureLocation.new(fixture_path, fixture_name),
@@ -64,10 +67,13 @@ module Fixturex
64
67
 
65
68
  private
66
69
 
70
+ # rubocop:disable Metrics/AbcSize
67
71
  def nested_fixtures_locations(parent_fixture_model_class, parent_fixture_name)
68
72
  associations_for_nested_models(parent_fixture_model_class).each_with_object([]) do |association, acc|
69
- belongs_to_attribute = belongs_to_attribute_for_association(association)
70
- model_fixtures = ModelFixtures.load(association.class_name)
73
+ next unless (belongs_to_attribute = belongs_to_attribute_for_association(association))
74
+
75
+ child_model_class = association_model_class(association)
76
+ model_fixtures = self.class.cache[association.class_name] ||= ModelFixtures.load(child_model_class)
71
77
 
72
78
  model_fixtures.each do |fixture|
73
79
  next if fixture.attributes.fetch(belongs_to_attribute, '').to_s.sub(/ .*/, '') != parent_fixture_name
@@ -78,6 +84,18 @@ module Fixturex
78
84
  end
79
85
  end
80
86
  end
87
+ # rubocop:enable Metrics/AbcSize
88
+
89
+ def association_model_class(association)
90
+ class_name = association.class_name
91
+
92
+ unless Object.const_defined?(class_name)
93
+ namespace = association.active_record.name.split('::')[0..-2].join('::')
94
+ class_name = "#{namespace}::#{class_name}"
95
+ end
96
+
97
+ class_name.constantize
98
+ end
81
99
 
82
100
  def fixture_already_collected(list, fixture)
83
101
  list.find do |tree_entry|
@@ -86,15 +104,22 @@ module Fixturex
86
104
  end
87
105
 
88
106
  def associations_for_nested_models(model_class)
89
- model_class.reflect_on_all_associations(:has_many) +
107
+ (
108
+ model_class.reflect_on_all_associations(:has_many) +
90
109
  model_class.reflect_on_all_associations(:has_one)
110
+ ).reject do |association|
111
+ association.is_a?(ActiveRecord::Reflection::ThroughReflection)
112
+ end
91
113
  end
92
114
 
93
115
  def belongs_to_attribute_for_association(association)
94
116
  if association.type.present?
95
117
  association.type.sub('_type', '')
96
118
  else
97
- association.active_record.name.tableize.singularize
119
+ child_model_class = association_model_class(association)
120
+ child_model_class.reflect_on_all_associations(:belongs_to).find do |belongs_to_association|
121
+ belongs_to_association.class_name == association.active_record.name
122
+ end&.name&.to_s
98
123
  end
99
124
  end
100
125
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fixturex
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.3'
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.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - artemave
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-01 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