fixture_farm 0.1.1 → 0.1.4

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: b3e5adb6c4342555b969a75e117c91b3f004024b221c4213875db0548761c21e
4
- data.tar.gz: 61d34c3eb3b0d7021a88942e7337ad9f5c216c2f1310ea125e9ab70a1baf26f8
3
+ metadata.gz: 3a4711e5c5ca0eaaebf6d44bfa7751f09e4ff1fc6b5969e4ed4af04d43469069
4
+ data.tar.gz: 6ed3b3e4961f21fb4435d3b571b9ab04e78cf3c7cdfe9cce8dc7c9021e663ae0
5
5
  SHA512:
6
- metadata.gz: 799185e8d4ffc59caa5a8eccc23c0fdeb1d075c7b190ebd78b17f49da1d64ed543d1eeecf738c6107cf76fd8143f3df5ce3ecd2b4b8c327d92eb805cd9d746fc
7
- data.tar.gz: 0a18ec774e121edb0dc8eafa0d67d57cd81cf5c01ace2765693121dc72b64a336554973713683a92b2bceb3d8ca96372ad63b3e4b642415e9e1ef26523f7b93e
6
+ metadata.gz: 2ddd75078a5228b34a5d3034d339ddff5fefccc1f003595a96bbae9f97e7ae334c2833d29cb979e9cd890fb77c1b89d95f612fc0a55996bd2f9b22e218d528e8
7
+ data.tar.gz: 7d53ed3b3617c6456fb515b17f6ce87361349c79ee370ee9cc42f2ae1d9f4a4fd383c45f5f4c6fee3434c485e3122d579a540e4c346ef62c1d2914564ba65093
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).
data/bin/fixture_farm.rb CHANGED
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env ruby
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require_relative '../lib/fixture_farm/fixture_recorder'
@@ -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,7 +113,7 @@ 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
@@ -125,18 +125,37 @@ module FixtureFarm
125
125
  end
126
126
  end.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.1'
2
+ VERSION = '0.1.4'
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.1
4
+ version: 0.1.4
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