Sha256: 419d3af6342c51dc656b721b0fffda5411334e3d00562745609d3f8ec2d94884

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

# rails generate effective:menu NAME

# Adds a menu to namespace/_navbar if present
# rails generate effective:menu Thing

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

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

      desc 'Adds a menu link to an existing _navbar.html.haml'

      def invoke_menu
        say_status :invoke, :menu, :white
      end

      def create_menu
        begin
          Effective::CodeWriter.new((['app/views'] + namespaces + ['_navbar.html.haml']).join('/')) do |w|
            if w.find { |line, _| line == content.last.strip }
              say_status :identical, index_path, :blue
            else
              index = w.last { |line, _| line.start_with?('- if can?') }

              if index.blank?
                say_status(:skipped, :menu, :yellow) and return
              end

              w.insert_raw(content, index+1, w.depth_at(index))
              say_status :menu, index_path, :green
            end
          end
        rescue Errno::ENOENT
          # This will raise an error if the navbar file isn't present
          say_status :skipped, :menu, :yellow
        end
      end

      private

      def content
        [
          "\n",
          "- if can? :manage, #{class_name}",
          "  %li= link_to '#{plural_name.titleize}', #{index_path}"
        ]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
effective_developer-0.0.10 lib/generators/effective/menu_generator.rb