dump_to_fixture 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: af60976a76605cc532ab18cf38ad4a18bb930d97eefa76acee51d2c3548f5c19
4
+ data.tar.gz: 541e5002b0a388e416526ead6da1a45c0ac9f8835a83acba43848b8bd6013e9c
5
+ SHA512:
6
+ metadata.gz: c655f7b58246b3012a84b998b54b1525187871564b0d2eeb7e3b98bd999c469ce9d93aa6f639f20566b31545f146de8db60ce332da4214e5fd4565f78e78c754
7
+ data.tar.gz: 4091b33ac3465f07b85c06fe231748236170df109b7d9eb40004b3507cdab2b68de34c3338b5fcc17d870b4827513478c731e68894799203d4cfe347518807b1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Justin Stone
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # DumpToFixture
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dump_to_fixture`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dump_to_fixture'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dump_to_fixture
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jcstone/dump_to_fixture.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+ task default: :test
@@ -0,0 +1,9 @@
1
+ class ActiveRecord::Base
2
+ def dump_to_fixture
3
+ fixture_file = "#{Rails.root}/test/fixtures/#{self.class.table_name}.yml"
4
+ File.open(fixture_file, "a+") do |f|
5
+ f.puts({ "#{self.class.table_name.singularize}_#{id}" => attributes }.
6
+ to_yaml.sub!(/---\s?/, "\n"))
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module DumpToFixture
2
+ class Railtie < ::Rails::Railtie
3
+ railtie_name :dump_to_fixture
4
+
5
+ rake_tasks do
6
+ path = File.expand_path(__dir__)
7
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ namespace :db do
2
+ desc 'Convert development DB to Rails test fixtures'
3
+ task dump_to_fixture: :environment do
4
+ TABLES_TO_SKIP = %w[ar_internal_metadata delayed_jobs schema_info schema_migrations].freeze
5
+
6
+ begin
7
+ ActiveRecord::Base.establish_connection
8
+ ActiveRecord::Base.connection.tables.each do |table_name|
9
+ next if TABLES_TO_SKIP.include?(table_name)
10
+
11
+ counter = '000'
12
+ file_path = "#{Rails.root}/test/fixtures/#{table_name}.yml"
13
+ File.open(file_path, 'w') do |file|
14
+ rows = ActiveRecord::Base.connection.select_all("SELECT * FROM #{table_name}")
15
+ data = rows.each_with_object({}) do |record, hash|
16
+ suffix = record['id'].blank? ? counter.succ! : record['id']
17
+ hash["#{table_name.singularize}_#{suffix}"] = record
18
+ end
19
+ puts "Writing table '#{table_name}' to '#{file_path}'"
20
+ file.write(data.to_yaml)
21
+ end
22
+ end
23
+ ensure
24
+ ActiveRecord::Base.connection.close if ActiveRecord::Base.connection
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module DumpToFixture
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,14 @@
1
+ require "dump_to_fixture/version"
2
+ require "dump_to_fixture/railtie"
3
+
4
+ module DumpToFixture
5
+ class ActiveRecord::Base
6
+ def dump_to_fixture
7
+ fixture_file = "#{Rails.root}/test/fixtures/#{self.class.table_name}.yml"
8
+ File.open(fixture_file, "a+") do |f|
9
+ f.puts({ "#{self.class.table_name.singularize}_#{id}" => attributes }.
10
+ to_yaml.sub!(/---\s?/, "\n"))
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :dump_to_fixture do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dump_to_fixture
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Stone
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ description: Create test fixtures from objects or existing development database
28
+ email:
29
+ - jclstone@outlook.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/dump_to_fixture.rb
38
+ - lib/dump_to_fixture/core_ext.rb
39
+ - lib/dump_to_fixture/railtie.rb
40
+ - lib/dump_to_fixture/tasks/db/dump_to_fixture.rake
41
+ - lib/dump_to_fixture/version.rb
42
+ - lib/tasks/dump_to_fixture_tasks.rake
43
+ homepage: https://github.com/jcstone/dump_to_fixture
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/jcstone/dump_to_fixture
48
+ source_code_uri: https://github.com/jcstone/dump_to_fixture
49
+ changelog_uri: https://github.com/jcstone/dump_to_fixture
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.7.6
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Create test fixtures from objects or existing development database
70
+ test_files: []