Sha256: 68700c1472f91d2168c974e61e405b1585a22bb774bdbb90dc0b0c836fe75347

Contents?: true

Size: 1.74 KB

Versions: 19

Compression:

Stored size: 1.74 KB

Contents

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

# Adds a route to app/models/ability.rb
# rails generate effective:ability Thing
# rails generate effective:ability Thing index edit create

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

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

      desc 'Creates a CanCanCan ability.'

      argument :actions, type: :array, default: ['crud'], banner: 'action action'

      def invoke_ability
        say_status :invoke, :ability, :white
      end

      def create_ability
        unless File.exists?('app/models/ability.rb')
          say_status(:skipped, :ability, :yellow) and return
        end

        Effective::CodeWriter.new('app/models/ability.rb') do |w|
          if w.find { |line, depth| depth == 2 && line == ability }
            say_status :identical, ability, :blue
          else
            w.insert_into_first(ability) { |line, depth| line.start_with?('def initialize') }
          end
        end
      end

      private

      def ability
        @ability ||= (
          abilities = []

          if (crud_actions - invoked_actions).present?
            abilities += (crud_actions & invoked_actions)
          end

          if non_crud_actions.present?
            abilities += non_crud_actions
          end

          abilities = ['manage'] if abilities.blank? || abilities == (crud_actions - ['show'])

          if abilities.length == 1
            abilities = ":#{abilities.first}"
          else
            abilities = '[' + abilities.map { |action| ':' + action }.join(', ') + ']'
          end

          "can #{abilities}, #{resource.class_name}"
        )
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
effective_developer-0.5.5 lib/generators/effective/ability_generator.rb
effective_developer-0.5.4 lib/generators/effective/ability_generator.rb
effective_developer-0.5.3 lib/generators/effective/ability_generator.rb
effective_developer-0.5.2 lib/generators/effective/ability_generator.rb
effective_developer-0.5.1 lib/generators/effective/ability_generator.rb
effective_developer-0.5.0 lib/generators/effective/ability_generator.rb
effective_developer-0.4.16 lib/generators/effective/ability_generator.rb
effective_developer-0.4.15 lib/generators/effective/ability_generator.rb
effective_developer-0.4.14 lib/generators/effective/ability_generator.rb
effective_developer-0.4.13 lib/generators/effective/ability_generator.rb
effective_developer-0.4.12 lib/generators/effective/ability_generator.rb
effective_developer-0.4.11 lib/generators/effective/ability_generator.rb
effective_developer-0.4.10 lib/generators/effective/ability_generator.rb
effective_developer-0.4.9 lib/generators/effective/ability_generator.rb
effective_developer-0.4.8 lib/generators/effective/ability_generator.rb
effective_developer-0.4.7 lib/generators/effective/ability_generator.rb
effective_developer-0.4.6 lib/generators/effective/ability_generator.rb
effective_developer-0.4.5 lib/generators/effective/ability_generator.rb
effective_developer-0.4.4 lib/generators/effective/ability_generator.rb