Sha256: 2fc8f5896b36f6583fc98a820fd5ce0f6370cab9019de62e8849ed1e6e2b5a81

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

# rails generate effective:controller NAME [action action] [options]

# Generates a controller
# rails generate effective:controller Thing
# rails generate effective:controller Thing index edit create
# rails generate effective:controller Thing index edit create --attributes name:string description:text

module Effective
  module Generators
    class ControllerGenerator < Rails::Generators::NamedBase
      include Helpers

      source_root File.expand_path(('../' * 4) + 'lib/scaffolds', __FILE__)

      desc 'Creates a controller in your app/controllers folder.'

      argument :actions, type: :array, default: ['crud'], banner: 'action action'
      class_option :attributes, type: :array, default: [], desc: 'Included permitted params, otherwise read from model'

      def assign_actions
        @actions = invoked_actions
      end

      def assign_attributes
        @attributes = (invoked_attributes.presence || klass_attributes).map do |attribute|
          Rails::Generators::GeneratedAttribute.parse(attribute)
        end

        self.class.send(:attr_reader, :attributes)
      end

      def invoke_controller
        say_status :invoke, :controller, :white
      end

      def create_controller
        template 'controllers/controller.rb', File.join('app/controllers', namespace_path, "#{plural_name}_controller.rb")
      end

      protected

      def permitted_param_for(attribute_name)
        case attribute_name
        when 'roles'
          'roles: EffectiveRoles.permitted_params'
        else
          ':' + attribute_name
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
effective_developer-0.0.10 lib/generators/effective/controller_generator.rb
effective_developer-0.0.9 lib/generators/effective/controller_generator.rb