Skip to content

Instantly share code, notes, and snippets.

@RISCfuture
Last active September 30, 2015 20:27
Show Gist options
  • Save RISCfuture/1857680 to your computer and use it in GitHub Desktop.
Save RISCfuture/1857680 to your computer and use it in GitHub Desktop.
Tim's Awesome Rails Template
# encoding: utf-8
require 'open3'
def rvm_env(cmd)
Open3.popen3 "bash -c 'source ~/.rvm/scripts/rvm && #{cmd}'" do |stdin, stdout, stderr, thread|
thread.join
end
end
def rvm(env, cmd)
rvm_env "rvm #{env} do #{cmd}"
end
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 '.ruby-version', '2.0.0'
create_file '.ruby-gemset' app_name
gem 'erector'
gem 'redcarpet', :require => nil, :group => 'development'
gem 'yard', :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'
rvm_env "rvm use --create 1.9.3@#{app_name}"
rvm "1.9.3@#{app_name}", 'bundle install'
# 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'
rvm "1.9.3@#{app_name}", 'rails g rspec:install'
selected_gems.each do |g|
say_status 'generate', g
rvm "1.9.3@#{app_name}", "rails g #{g}"
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 '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', "config.active_record.schema_format = :ruby", "config.active_record.schema_format = :sql"
gsub_file 'config/application.rb',
'# config.autoload_paths += %W(#{config.root}/extras)", "config.autoload_paths << config.root.join(\'lib\')'
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 %{count} characters or shorter
too_short: must be %{count} characters or longer
wrong_length: must 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
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