Sha256: f45c55a82a1652cac4a478da0bed5b4b40eafcf94406a1ae0bc569ce18aaba8e

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

require 'rails/generators'

module Layout
  module Generators
    class LayoutGenerator < ::Rails::Generators::Base
      source_root File.expand_path("../templates", __FILE__)
      argument :framework_name, :type => :string, :default => "simple"

      attr_reader :app_name

      # Create an application layout file with partials for messages and navigation
      def generate_layout
        app = ::Rails.application
        @app_name = app.class.to_s.split("::").first
        ext = app.config.generators.options[:rails][:template_engine] || :erb
        # so far, I've only got templates for ERB, but Haml and Slim could be added
        template "#{framework_name}-application.html.#{ext}", "app/views/layouts/application.html.#{ext}"
        copy_file "#{framework_name}-messages.html.#{ext}", "app/views/layouts/_messages.html.#{ext}"
        copy_file "#{framework_name}-navigation.html.#{ext}", "app/views/layouts/_navigation.html.#{ext}"
      end

      # Add a simple stylesheet if there is no front-end framework
      def simple_css
        if framework_name == 'simple'
          copy_file 'simple.css', 'app/assets/stylesheets/simple.css'
        else
          remove_file 'app/assets/stylesheets/simple.css'
        end
      end

      # If 'About' or 'Contact' views exist in known locations, add navigation links
      def add_navigation_links
        # not yet accommodating Haml and Slim (we'll need different substitutions)
        if File.exists?('app/views/pages/about.html.erb')
          insert_into_file 'app/views/layouts/_navigation.html.erb', "\n  <li><%= link_to 'About', page_path('about') %></li>", :before => "\n</ul>"
        end
        if File.exists?('app/views/contacts/new.html.erb')
          insert_into_file 'app/views/layouts/_navigation.html.erb', "\n  <li><%= link_to 'Contact', new_contact_path %></li>", :before => "\n</ul>"
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_layout-0.1.6 lib/generators/layout/layout_generator.rb
rails_layout-0.1.4 lib/generators/layout/layout_generator.rb