Sha256: b6a070096a49d44522bd6cb21392ae8d5eba1f80623a868895629b4037cd31d0

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require "rails/generators"
require "rails/generators/migration"

module Settings
  class InstallGenerator < Rails::Generators::NamedBase
    desc "Generate RailsSettings files."
    include Rails::Generators::Migration

    argument :name, type: :string, default: "setting"

    source_root File.expand_path("../templates", __FILE__)

    @@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 "%.3d", current_migration_number(dirname) + 1
      end
    end

    def install_setting
      template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
      template "app.yml", File.join("config", "app.yml")
      migration_template "migration.rb", "db/migrate/create_settings.rb", migration_version: migration_version
    end

    def rails5?
      Rails.version.start_with? "5"
    end

    def migration_version
      "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if rails5?
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
jdx-rails-settings-0.7.4 lib/generators/settings/install_generator.rb
rails-settings-cached-0.7.2 lib/generators/settings/install_generator.rb
rails-settings-cached-0.7.1 lib/generators/settings/install_generator.rb