Skip to content

Instantly share code, notes, and snippets.

@idlefingers
Created February 4, 2011 15:08
Show Gist options
  • Save idlefingers/811202 to your computer and use it in GitHub Desktop.
Save idlefingers/811202 to your computer and use it in GitHub Desktop.

Revisions

  1. Damien Timewell created this gist Feb 4, 2011.
    146 changes: 146 additions & 0 deletions rails_remplate.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,146 @@
    app_name = ARGV[0].humanize

    # Remove unwanted files
    remove_file "public/index.html"
    remove_file "public/favicon.ico"
    remove_file "public/images/rails.png"
    remove_file "public/robots.txt"

    # Add gems to Gemfile
    append_to_file 'Gemfile', %Q{
    gem 'jquery-rails'
    gem 'devise'
    gem 'dynamic_form'
    gem 'will_paginate', '>= 3.0.beta'
    group :development, :test do
    gem 'rspec', '>= 2.3.0'
    gem 'rspec-rails', '>= 2.3.0'
    gem 'remarkable', '>= 4.0.0.alpha4'
    gem 'remarkable_activerecord', '>= 4.0.0.alpha4'
    gem 'machinist', '~> 2.0.0.beta2'
    gem 'steak', '>= 1.1.0'
    gem 'capybara'
    gem 'delorean'
    gem 'faker'
    gem 'database_cleaner'
    gem 'ruby-debug'
    end
    }

    # Install gems
    run "bundle install"

    # Replace stock README with just the app name
    remove_file "README"
    file "README", app_name

    # Define generators in application.rb
    inject_into_file 'config/application.rb', :before => " end\nend" do
    %Q{
    config.generators do |g|
    g.integration_tool :rspec
    g.test_framework :rspec
    g.helper false
    end
    }
    end

    # Add :password_confirmation to config.filter_parameters
    gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'

    # Set default time zone to London
    gsub_file 'config/application.rb', "# config.time_zone = 'Central Time (US & Canada)'", "config.time_zone = 'London'"

    # Replace prototype with jQuery
    generate "jquery:install", "--force"

    # Don't use asset IDs in development
    append_to_file 'config/environments/development.rb', "ENV['RAILS_ASSET_ID'] = ''"

    # Install rspec & steak
    generate "rspec:install"
    generate "steak:install"

    # Add remarkable and machinist to Rspec requires
    inject_into_file "spec/spec_helper.rb", :after => "require 'rspec/rails'" do
    %Q{
    require 'remarkable/active_record'
    require 'machinist/active_record'}
    end

    # Configure machinist and include devise test helpers
    inject_into_file "spec/spec_helper.rb", :after => " config.use_transactional_fixtures = true" do
    %Q{
    config.before(:each) do
    Machinist.reset_before_test
    end
    config.include Devise::TestHelpers, :type => :controller}
    end

    # Stop machinist from caching objects
    append_to_file "spec/spec_helper.rb", %Q{
    Machinist.configure do |config|
    config.cache_objects = false
    end
    }

    # Devise install
    if yes?("Would you like to install Devise and generate a Devise model now?")
    generate "devise:install"

    devise_model_name = ask("What should the Devise model be called? (Default: 'User')")
    devise_model_name = "User" if devise_model_name.blank?
    generate "devise", devise_model_name
    generate "devise:views"
    end

    # Create a couple of useful helpers in ApplicationHelper
    inject_into_file 'app/helpers/application_helper.rb', :after => "module ApplicationHelper" do
    %Q{
    def button_submit(text)
    content_tag :p, content_tag(:button, text, :type => :submit), :class => "submit"
    end
    def display_flashes
    return "" if flash.empty?
    out = []
    flash.each do |key, value|
    out << content_tag(:li, value, :class => key)
    end
    content_tag :ul, out.join("\\n").html_safe, :class => "flashes"
    end
    def page_title
    @page_title ? @page_title + " - #{app_name}" : "#{app_name}"
    end
    def title_tag
    content_tag :title, page_title
    end
    def tab(text, link=nil)
    css_class = (controller.controller_name.humanize =~ /\#{text}/i ? "active" : "inactive")
    content_tag :li, :class => css_class do
    link_to text, (link ? link : send(text.downcase.underscore + "_path"))
    end
    end
    }
    end

    # Database config
    run "cp config/database.yml config/database.yml.example"
    append_to_file '.gitignore', "config/database.yml"

    if yes?("Create and prepare db?")
    rake "db:create:all"
    rake "db:migrate"
    rake "db:test:prepare"
    end

    # Git
    git :init
    git :add => "."
    git :commit => "-a -m 'Setting up project'"