docbrown/lib/generators/settings_model/settings_model_generator.rb
dependabot[bot] f0093c5338
Bump rubocop from 1.18.4 to 1.19.0 (#14489)
* Bump rubocop from 1.18.4 to 1.19.0

Bumps [rubocop](https://github.com/rubocop/rubocop) from 1.18.4 to 1.19.0.
- [Release notes](https://github.com/rubocop/rubocop/releases)
- [Changelog](https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rubocop/rubocop/compare/v1.18.4...v1.19.0)

---
updated-dependencies:
- dependency-name: rubocop
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Rubocop autofixes

* Manually fix some Rubocop warnings/errors

* How about another coffee, Michael?

* Maybe Rubocop needs a coffee too

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Michael Kohl <citizen428@forem.com>
2021-08-13 13:14:59 +07:00

59 lines
1.4 KiB
Ruby

# Based on https://github.com/huacnlee/rails-settings-cached/blob/main/lib/generators/settings/install_generator.rb
require "rails/generators"
require "rails/generators/migration"
class SettingsModelGenerator < Rails::Generators::NamedBase
include Rails::Generators::Migration
argument :name, type: :string, default: "setting"
source_root File.expand_path("templates", __dir__)
@migrations = false
def self.next_migration_number(dirname) # :nodoc:
if ActiveRecord::Base.timestamped_migrations
if @migrations
(current_migration_number(dirname) + 1)
else
@migrations = true
Time.now.utc.strftime("%Y%m%d%H%M%S")
end
else
format "%.3<number>d", number: current_migration_number(dirname) + 1
end
end
def create_migration_file
migration_template(
"migration.erb",
"db/migrate/create_#{table_name}.rb",
migration_version: migration_version,
table_name: table_name,
)
end
def create_model
template(
"model.erb",
File.join("app/models/settings", class_path, "#{file_name}.rb"),
)
end
def rails_version_major
Rails::VERSION::MAJOR
end
def rails_version_minor
Rails::VERSION::MINOR
end
def migration_version
"[#{rails_version_major}.#{rails_version_minor}]" if rails_version_major >= 5
end
def table_name
@table_name ||= "settings_#{class_name.underscore.pluralize}"
end
end