railnroll 0.0.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rail_n_roll.rb +74 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 818059c1c39ff62b1ee4c1211a826ef0e0ea8ce1f2ae52707d9c407ae81e2d58
4
+ data.tar.gz: edafe3b68792462fed08ec695b862340a9fc8c17784cd67ec1d79b9b7ffde86c
5
+ SHA512:
6
+ metadata.gz: c336b05961b7b504e07a09f60e0254b70410f8b7c0b980350cc52a457cdc9074f09f721cd40634c606e58df7934e639532a132294e31adbfe0599e6beda355fa
7
+ data.tar.gz: 2db8e9a84665cafd91125549b08563e568e2591ac9a087323704844958d399ef970636c9694c279a04fe23de4d4bb9827ce78505dd8c4486556f7f2a249dfa6b
@@ -0,0 +1,74 @@
1
+ class RailNRoll
2
+ class << self
3
+ def add_to_fixture(fixture_name, object, ignored_has_many_associations:[])
4
+ return unless object
5
+
6
+ attributes = fixturable_attributes(object)
7
+
8
+ existing_data = fixture_data(object)
9
+ existing_data[fixture_name.to_s] = attributes
10
+
11
+ File.open(Rails.root.join('test', 'fixtures', fixture_file(object)), 'w') do |file|
12
+ file.write(YAML.dump(existing_data))
13
+ end
14
+
15
+ object.class.reflections.each do |association_name, reflection|
16
+ next if ignored_has_many_associations.include? association_name
17
+ if reflection.macro == :has_many
18
+ object.send(association_name).each_with_index do |child, index|
19
+ next if fixture_name_for(child)
20
+ add_to_fixture "#{fixture_name}_#{index + 1}", child
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ protected
27
+
28
+ def fixturable_attributes(object)
29
+ attributes = object.attributes.except("id", "created_at", "updated_at")
30
+
31
+ object.class.reflections.each do |association_name, reflection|
32
+ if reflection.macro == :belongs_to
33
+ attributes = attributes.except(reflection.foreign_key)
34
+ foreign_object = object.send(association_name)
35
+ foreign_object_fixture_name = fixture_name_for(foreign_object)
36
+ # TODO: Check that the foreign object actually responds to the inverse
37
+
38
+ attributes[association_name] = foreign_object_fixture_name ? foreign_object_fixture_name : create_fixture(foreign_object, [reflection.inverse_of&.name || object.class.name.underscore.pluralize ])
39
+ end
40
+ end
41
+ attributes
42
+ end
43
+
44
+ def create_fixture(object, ignored_has_many_associations)
45
+ fixture_name = object.to_s.split(" ").first.downcase
46
+ add_to_fixture(fixture_name, object, ignored_has_many_associations:)
47
+ fixture_name
48
+ end
49
+
50
+ def fixture_file(object)
51
+ "#{object.class.name.underscore.pluralize}.yml"
52
+ end
53
+
54
+ def fixture_data(object)
55
+ file_path = Rails.root.join('test', 'fixtures', fixture_file(object))
56
+ File.exist?(file_path) ? YAML.load_file(file_path) : {}
57
+ end
58
+
59
+ def fixture_name_for(object)
60
+ data = fixture_data(object)
61
+ return if data.empty?
62
+
63
+ attributes = fixturable_attributes(object)
64
+
65
+ data.each do |fixture_key, data|
66
+ return fixture_key if data == attributes
67
+ end
68
+ return nil
69
+ end
70
+ end
71
+
72
+
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: railnroll
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniela Velasquez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-04-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Fixtures created by active record models
14
+ email: daniela.velasquez.g@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rail_n_roll.rb
20
+ homepage: https://rubygems.org/gems/fixture_factory
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.3.7
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A factory to create reliable fixtures
43
+ test_files: []