Sha256: ab0d1f221119a17ed4f4ae350612576128d9e1b36ed143fcbcf790c4e07dd654

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module Enja
  module Generators
    module Helper
      extend ActiveSupport::Concern

      private

      def model_class_name
        options.key?(:model) ? options[:model].classify : "User"
      end

      def model_file_path
        model_name.underscore
      end

      def model_path
        @model_path ||= File.join("app", "models", "#{model_file_path}.rb")
      end

      def namespace
        Rails::Generators.namespace if Rails::Generators.respond_to?(:namespace)
      end

      def namespaced?
        !!namespace
      end

      def model_name
        if namespaced?
          [namespace.to_s] + [model_class_name]
        else
          [model_class_name]
        end.join("::")
      end

      def attribute_name
        options.key?(:attribute) ? options[:attribute].underscore : "role"
      end

      def migration_class_name
        if Rails::VERSION::MAJOR >= 5
          "ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
        else
          "ActiveRecord::Migration"
        end
      end

      class_methods do
        # Define the next_migration_number method (necessary for the migration_template method to work)
        def next_migration_number(dirname)
          if ActiveRecord::Base.timestamped_migrations
            sleep 1 # make sure each time we get a different timestamp
            Time.new.utc.strftime("%Y%m%d%H%M%S")
          else
            format("%.3d", (current_migration_number(dirname) + 1))
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
enja-0.1.2 lib/generators/enja/helper.rb
enja-0.1.1 lib/generators/enja/helper.rb
enja-0.1.0 lib/generators/enja/helper.rb