Add settings model generator (#13003)

* Add settings model generator

* Obey the CodeClimate overlords

* Update generator

* Update USAGE

* Add Zeitwerk exception
This commit is contained in:
Michael Kohl 2021-03-19 11:55:31 +00:00 committed by GitHub
parent 53d8aaa175
commit 847d8a8590
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 131 additions and 0 deletions

View file

@ -14,3 +14,4 @@ end
Rails.autoloaders.main.ignore(Rails.root.join("lib/data_update_scripts"))
Rails.autoloaders.main.ignore(Rails.root.join("lib/generators/data_update"))
Rails.autoloaders.main.ignore(Rails.root.join("lib/generators/service"))
Rails.autoloaders.main.ignore(Rails.root.join("lib/generators/settings_model"))

View file

@ -0,0 +1,42 @@
Description:
Generates a settings model.
Examples:
* Generating a settings model (you don't need to specify the Settings::
namespace)
$ rails generate settings_model Notification
# db/migrate/<timestamp>_create_settings_notifications.rb
class CreateSettingsNotifications < ActiveRecord::Migration[6.0]
def self.up
create_table :settings_notifications do |t|
t.string :var, null: false
t.text :value, null: true
t.timestamps
end
add_index :settings_notifications, :var, unique: true
end
def self.down
drop_table :settings_notifications
end
end
# app/models/settings/notification.rb
module Settings
class Notification < RailsSettings::Base
self.table_name = :settings_notifications
# The configuration is cached, change this if you want to force update
# the cache, or call Setting::Notification.clear_cache
cache_prefix { "v1" }
# Define your fields
# field :host, type: :string, default: "http://localhost:3000"
# field :default_locale, default: "en", type: :string
end
end

View file

@ -0,0 +1,59 @@
# 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

View file

@ -0,0 +1,16 @@
class Create<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
def self.up
create_table :<%= table_name %> do |t|
t.string :var, null: false
t.text :value, null: true
t.timestamps
end
add_index :<%= table_name %>, :var, unique: true
end
def self.down
drop_table :<%= table_name %>
end
end

View file

@ -0,0 +1,13 @@
module Settings
class <%= class_name %> < RailsSettings::Base
self.table_name = :<%= table_name %>
# The configuration is cached, change this if you want to force update
# the cache, or call Settings::<%= class_name %>.clear_cache
cache_prefix { "v1" }
# Define your fields
# field :host, type: :string, default: "http://localhost:3000"
# field :default_locale, default: "en", type: :string
end
end