Skip to content

Instantly share code, notes, and snippets.

@tonytonyjan
Last active October 12, 2015 10:07
Show Gist options
  • Save tonytonyjan/4010690 to your computer and use it in GitHub Desktop.
Save tonytonyjan/4010690 to your computer and use it in GitHub Desktop.
tonytonyjan's rails template
# Usage:
# > rails new myapp -m https://gist.github.com/raw/4010690/d9106a1c3af695d1ed4ef8d16e3c8a06b5e3c7f3/tj_rails_template.rb
config = {}
puts "Gems:"
config[:install_devise_cancan_rolify] = yes? "install devise, cancan, rolify?"
config[:install_form_helpers] = yes? "install simple_form, dynamic_form, nested_form?"
config[:install_bootstrap_tjstyle] = yes? "install bootstrap, tjstyle?"
config[:install_rspec_spork] = yes? "install rspec, spork?"
config[:install_carrer_wave] = yes? "install carrierwave?"
config[:install_faker] = yes? "install faker?"
puts "Addition:"
config[:generate_home_page] = yes? "generate default home page?"
config[:config_heroku] = yes? "config_heroku?"
require 'tempfile'
require 'fileutils'
def modify_file path, &block
temp_file = Tempfile.new("foo")
begin
File.open(path, 'r') do |file|
file.each_line do |line|
yield temp_file, line
end
end
temp_file.rewind
FileUtils.mv(temp_file.path, path)
ensure
temp_file.close
temp_file.unlink
end
end
gem "rails-i18n"
gem "will_paginate"
if config[:install_devise_cancan_rolify]
gem "devise"
generate "devise:install"
generate "devise:views"
generate "devise", "user"
gem "cancan"
generate "cancan:ability"
gem "rolify"
generate "rolify:role"
end
if config[:install_form_helpers]
gem "simple_form"
generate "simple_form:install", "--bootstrap" if config[:install_bootstrap_tjstyle]
gem "nested_form"
gem "dynamic_form"
end
if config[:install_bootstrap_tjstyle]
gem_group :assets do
gem 'bootstrap-sass', '~> 2.2.2.0'
gem 'tjstyle'
end
gem 'will_paginate-bootstrap'
# write will_paginate_helper
file 'app/helpers/will_paginate_helper.rb', <<-CODE
module WillPaginateHelper
# change the default link renderer for will_paginate
def will_paginate(collection_or_options = nil, options = {})
if collection_or_options.is_a? Hash
options, collection_or_options = collection_or_options, nil
end
unless options[:renderer]
options = options.merge :renderer => BootstrapPagination::Rails
end
super *[collection_or_options, options].compact
end
end
CODE
# write application.html.erb
path = "app/views/layouts/application.html.erb"
src = File.read(path)
src.sub! %r(<body.*body>)m, <<-CODE
<body class="<%= controller_name %> <%= action_name %>" data-controller="<%= controller_name %>" data-action="<%= action_name %>">
<%= render :partial => 'layouts/navbar' %>
<div class="container">
<%= yield %>
<hr>
<footer>
<p>&copy; Company 2013</p>
</footer>
</div>
</body>
CODE
File.write path, src
# write application.css.scss
path = "app/assets/stylesheets/application.css"
modify_file(path){ |temp_file, line|
temp_file.puts line
if line =~ %r(\s*\*/)
temp_file.puts <<-CODE
@import 'bootstrap';
body {
padding-top: 60px;
padding-bottom: 40px;
}
@import 'bootstrap-responsive';
CODE
end
}
FileUtils.mv(path, path+'.scss')
# write application.js
modify_file("app/assets/javascripts/application.js"){ |temp_file, line|
if line =~ %r(require_tree \.)
temp_file.puts "//= require bootstrap"
temp_file.puts "//= require tjstyle"
end
temp_file.puts line
}
end
if config[:install_rspec_spork]
gem_group :test, :development do
gem "rspec-rails", "~> 2.0"
end
generate "rspec:install"
gem_group :test do
gem "spork-rails"
end
end
if config[:install_carrer_wave]
gem "fog" if config[:config_heroku]
gem "carrierwave"
end
if config[:install_faker]
gem_group :development do
gem "faker"
end
end
if config[:generate_home_page]
File.delete "public/index.html"
generate(:controller, "home index")
route "root :to => 'home#index'"
if config[:install_bootstrap_tjstyle]
file "app/views/layouts/_navbar.html.erb", <<-CODE
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">Project name</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li class="nav-header">Nav header</li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class="navbar-form pull-right">
<input class="span2" type="text" placeholder="Email">
<input class="span2" type="password" placeholder="Password">
<button type="submit" class="btn">Sign in</button>
</form>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
CODE
file "app/views/home/index.html.erb", <<-CODE
<!-- Main hero unit for a primary marketing message or call to action -->
<div class="hero-unit">
<h1>Hello, world!</h1>
<p>This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique.</p>
<p><a class="btn btn-primary btn-large">Learn more &raquo;</a></p>
</div>
<!-- Example row of columns -->
<div class="row">
<div class="span4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn" href="#">View details &raquo;</a></p>
</div>
<div class="span4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn" href="#">View details &raquo;</a></p>
</div>
<div class="span4">
<h2>Heading</h2>
<p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
<p><a class="btn" href="#">View details &raquo;</a></p>
</div>
</div>
CODE
end
end
if config[:config_heroku]
# gem
gem_group :production do
gem 'pg'
end
gem_group :development do
gem 'sqlite3'
end
# devise
environment "config.assets.initialize_on_precompile = false" if config[:install_devise_cancan_rolify]
# carrierwave
if config[:install_carrer_wave]
# write to carrierwave.rb
initializer "carrierwave.rb", <<-CODE
CarrierWave.configure do |config|
if Rails.env == 'production'
config.storage = :fog
# heroku setting
config.root = Rails.root.join('tmp')
config.cache_dir = 'carrierwave'
# s3 setting
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'xxx', # required
:aws_secret_access_key => 'yyy', # required
# :region => 'eu-west-1', # optional, defaults to 'us-east-1'
# :host => 's3.example.com', # optional, defaults to nil
# :endpoint => 'https://s3.example.com:8080' # optional, defaults to nil
}
config.fog_directory = 'name_of_directory' # required
# config.fog_public = false # optional, defaults to true
# config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
else
config.storage = :file
end
end
CODE
# write to config.ru
modify_file("config.ru"){ |temp_file, line|
temp_file.puts line
if line =~ %r(\s*require\s.*config/environment.*)
temp_file.puts "use Rack::Static, :urls => ['/carrierwave'], :root => 'tmp' # heroku"
end
}
end
end
rake "db:migrate"
# First Commit
git :init
git :add => "."
git :commit => '-m "init"'
@tonytonyjan
Copy link
Author

Usage

rails new myapp -m https://gist.github.com/raw/4010690/d9106a1c3af695d1ed4ef8d16e3c8a06b5e3c7f3/tj_rails_template.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment