record_loader 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.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/cops_and_specs.yml +39 -0
  3. data/.gitignore +11 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +51 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +7 -0
  8. data/.yardopts +4 -0
  9. data/CHANGELOG.md +16 -0
  10. data/Gemfile +8 -0
  11. data/Gemfile.lock +70 -0
  12. data/LICENSE +24 -0
  13. data/README.md +182 -0
  14. data/Rakefile +36 -0
  15. data/bin/console +11 -0
  16. data/bin/setup +8 -0
  17. data/lib/generators/record_loader/USAGE +36 -0
  18. data/lib/generators/record_loader/record_loader_generator.rb +55 -0
  19. data/lib/generators/record_loader/static_files/application_record_loader.rb +13 -0
  20. data/lib/generators/record_loader/static_files/record_loader.rake +11 -0
  21. data/lib/generators/record_loader/templates/config/record_loader/%underscores%/default_records.yml +11 -0
  22. data/lib/generators/record_loader/templates/lib/record_loader/%underscore_loader%.rb.tt +16 -0
  23. data/lib/generators/record_loader/templates/lib/tasks/record_loader/%underscore%.rake.tt +13 -0
  24. data/lib/generators/record_loader/templates/spec/data/record_loader/%underscores%/two_entry_example.yml +5 -0
  25. data/lib/generators/record_loader/templates/spec/lib/record_loader/%underscore_loader%_spec.rb.tt +31 -0
  26. data/lib/record_loader/adapter/basic.rb +48 -0
  27. data/lib/record_loader/adapter/rails.rb +33 -0
  28. data/lib/record_loader/adapter.rb +58 -0
  29. data/lib/record_loader/base.rb +160 -0
  30. data/lib/record_loader/railtie.rb +9 -0
  31. data/lib/record_loader/record_file.rb +51 -0
  32. data/lib/record_loader/version.rb +6 -0
  33. data/lib/record_loader.rb +15 -0
  34. data/record_loader.gemspec +55 -0
  35. metadata +192 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 159dabfb7769aebda7cb1c5fd9f77b67e728917876c769d663191caefc386a48
4
+ data.tar.gz: 64e7699028b6ca62daa1dd1bab3b9513f19a61affca1ce3df0f0b074cfc60315
5
+ SHA512:
6
+ metadata.gz: 3dff12471dc11ca8b76a029e229240bb76f2b86e079c9149af0119525e4ce784c8875f33e17b92f3d2ca900eb7d7d6759bb589959f568af4a56ea9a6ede85454
7
+ data.tar.gz: a7506298753fe9e3d99a06b574312d00c975d93763c9a9715dc17ee5657cea60ab62fcacc5116dd36b57e2a538ecc26f841e2a0dc0891f9fa135052378a2595e
@@ -0,0 +1,39 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Cops and Specs
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
+ # uses: ruby/setup-ruby@v1
27
+ uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
28
+ with:
29
+ ruby-version: 2.5
30
+ - name: Install dependencies
31
+ run: bundle install
32
+ - name: Run Rubocop
33
+ run: bundle exec rubocop
34
+ - name: Test & publish code coverage
35
+ uses: paambaati/codeclimate-action@v2.6.0
36
+ env:
37
+ CC_TEST_REPORTER_ID: 5c1f50b6c3beb11b20e0e1d40800ebd81a5e1724b626060e9b366f2942acc315
38
+ with:
39
+ coverageCommand: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,51 @@
1
+ # The behaviour of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behaviour if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
11
+
12
+ require: rubocop-rspec
13
+
14
+ Layout/LineLength:
15
+ Max: 120
16
+
17
+ # Blocks are used to structure tests and are part of the rspec dsl.
18
+ # The standard BlockLength limits are too stringent for this purpose.
19
+ Metrics/BlockLength:
20
+ ExcludedMethods:
21
+ - describe
22
+ - context
23
+
24
+ # Increase the acceptable nesting levels. While we don't want
25
+ # 4 nested contexts, it appears that the cop is also counting
26
+ # describe. So by the time we've described out class, and the method
27
+ # we only have room for one context. Maybe this is the cops intent
28
+ # but feels a little strict to me.
29
+ RSpec/NestedGroups:
30
+ Max: 4
31
+
32
+ # Pending cops in 0.83 can be removed once enabled in bulk
33
+ # see https://docs.rubocop.org/en/latest/versioning/:
34
+ Layout/SpaceAroundMethodCallOperator:
35
+ Enabled: true
36
+ Lint/RaiseException:
37
+ Enabled: true
38
+ Lint/StructNewOverride:
39
+ Enabled: true
40
+ Style/ExponentialNotation:
41
+ Enabled: true
42
+ Style/HashEachMethods:
43
+ Enabled: true
44
+ Style/HashTransformKeys:
45
+ Enabled: true
46
+ Style/HashTransformValues:
47
+ Enabled: true
48
+ Layout/EmptyLinesAroundAttributeAccessor:
49
+ Enabled: true
50
+ Style/SlicingWithRange:
51
+ Enabled: true
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.5.3
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.1
7
+ before_install: gem install bundler -v 1.17.3
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ -
2
+ LICENSE
3
+ CHANGELOG.md
4
+ lib/generators/record_loader/USAGE
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ Keeps track of notable changes. Please remember to add new behaviours to the
4
+ Unreleased section to make new releases easy.
5
+
6
+ ## [Unreleased]
7
+
8
+ ## [0.1.0]
9
+
10
+ Initial release
11
+
12
+ - [Feature] Produce testable, reproducible data migrations across multiple environments
13
+ - [Feature] Organize data into multiple files to provide context
14
+ - [Feature] Add development environment specific data with .dev.yml files
15
+ - [Feature] Keep work-in-progress isolated with .wip.yml files
16
+ - [Feature] Rails generators to quickly create new record loaders
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in record_loader.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ record_loader (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ docile (1.3.2)
13
+ json (2.3.0)
14
+ method_source (1.0.0)
15
+ parallel (1.19.1)
16
+ parser (2.7.1.2)
17
+ ast (~> 2.4.0)
18
+ pry (0.13.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.0.0)
22
+ rake (12.3.3)
23
+ rexml (3.2.4)
24
+ rspec (3.9.0)
25
+ rspec-core (~> 3.9.0)
26
+ rspec-expectations (~> 3.9.0)
27
+ rspec-mocks (~> 3.9.0)
28
+ rspec-core (3.9.2)
29
+ rspec-support (~> 3.9.3)
30
+ rspec-expectations (3.9.2)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-mocks (3.9.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-support (3.9.3)
37
+ rubocop (0.83.0)
38
+ parallel (~> 1.10)
39
+ parser (>= 2.7.0.1)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ rexml
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 1.4.0, < 2.0)
44
+ rubocop-rspec (1.39.0)
45
+ rubocop (>= 0.68.1)
46
+ ruby-progressbar (1.10.1)
47
+ simplecov (0.17.1)
48
+ docile (~> 1.1)
49
+ json (>= 1.8, < 3)
50
+ simplecov-html (~> 0.10.0)
51
+ simplecov-html (0.10.2)
52
+ unicode-display_width (1.7.0)
53
+ yard (0.9.25)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ bundler (~> 2.1)
60
+ pry (~> 0.13)
61
+ rake (~> 12.3)
62
+ record_loader!
63
+ rspec (~> 3.0)
64
+ rubocop (~> 0.82)
65
+ rubocop-rspec (~> 1.39)
66
+ simplecov (~> 0.17.0)
67
+ yard (~> 0.9)
68
+
69
+ BUNDLED WITH
70
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ The MIT License (MIT)
2
+
3
+ Authors:
4
+ James Glover <james.glover@sanger.ac.uk>
5
+
6
+ Copyright (c) 2020 Genome Research Ltd
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in
16
+ all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,182 @@
1
+
2
+ # RecordLoader
3
+
4
+ ![Cops and Specs](https://github.com/sanger/record_loader/workflows/Cops%20and%20Specs/badge.svg)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/0ce827d110dfced197ab/maintainability)](https://codeclimate.com/github/sanger/record_loader/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/0ce827d110dfced197ab/test_coverage)](https://codeclimate.com/github/sanger/record_loader/test_coverage)
7
+ [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://rubydoc.info/github/sanger/record_loader)
8
+
9
+ RecordLoader provides a simple and standardized way of populating databases from information described in a series of
10
+ organized yaml files. It is intended to be used to generate a number of idempotent tasks, which can be run in both
11
+ your production and development environments.
12
+
13
+ While written with ActiveRecord/Rails in mind, it is possible to use RecordLoader in different environments.
14
+
15
+ ## Key features
16
+
17
+ - Produce testable, reproducible data migrations across multiple environments
18
+ - Organize data into multiple files to provide context
19
+ - Add development environment specific data with .dev.yml files
20
+ - Keep work-in-progress isolated with .wip.yml files
21
+ - Rails generators to quickly create new record loaders
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'record_loader'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ ```bash
34
+ bundle
35
+ ```
36
+
37
+ Or install it yourself as:
38
+
39
+ ```bash
40
+ gem install record_loader
41
+ ```
42
+
43
+ If you are using Rails, you do not need to make any further changes, and all necessary hooks will be installed when
44
+ generating your first record loader.
45
+
46
+ ## Usage (Rails)
47
+
48
+ RecordLoader provides a generator to automatically build a loader, specs and the yaml files necessary to use it.
49
+ In addition, the first time you use it it will automatically install the necessary rake files and configuration.
50
+ You can access this by running:
51
+
52
+ ```bash
53
+ bundle exec rails g record_loader
54
+ ```
55
+
56
+ Which will return the documentation:
57
+
58
+ {include:file:lib/generators/record_loader/USAGE}
59
+
60
+ ### An example loader
61
+
62
+ Suppose you want to create a loader to maintain a selection of product types. You'll first use the generator:
63
+
64
+ ```bash
65
+ $ bundle exec rails g record_loader
66
+ exist
67
+ create config/record_loader/product_types/default_records.yml
68
+ create lib/record_loader/product_type_loader.rb
69
+ create lib/record_loader/tasks/record_loader/product_type.rake
70
+ create spec/data/record_loader/product_types/two_entry_example.yml
71
+ create spec/lib/record_loader/product_type_loader_spec.rb
72
+ skip lib/record_loader/application_record_loader.rb
73
+ identical lib/tasks/record_loader.rake
74
+ ```
75
+
76
+ This will create several files:
77
+
78
+ #### `lib/tasks/record_loader.rake`
79
+
80
+ Adds the record_loader:all rake task which can be used to trigger all record loaders.
81
+
82
+ #### `lib/record_loader/application_record_loader.rb`
83
+
84
+ Application specific base class for customization.
85
+
86
+ #### `config/record_loader/product_types/default_records.yml`
87
+
88
+ Example yaml file to begin populating with your record information. Record Loaders will load all yaml files from within
89
+ this directory, so it is possible to separate your records into multiple different files for better organization.
90
+ In addition yaml files ending in `.dev.yml` and `.wip.yml` exhibit special behaviour.
91
+ See [dev and wip files](#dev-and-wip).
92
+
93
+ #### `lib/record_loader/product_type_loader.rb`
94
+
95
+ The actual loader. It will look something like this:
96
+
97
+ ```ruby
98
+ # frozen_string_literal: true
99
+ # This file was automatically generated via `rails g record_loader`
100
+
101
+ # RecordLoader handles automatic population and updating of database records
102
+ # across different environments
103
+ # @see https://rubydoc.info/github/sanger/record_loader/
104
+ module RecordLoader
105
+ # Creates the specified plate types if they are not present
106
+ class ProductTypeLoader < ApplicationRecordLoader
107
+ config_folder 'product_types'
108
+
109
+ def create_or_update!(name, options)
110
+ ProductType.create_with(options).find_or_create_by!(name: name)
111
+ end
112
+ end
113
+ end
114
+ ```
115
+
116
+ The `config_folder` specifies which directory under `config/record_loader` will be used to source the yaml files.
117
+ The method `create_or_update!` will create the actual records, and should be idempotent (ie. calling it multiple times will
118
+ have the same effect as calling it once). `create_or_update!` will be called once for each entry in the yaml files,
119
+ with the first argument being the key, and the second argument being the value, usually a hash of options.
120
+
121
+ #### `lib/record_loader/tasks/record_loader/product_type.rake`
122
+
123
+ This contains the `record_loader:product_type` which will trigger the record loader, and also ensures that
124
+ `record_loader:product_type` will get invoked on calling `record_loader:all`.
125
+
126
+ #### `spec/data/record_loader/product_types/two_entry_example.yml`
127
+
128
+ A basic configuration for testing the loader. Tests use a separate directory to avoid coupling your specs to the data.
129
+
130
+ #### `spec/lib/record_loader/product_type_loader_spec.rb`
131
+
132
+ A basic rspec spec file for testing your loader. By default this just confirms that your loader creates the
133
+ expected number of records, and that it is idempotent.
134
+
135
+ ## Dev and Wip files
136
+
137
+ Each loader can have one or more yaml files contained within its config directory. Most files will be aggregated
138
+ together and only serve to provide a means of organization. However it is possible to add extra behaviour:
139
+
140
+ `.dev.yml` files will only be loaded in development environments. This is useful for seeding data for quick testing, but
141
+ which will not be needed in production environments. This may include test user accounts, dummy projects or quick
142
+ start data.
143
+
144
+ `.wip.yml` files will only be loaded if explicitly enabled via a WIP environmental variable. For example the file
145
+ `my_feature.wip.yml` will run if the WIP env is set to `my_feature`. Multiple WIP flags can be set at the same time by
146
+ providing a comma separated list. eg. `WIP=my_feature,other_feature`
147
+
148
+ If you have an existing feature flag system you can use this instead by adding a `wip_list` method to
149
+ `RecordLoader::ApplicationRecordLoader` which returns an array of enabled feature names. For example:
150
+
151
+ ```ruby
152
+ def wip_list
153
+ FeatureFlags.active.pluck(:name)
154
+ end
155
+ ```
156
+
157
+ ## Non Rails Environments
158
+
159
+ In non-rails environments you can use the {RecordLoader::Adapter::Basic} adapter to avoid Rails specific functionality.
160
+ This is the default adapter for {RecordLoader::Base}, although it is still recommended that you create an application
161
+ specific class that inherits from this to allow for customization. Your custom record loaders can then inherit from this
162
+ class instead.
163
+
164
+ See {RecordLoader::Adapter} for information about custom adapters.
165
+
166
+ ## Development
167
+
168
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
169
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
170
+
171
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
172
+ version number in `version.rb`, ensure the CHANGELOG.md is updated and that everything is committed.
173
+ Then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push
174
+ the `.gem` file to [rubygems.org](https://rubygems.org).
175
+
176
+ ## Contributing
177
+
178
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/sanger/record_loader>.
179
+
180
+ ## License
181
+
182
+ 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,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[rubocop spec]
11
+
12
+ task release: :preflight
13
+
14
+ desc 'Runs the preflight checklist before building a release'
15
+ task :preflight do
16
+ puts '🛫 Preflight checklist'
17
+
18
+ current_version = RecordLoader::VERSION
19
+
20
+ puts 'This is still a manual process, but before we roll a release, lets confirm a few things:'
21
+ puts
22
+ puts "☐ lib/record_loader/version.rb updated? Currently contains #{current_version}"
23
+ puts '☐ CHANGELOG.md updated'
24
+ puts '☐ Commited, on master and up to date'
25
+ puts
26
+ print 'Proceed Y/N > '
27
+
28
+ proceed = $stdin.gets.chomp
29
+
30
+ if proceed.casecmp? 'Y'
31
+ puts 'Proceeding ...'
32
+ else
33
+ puts 'Canceling release process'
34
+ exit 1
35
+ end
36
+ end
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'record_loader'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require 'pry'
11
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ Description:
2
+ Adds a new RecordLoader and all necessary files.
3
+
4
+ --record-class
5
+ Typically a loader is named after the class it creates, but if you want to
6
+ override this, you can specify a different ActiveRecord class name with the
7
+ record-class option. This functionality is purely for convenience, and will
8
+ change the class names used in the record loader, and associated specs. If
9
+ your loader is more complicated, and will create records of multiple classes
10
+ then you can ignore this setting and make the relevant modifications to
11
+ lib/record_loader/xxx_loader.rb and
12
+ spec/lib/record_loader/xxx_loader_spec.rb manually.
13
+
14
+ --record-key
15
+ By default the generator creates a simple loader that checks for a record
16
+ based on the value of a particular attribute, and it it doesn't exists,
17
+ creates it using the attributes specified in the yaml file. You can
18
+ specify which attribute gets used by specifying record-key, by default it
19
+ uses name.
20
+
21
+ Example:
22
+ rails generate record_loader MyThing
23
+
24
+ This will generate a RecordLoader named MyThingLoader, a rake task to
25
+ trigger it, specs and example yaml files.
26
+
27
+ On first usage this will create:
28
+ lib/record_loader/application_record_loader.rb
29
+ lib/tasks/record_loader.rake
30
+
31
+ It will also create:
32
+ config/record_loader/my_things/default_records.yml
33
+ lib/record_loader/my_thing_loader.rb
34
+ lib/record_loader/tasks/record_loader/my_thing.rake
35
+ spec/data/record_loader/my_things/two_entry_example.yml
36
+ spec/lib/record_loader/my_thing_loader_spec.rb
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Rails generator to automatically build record loaders
4
+ # @see lib/generators/record_loader/USAGE
5
+ class RecordLoaderGenerator < Rails::Generators::NamedBase
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ class_option :record_class, type: :string, default: nil,
9
+ desc: 'The name of the ActiveRecord class which will be created. eg. User'
10
+ class_option :record_key, type: :string, default: 'name',
11
+ desc: 'The unique attribute by which record will be identified'
12
+
13
+ # Builds all templates in the templates directory
14
+ def create_directories
15
+ directory '.', './'
16
+ end
17
+
18
+ # Generally this will only run on generating the first loader
19
+ def initial_setup
20
+ # Copy across the application_record_loader.rb unless it already exists
21
+ copy_file '../static_files/application_record_loader.rb',
22
+ 'lib/record_loader/application_record_loader.rb',
23
+ skip: true
24
+ # Copy across the record_loader.rake unless it already exists
25
+ copy_file '../static_files/record_loader.rake',
26
+ 'lib/tasks/record_loader.rake',
27
+ skip: true
28
+ end
29
+
30
+ private
31
+
32
+ def loader_class_name
33
+ "#{name.camelcase}Loader"
34
+ end
35
+
36
+ def record_class
37
+ options['record_class'] || name.camelcase
38
+ end
39
+
40
+ def record_key
41
+ options['record_key']
42
+ end
43
+
44
+ def underscore_loader
45
+ "#{name.underscore}_loader"
46
+ end
47
+
48
+ def underscore
49
+ name.underscore
50
+ end
51
+
52
+ def underscores
53
+ underscore.pluralize
54
+ end
55
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was automatically generated by `rails g record_loader`
4
+ module RecordLoader
5
+ # This forms the standard base class for record loaders in your
6
+ # application, allowing for easy configuration.
7
+ # @see https://rubydoc.info/github/sanger/record_loader/
8
+ class ApplicationRecordLoader < RecordLoader::Base
9
+ # Uses the standard RailsAdapter
10
+ # @see https://rubydoc.info/github/sanger/record_loader/RecordLoader/Adapter
11
+ adapter RecordLoader::Adapter::Rails.new
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was automatically generated by `rails g record_loader`
4
+ # It is not necessary to modify this file, instead individual tasks
5
+ # will register themselves as a dependency.
6
+ namespace :record_loader do
7
+ desc 'Automatically runs all record loader tasks'
8
+ task :all do
9
+ puts 'Loading all records'
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ ---
2
+ # Add entries to this file to generate them automatically upon running
3
+ # rake record_loader:all or rake record_loader:<%= underscore %>
4
+ # If you want entries which are only created in development mode then simply
5
+ # create a file in this directory ending in '.dev.yml'
6
+ # You can add as many additional .yml files to this directory as you wish.
7
+ # Example:
8
+ # <%= record_key %> A:
9
+ # other_attribute: 'Value'
10
+ # <%= record_key %> B:
11
+ # other_attribute: 'Value'
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ # This file was automatically generated via `rails g record_loader`
3
+
4
+ # RecordLoader handles automatic population and updating of database records
5
+ # across different environments
6
+ # @see https://rubydoc.info/github/sanger/record_loader/
7
+ module RecordLoader
8
+ # Creates the specified plate types if they are not present
9
+ class <%= loader_class_name %> < ApplicationRecordLoader
10
+ config_folder '<%= underscores %>'
11
+
12
+ def create_or_update!(<%= record_key %>, options)
13
+ <%= record_class %>.create_with(options).find_or_create_by!(<%= record_key %>: <%= record_key %>)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was automatically generated via `rails g record_loader`
4
+ namespace :record_loader do
5
+ desc 'Automatically generate <%= record_class %> through <%= loader_class_name %>'
6
+ task <%= underscore %>: :environment do
7
+ RecordLoader::<%= loader_class_name %>.new.create!
8
+ end
9
+ end
10
+
11
+ # Automatically run this record loader as part of record_loader:all
12
+ # Remove this line if the task should only run when invoked explicitly
13
+ task 'record_loader:all' => 'record_loader:<%= underscore %>'
@@ -0,0 +1,5 @@
1
+ ---
2
+ # This file was automatically generated via `rails g record_loader`
3
+ # You should modify it to match your particular model.
4
+ Unique attribute: {}
5
+ Unique attribute 2: {}