fixture_farm 0.1.2 → 0.1.5

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: 8bb437d76da7783d3bcec79d5eb456dfc6279aaf147e4299323c072cb5823ba3
4
- data.tar.gz: 3dbadf4de6166743fb4eeb23c1389479dcdbd6341d0aaed7757478ada4be3042
3
+ metadata.gz: 9120ab7f693c781ea48772f5b4f9d277165771afc073f6ed466c88a5fdea2455
4
+ data.tar.gz: 50b502cab64010626ea929ecf3702b51c4211b98db14a013adb1194c687073a5
5
5
  SHA512:
6
- metadata.gz: 31a03a177602b04b48a047ab1cd0d8820784ebc4fc9524e0577eeac5c165579db49891f739ee15e593b57f631388ada0d47d9d1236e31d5a8da2abbdda9797db
7
- data.tar.gz: d385f242b1d5670f0e1ffc70985f40c5297dcd97f5e25c870f205e06f598faaf8c9e6bb0f45bbccb82cf9d4f1e2614558718dc10d618fb6584ecde162226155d
6
+ metadata.gz: 17eadbd5c25eb70dd41dec53f0727890bb7674ddb25bb349eca51e646e8a81f822b8736fe9e6e5ffb9e5cd7b19f7677b92e574c694dc35814d79f0441c22d6bb
7
+ data.tar.gz: 4403bf7831042e7abce331e3076242cf97c9916ddef4457c08322e956f0a79f9606ed1fd9f348876988741ab277461e15484577698a74d4732d2d0acd8a3a13f
data/README.md CHANGED
@@ -7,6 +7,31 @@ This gem lets you do two things:
7
7
 
8
8
  Generated fixture that `belongs_to` a record from an existing fixture, will reference that fixture by name.
9
9
 
10
+ ### Limitations
11
+
12
+ - doesn't update fixtures
13
+ - doesn't delete fixtures
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'fixture_farm', group: %i[development test]
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ ```bash
26
+ bundle install
27
+ ```
28
+
29
+ Or install it yourself as:
30
+
31
+ ```bash
32
+ gem install fixture_farm
33
+ ```
34
+
10
35
  ## Usage
11
36
 
12
37
  ### Record as you browse
@@ -14,7 +39,13 @@ Generated fixture that `belongs_to` a record from an existing fixture, will refe
14
39
  To record as you browse in development add this to `ApplicationController`:
15
40
 
16
41
  ```ruby
17
- include FixtureFarm::ControllerHook if Rails.env.development?
42
+ include FixtureFarm::ControllerHook if defined?(FixtureFarm)
43
+ ```
44
+
45
+ And in `ApplicationJob` if needed:
46
+
47
+ ```ruby
48
+ include FixtureFarm::ActiveJobHook if defined?(FixtureFarm)
18
49
  ```
19
50
 
20
51
  Then start/stop recording using tasks:
@@ -59,24 +90,5 @@ test 'some stuff does the right thing' do
59
90
  end
60
91
  ```
61
92
 
62
- ## Installation
63
- Add this line to your application's Gemfile:
64
-
65
- ```ruby
66
- gem 'fixture_farm', group: %i[development test]
67
- ```
68
-
69
- And then execute:
70
-
71
- ```bash
72
- bundle install
73
- ```
74
-
75
- Or install it yourself as:
76
-
77
- ```bash
78
- gem install fixture_farm
79
- ```
80
-
81
93
  ## License
82
94
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fixture_farm/hook'
4
+
5
+ module FixtureFarm
6
+ module ActiveJobHook
7
+ extend ActiveSupport::Concern
8
+ include Hook
9
+
10
+ included do
11
+ around_perform :record_new_fixtures, if: :record_new_fixtures?
12
+ end
13
+ end
14
+ end
@@ -5,15 +5,29 @@ module FixtureFarm
5
5
  def fixture_name
6
6
  require 'active_record/fixtures'
7
7
 
8
- return nil unless fixture_file_path
8
+ return nil unless File.exist?(fixtures_file_path)
9
9
 
10
- fixtures = YAML.load_file(fixture_file_path)
10
+ fixtures = YAML.load_file(fixtures_file_path, permitted_classes: [ActiveSupport::HashWithIndifferentAccess])
11
11
  fixtures.keys.find do |key|
12
12
  ActiveRecord::FixtureSet.identify(key) == id
13
13
  end
14
14
  end
15
15
 
16
- def fixture_file_path
16
+ def fixtures_file_path
17
+ existing_fixtures_file_path || candidate_fixtures_file_path
18
+ end
19
+
20
+ def candidate_fixtures_file_path
21
+ klass = self.class
22
+ loop do
23
+ path = Rails.root.join('test', 'fixtures', "#{klass.to_s.underscore.pluralize}.yml")
24
+ return path if klass >= ActiveRecord::Base || !klass.columns.map(&:name).include?(klass.inheritance_column)
25
+
26
+ klass = klass.superclass
27
+ end
28
+ end
29
+
30
+ def existing_fixtures_file_path
17
31
  klass = self.class
18
32
 
19
33
  while klass < ActiveRecord::Base
@@ -1,29 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "fixture_farm/fixture_recorder"
3
+ require "fixture_farm/hook"
4
4
 
5
5
  module FixtureFarm
6
6
  module ControllerHook
7
7
  extend ActiveSupport::Concern
8
+ include Hook
8
9
 
9
10
  included do
10
11
  around_action :record_new_fixtures, if: :record_new_fixtures?
11
12
  end
12
-
13
- private
14
-
15
- def record_new_fixtures(&block)
16
- fixture_recorder = FixtureRecorder.resume_recording_session
17
-
18
- fixture_recorder.record_new_fixtures do
19
- block.call
20
- end
21
- ensure
22
- fixture_recorder.update_recording_session
23
- end
24
-
25
- def record_new_fixtures?
26
- FixtureRecorder.recording_session_in_progress?
27
- end
28
13
  end
29
14
  end
@@ -13,7 +13,7 @@ module FixtureFarm
13
13
  def self.resume_recording_session
14
14
  start_recording_session! unless recording_session_in_progress?
15
15
 
16
- recording_session = JSON.load_file(STORE_PATH)
16
+ recording_session = JSON.load_file(STORE_PATH, permitted_classes: [ActiveSupport::HashWithIndifferentAccess])
17
17
 
18
18
  new_models = recording_session['new_models'].map do |(class_name, id)|
19
19
  class_name.constantize.find(id)
@@ -113,30 +113,49 @@ module FixtureFarm
113
113
  end
114
114
 
115
115
  if belongs_to_association
116
- associated_model_instance = model_instance.public_send(belongs_to_association.name)
116
+ associated_model_instance = find_assiciated_model_instance(model_instance, belongs_to_association)
117
117
 
118
118
  associated_fixture_name = named_new_fixtures.find do |_, fixture_model|
119
119
  fixture_model.id == associated_model_instance.id
120
120
  end&.first || associated_model_instance.fixture_name
121
121
 
122
122
  [belongs_to_association.name.to_s, associated_fixture_name]
123
- else
123
+ elsif model_instance.column_for_attribute(k).type
124
124
  [k, serialize_attributes(v)]
125
125
  end
126
- end.to_h
126
+ end.compact.to_h
127
127
 
128
- fixture_file_path = model_instance.fixture_file_path || Rails.root.join('test', 'fixtures', "#{model_instance.class.table_name}.yml")
128
+ yaml_attributes.delete('created_at') if yaml_attributes['created_at'] == '<%= Time.zone.now %>'
129
+ yaml_attributes.delete('updated_at') if yaml_attributes['updated_at'] == '<%= Time.zone.now %>'
129
130
 
130
- fixtures = File.exist?(fixture_file_path) ? YAML.load_file(fixture_file_path) : {}
131
+ fixtures_file_path = model_instance.fixtures_file_path
132
+
133
+ fixtures = File.exist?(fixtures_file_path) ? YAML.load_file(fixtures_file_path, permitted_classes: [ActiveSupport::HashWithIndifferentAccess]) : {}
131
134
  fixtures[new_fixture_name] = yaml_attributes
132
135
 
133
- File.open(fixture_file_path, 'w') do |file|
136
+ FileUtils.mkdir_p(fixtures_file_path.dirname)
137
+
138
+ File.open(fixtures_file_path, 'w') do |file|
134
139
  yaml = YAML.dump(fixtures).gsub(/\n(?=[^\s])/, "\n\n").delete_prefix("---\n\n")
135
140
  file.write(yaml)
136
141
  end
137
142
  end
138
143
  end
139
144
 
145
+ # Clear default_scope before finding associated model record.
146
+ # This, in particular, turns off ActsAsTenant, that otherwise
147
+ # might return no record if the tenant has changed by this point.
148
+ def find_assiciated_model_instance(model_instance, association)
149
+ associated_model_class = if association.polymorphic?
150
+ model_instance.public_send(association.foreign_type).safe_constantize
151
+ else
152
+ association.klass
153
+ end
154
+
155
+ id = model_instance.public_send(association.foreign_key)
156
+ associated_model_class.unscoped.find(id)
157
+ end
158
+
140
159
  def serialize_attributes(value)
141
160
  case value
142
161
  when ActiveSupport::TimeWithZone, Date
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fixture_farm/fixture_recorder"
4
+
5
+ module FixtureFarm
6
+ module Hook
7
+ def record_new_fixtures(&block)
8
+ fixture_recorder = FixtureRecorder.resume_recording_session
9
+ fixture_recorder.record_new_fixtures { block.call }
10
+ ensure
11
+ fixture_recorder.update_recording_session
12
+ end
13
+
14
+ private
15
+
16
+ def record_new_fixtures?
17
+ FixtureRecorder.recording_session_in_progress?
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module FixtureFarm
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.5'
3
3
  end
data/lib/fixture_farm.rb CHANGED
@@ -5,10 +5,11 @@ require 'fixture_farm/version'
5
5
  module FixtureFarm
6
6
  autoload :ActiveRecordExtension, 'fixture_farm/active_record_extension'
7
7
  autoload :ControllerHook, 'fixture_farm/controller_hook'
8
+ autoload :ActiveJobHook, 'fixture_farm/active_job_hook'
8
9
  autoload :TestHelper, 'fixture_farm/test_helper'
9
10
  autoload :FixtureRecorder, 'fixture_farm/fixture_recorder'
10
11
  end
11
12
 
12
- ActiveSupport.on_load(:active_record) do |base|
13
- base.include FixtureFarm::ActiveRecordExtension
13
+ ActiveSupport.on_load(:active_record) do
14
+ include FixtureFarm::ActiveRecordExtension
14
15
  end
metadata CHANGED
@@ -1,35 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixture_farm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - artemave
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-01 00:00:00.000000000 Z
11
+ date: 2022-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 6.1.4
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: 6.1.4.1
19
+ version: '6.2'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: 6.1.4
30
24
  - - ">="
31
25
  - !ruby/object:Gem::Version
32
- version: 6.1.4.1
26
+ version: '6.2'
33
27
  description:
34
28
  email:
35
29
  - mr@artem.rocks
@@ -45,9 +39,11 @@ files:
45
39
  - bin/fixture_farm
46
40
  - bin/fixture_farm.rb
47
41
  - lib/fixture_farm.rb
42
+ - lib/fixture_farm/active_job_hook.rb
48
43
  - lib/fixture_farm/active_record_extension.rb
49
44
  - lib/fixture_farm/controller_hook.rb
50
45
  - lib/fixture_farm/fixture_recorder.rb
46
+ - lib/fixture_farm/hook.rb
51
47
  - lib/fixture_farm/test_helper.rb
52
48
  - lib/fixture_farm/version.rb
53
49
  homepage: https://github.com/featurist/fixture_farm