Sha256: fe6d24056257755db54322f61aef48577f7f8f7f29c571c843615d6207f4ff46

Contents?: true

Size: 1.9 KB

Versions: 16

Compression:

Stored size: 1.9 KB

Contents

require 'rails/generators/base'

module ActivityNotification
  module Generators
    # Controller generator to create customizable controller files from templates.
    # @example Run controller generator for users as target
    #   rails generate activity_notification:controllers users
    class ControllersGenerator < Rails::Generators::Base
      CONTROLLERS = ['notifications', 'notifications_with_devise', 'subscriptions', 'subscriptions_with_devise'].freeze

      desc <<-DESC.strip_heredoc
        Create inherited ActivityNotification controllers in your app/controllers folder.

        Use -c to specify which controller you want to overwrite.
        If you do no specify a controller, all controllers will be created.
        For example:

          rails generate activity_notification:controllers users -c notifications

        This will create a controller class at app/controllers/users/notifications_controller.rb like this:

          class Users::NotificationsController < ActivityNotification::NotificationsController
            content...
          end
      DESC

      source_root File.expand_path("../../templates/controllers", __FILE__)
      argument :target, required: true,
        desc: "The target to create controllers in, e.g. users, admins"
      class_option :controllers, aliases: "-c", type: :array,
        desc: "Select specific controllers to generate (#{CONTROLLERS.join(', ')})"

      # Creates controller files in application directory
      def create_controllers
        @target_prefix = target.blank? ? '' : (target.camelize + '::')
        controllers = options[:controllers] || CONTROLLERS
        controllers.each do |name|
          template "#{name}_controller.rb",
                   "app/controllers/#{target}/#{name}_controller.rb"
        end
      end

      # Shows readme to console
      def show_readme
        readme "README" if behavior == :invoke
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
activity_notification-2.0.0 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.7.1 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.7.0 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.6.1 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.6.0 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.5.1 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.5.0 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.4.4 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.4.3 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.4.2 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.4.1 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.4.0 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.3.0 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.2.1 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.2.0 lib/generators/activity_notification/controllers_generator.rb
activity_notification-1.1.0 lib/generators/activity_notification/controllers_generator.rb