-
-
Save zbrock/1997435 to your computer and use it in GitHub Desktop.
Tim's Awesome Rails Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
pretty_name = ARGV[1] | |
pretty_name = 'TODO' if ARGV[1] == '--skip-bundle' | |
author = `dscl localhost -read /Local/Default/Users/$USER RealName`.match(/^RealName:\n\s*(.+?)\n$/m)[1] | |
create_file '.rvmrc', "rvm 1.9.3@#{app_name} --create" | |
gem 'erector' | |
gem 'redcarpet', :require => nil, :group => 'development' | |
gem 'yard', :require => nil, :group => 'development' | |
gem 'rails3-generators', :require => nil, :group => 'development' | |
gem 'rspec-rails', :group => 'test' | |
gem 'factory_girl_rails', :group => 'test' | |
OPTIONAL_GEMS = %w( configoro has_metadata slugalicious paperclip ) | |
selected_gems = OPTIONAL_GEMS.select { |g| yes? "Will this project be using #{g}?" } | |
selected_gems.each { |g| gem g } | |
say_status 'gemset', 'Creating gemset' | |
run "source ~/.rvm/scripts/rvm && rvm --create 1.9.3@#{app_name}", :verbose => false | |
run "rvm 1.9.3@#{app_name} do bundle install", :verbose => false | |
# create database | |
say_status 'db', "Creating database" | |
run "createuser -DRS #{app_name}", :verbose => false | |
run "createdb -O #{app_name} --encoding=utf8 #{app_name}_development", :verbose => false | |
run "createdb -O #{app_name} --encoding=utf8 #{app_name}_test", :verbose => false | |
say_status 'generate', 'rspec:install' | |
run "rvm 1.9.3@#{app_name} do rails g rspec:install", :verbose => false | |
selected_gems.each do |g| | |
say_status 'generate', g | |
run "rvm 1.9.3@#{app_name} do rails g #{g}", :verbose => false | |
end | |
remove_file 'app/assets/images/rails.png' | |
remove_file 'public/index.html' | |
remove_file 'app/views/layouts/application.html.erb' | |
create_file 'app/views/layouts/application.html.rb', <<-RUBY | |
# encoding: utf-8 | |
module Views | |
module Layouts | |
class Application < Erector::Widget | |
def content | |
rawtext "<!DOCTYPE html>" | |
html(lang: 'en') do | |
head_portion | |
body_portion | |
end | |
end | |
protected | |
def body_content | |
raise NotImplementedError | |
end | |
def body_id() nil end | |
private | |
def head_portion | |
metas | |
title "#{pretty_name}" | |
stylesheet_link_tag 'application' | |
inline_css | |
end | |
def metas | |
meta(charset: 'utf-8') | |
meta(name: 'description', content: "TODO") | |
meta(name: 'author', content: "#{author}") | |
end | |
def body_portion | |
body do | |
body_content | |
javascript_include_tag 'application' | |
inline_javascript | |
end | |
end | |
def inline_javascript | |
file = Rails.root.join('app', self.class.to_s.underscore + '.js') | |
script(raw(File.read(file)), type: 'text/javascript') if File.exist?(file) | |
end | |
def inline_css | |
file = Rails.root.join('app', self.class.to_s.underscore + '.css') | |
style(raw(File.read(file)), type: 'text/css') if File.exist?(file) | |
end | |
end | |
end | |
end | |
RUBY | |
gsub_file 'app/assets/stylesheets/application.css', ' *= require_tree .', " *\n *= require normalize" | |
gsub_file 'Gemfile', /:(\w+)\s*=>\s*/, '\1: ' | |
gsub_file 'Gemfile', "source 'https://rubygems.org'", 'source :rubygems' | |
gsub_file 'config/application.rb', /:(\w+)\s*=>\s*/, '\1: ' | |
gsub_file 'config/application.rb', "# config.time_zone = 'Central Time (US & Canada)'", "config.time_zone = 'Pacific Time (US & Canada)'" | |
gsub_file 'config/application.rb', '+= [:password]', '<< :password' | |
gsub_file 'config/application.rb', '"utf-8"', "'utf-8'" | |
inject_into_file 'config/application.rb', <<-RUBY, :after => "config.assets.version = '1.0'" | |
config.generators do |g| | |
g.template_engine :erector | |
g.test_framework :rspec, fixture: true, views: false | |
g.integration_tool :rspec | |
g.fixture_replacement :factory_girl, dir: 'spec/factories' | |
end | |
RUBY | |
remove_file 'config/locales/en.yml' | |
create_file 'config/locales/en.yml', <<-YAML | |
en: | |
activerecord: | |
errors: | |
models: | |
TODO: | |
attributes: | |
TODO: | |
TODO: TODO | |
models: | |
TODO: TODO | |
attributes: | |
TODO: | |
TODO: TODO | |
date: | |
formats: | |
TODO: TODO | |
errors: | |
messages: | |
accepted: must be accepted | |
blank: can’t be blank | |
confirmation: doesn’t match confirmation | |
empty: can’t be empty | |
equal_to: must be equal to %{count} | |
even: must be even | |
exclusion: reserved | |
greater_than: must be greater than %{count} | |
greater_than_or_equal_to: must be greater than or equal to %{count} | |
inclusion: not acceptable | |
incorrect_type: incorrect type | |
invalid: invalid | |
invalid_email: not a valid email address | |
less_than: must be less than %{count} | |
less_than_or_equal_to: must be less than or equal to %{count} | |
not_a_number: not a number | |
not_an_integer: not an integer | |
odd: must be odd | |
taken: already taken | |
too_long: must be shorter than %{count} characters | |
too_short: must be longer than %{count} characters | |
wrong_length: must be be %{count} characters long | |
invalid_date: not a valid date | |
invalid_time: not a valid time | |
invalid_datetime: not a valid date and time | |
is_at: must be at %{restriction} | |
before: must be before %{restriction} | |
on_or_before: must be on or before %{restriction} | |
after: must be after %{restriction} | |
on_or_after: must be on or after %{restriction} | |
helpers: | |
submit: | |
TODO: | |
create: TODO | |
update: TODO | |
time: | |
formats: | |
TODO: TODO | |
YAML | |
create_file 'config/initializers/field_error_proc.rb', <<-RUBY | |
# Change the behavior of fields with bad data. This wraps them in a span with | |
# data-errors attributes listing the errors. JavaScript then creates the | |
# appropriate visual error display. | |
ActionView::Base.field_error_proc = Proc.new do |html, object| | |
errors = Array.wrap(object.error_message).map { |error| %(data-error="\#{error}") }.join(' ') | |
%(<span class="field-with-errors" \#{errors}>\#{html}</span>).html_safe | |
end | |
RUBY | |
create_file 'lib/tasks/doc.rake', <<-RUBY | |
if Rails.env.development? then | |
require 'yard' | |
YARD::Rake::YardocTask.new do |doc| | |
doc.options << '-m' << 'markdown' << '-M' << 'redcarpet' | |
doc.options << '--protected' << '--no-private' | |
doc.options << '-r' << 'doc/README_FOR_APP.md' | |
doc.options << '-o' << 'doc/app' | |
doc.options << '--title' << "#{pretty_name} Documentation" | |
doc.files = %w( app/**/*.rb lib/**/*.rb doc/README_FOR_APP.md ) | |
end | |
end | |
RUBY | |
remove_file 'doc/README_FOR_APP' | |
create_file 'doc/README_FOR_APP.md', <<-MARKDOWN | |
#{pretty_name} | |
#{'='*pretty_name.size} | |
TODO | |
MARKDOWN | |
inject_into_file 'app/controllers/application_controller.rb', 'layout false # handled by view class inheritance', :before => 'protect_from_forgery' | |
gsub_file '.rspec', '--colour', '-cfs' | |
gsub_file 'spec/spec_helper.rb', 'Rails.root.join("spec/support/**/*.rb")', "Rails.root.join('spec', 'support', '**', '*.rb')" | |
gsub_file 'spec/spec_helper.rb', 'config.fixture_path = "#{::Rails.root}/spec/fixtures"', '# config.fixture_path = "#{::Rails.root}/spec/fixtures"' | |
get "https://raw.github.com/necolas/normalize.css/master/normalize.css", 'vendor/assets/stylesheets/normalize.css' | |
remove_file 'log/.gitkeep' | |
create_file 'log/.gitignore', '*.log' | |
remove_file '.gitignore' | |
create_file '.gitignore', ".bundle\n.yardoc" | |
create_file 'db/.gitignore', '*.sqlite3' | |
create_file 'tmp/.gitignore', '*' | |
create_file 'doc/.gitignore', 'app' | |
git :init | |
git :add => '-f tmp/.gitignore' | |
run "mine ." | |
say "Be sure to do a project-wide search for the word 'TODO' now." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment