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:
parent
53d8aaa175
commit
847d8a8590
5 changed files with 131 additions and 0 deletions
|
|
@ -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"))
|
||||
|
|
|
|||
42
lib/generators/settings_model/USAGE
Normal file
42
lib/generators/settings_model/USAGE
Normal 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
|
||||
|
||||
59
lib/generators/settings_model/settings_model_generator.rb
Normal file
59
lib/generators/settings_model/settings_model_generator.rb
Normal 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
|
||||
16
lib/generators/settings_model/templates/migration.erb
Normal file
16
lib/generators/settings_model/templates/migration.erb
Normal 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
|
||||
13
lib/generators/settings_model/templates/model.erb
Normal file
13
lib/generators/settings_model/templates/model.erb
Normal 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
|
||||
Loading…
Add table
Reference in a new issue