Skip to content

Instantly share code, notes, and snippets.

@fxposter
Created October 22, 2015 11:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fxposter/9133bc62642c75c33189 to your computer and use it in GitHub Desktop.
Save fxposter/9133bc62642c75c33189 to your computer and use it in GitHub Desktop.
Default Rails template
# rails new app_name -d mysql --skip-bundle -m path/to/this/template.rb
comment_lines 'Gemfile', "gem 'turbolinks'"
comment_lines 'Gemfile', "gem 'jbuilder'"
comment_lines 'Gemfile', "gem 'sass-rails'"
comment_lines 'Gemfile', "gem 'coffee-rails'"
comment_lines 'Gemfile', "gem 'uglifier'"
comment_lines 'Gemfile', "gem 'sdoc'"
gsub_file 'Gemfile', "gem 'mysql2'", "gem 'mysql2', '~> 0.3.20'"
inject_into_file 'Gemfile', <<-EOF, :before => "# Use jquery as the JavaScript library"
group :assets do
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'therubyracer', platforms: :ruby
gem 'bootstrap-sass', '~> 3.3'
end
EOF
inject_into_file 'Gemfile', <<-EOF, :after => /gem 'jquery-rails'.*$/
gem 'responders'
gem 'simple_form','~> 3.2' # rails generate simple_form:install --bootstrap
gem 'airbrake'
gem 'omniauth-google-oauth2'
# gem 'mutations' # https://developer.uservoice.com/blog/2013/02/27/introducing-mutations-putting-soa-on-rails-for-security-and-maintainability/
group :development do
gem 'binding_of_caller'
gem 'better_errors'
gem 'pry-rails'
gem 'bond'
gem 'jist'
gem 'pry-byebug'
gem 'pry-doc'
gem 'pry-docmore'
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'pry-remote'
gem 'quiet_assets'
gem 'spring-commands-rspec'
# gem 'bullet'
# gem 'flog', :require => false
# gem 'reek', :require => false
# gem 'flay', :require => false
# gem 'localtunnel', :require => false
# gem 'rack-mini-profiler'
# gem 'rails_best_practices', :require => false
end
group :development, :test do
gem 'rspec-rails', '~> 3.3', :require => false # rails generate rspec:install
gem 'factory_girl_rails', :require => false
gem 'connection_pool'
end
group :test do
gem 'capybara', '~> 2.5', :require => false
gem 'launchy', :require => false
gem 'poltergeist'
# gem 'timecop'
# gem 'shoulda-matchers'
# gem 'capybara-screenshot'
# gem 'rb-fsevent', :require => false
# gem 'rb-inotify', :require => false
gem 'terminal-notifier-guard', :require => false
gem 'libnotify', :require => false
gem 'guard-rspec', :require => false # guard init rspec
gem 'simplecov', :require => false
end
EOF
comment_lines 'config/application.rb', Regexp.escape('Bundler.require(*Rails.groups)')
inject_into_file 'config/application.rb', "\nBundler.require(*Rails.groups(:assets => %w(development test)))", :after => /Bundler\.require\(\*Rails\.groups\).*$/
inject_into_file 'config/application.rb', <<-EOF, :after => /config\.i18n\.default_locale.*$/
config.generators do |g|
g.test_framework :rspec
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.integration_tool :rspec
g.stylesheets false
g.javascripts false
g.view_specs false
end
EOF
run 'bundle update'
generate 'simple_form:install --bootstrap'
generate 'rspec:install'
run 'guard init rspec'
run 'spring binstub --all'
gsub_file 'Guardfile', 'guard :rspec, cmd: "bundle exec rspec" do', 'guard :rspec, cmd: "bin/rspec" do'
inject_into_file 'Guardfile', <<-EOF, :after => /# More info at https:\/\/github\.com\/guard\/guard#readme.*$/
if RUBY_PLATFORM =~ /darwin/
notification :terminal_notifier
else
notification :libnotify
end
EOF
file '.rspec', <<-EOF, :force => true
--color
--format documentation
EOF
file 'config/initializers/omniauth.rb', <<-EOF
Rails.application.config.middleware.use OmniAuth::Builder do
provider :developer unless Rails.env.production?
if Rails.application.secrets.google_client_id.present? && Rails.application.secrets.google_client_secret.present?
provider :google_oauth2, Rails.application.secrets.google_client_id, Rails.application.secrets.google_client_secret,
:scope => 'userinfo.email', :approval_prompt => 'auto'
end
end
EOF
file 'config/initializers/airbrake.rb', <<-EOF
if Rails.application.secrets.airbrake_api_key.present?
require 'rake'
require 'airbrake/rake_handler'
Airbrake.configure do |config|
config.api_key = Rails.application.secrets.airbrake_api_key
config.port = 80
config.secure = config.port == 443
config.rescue_rake_exceptions = true
# config.async = true
end
end
EOF
file 'lib/templates/erb/scaffold/_form.html.erb', <<-EOF, :force => true
<%%= simple_form_for(@<%= singular_table_name %>, :html => { :class => 'form-horizontal' },
:wrapper => :horizontal_form,
:wrapper_mappings => {
:check_boxes => :horizontal_radio_and_checkboxes,
:radio_buttons => :horizontal_radio_and_checkboxes,
:file => :horizontal_file_input,
:boolean => :horizontal_boolean
}) do |f| %>
<%%= f.error_notification %>
<fieldset>
<%- attributes.each do |attribute| -%>
<%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
<%- end -%>
</fieldset>
<div class="form-group">
<div class="form-actions col-sm-offset-3 col-sm-9">
<%%= f.button :submit %>
</div>
</div>
<%% end %>
EOF
file 'lib/templates/erb/scaffold/index.html.erb', <<-EOF, :force => true
<h1>Listing <%= plural_table_name.titleize %></h1>
<table class="table table-striped table-hover">
<thead>
<tr>
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<th><%= attribute.human_name %></th>
<% end -%>
<th></th>
</tr>
</thead>
<tbody>
<%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
<tr>
<% attributes.reject(&:password_digest?).each_with_index do |attribute, index| -%>
<% if index.zero? -%>
<td><%%= link_to <%= singular_table_name %>.<%= attribute.name %>, <%= singular_table_name %> %></td>
<% else -%>
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end -%>
<% end -%>
<td>
<%%= link_to '<span class="glyphicon glyphicon-edit" aria-hidden="true"></span>'.html_safe, edit_<%= singular_table_name %>_path(datacenter) %>
<%%= link_to '<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>'.html_safe, <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<%% end %>
</tbody>
</table>
<br>
<%%= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path %>
EOF
file 'lib/templates/rails/scaffold_controller/controller.rb', <<-EOF
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
respond_to :html, :json
def index
respond_with(<%= plural_table_name %>)
end
def show
respond_with(<%= singular_table_name %>)
end
def new
build_<%= singular_table_name %>
respond_with(<%= singular_table_name %>)
end
def edit
respond_with(<%= singular_table_name %>)
end
def create
build_<%= singular_table_name %>(<%= "\#{singular_table_name}_params" %>).save
respond_with(<%= singular_table_name %>)
end
def update
<%= orm_instance.update("\#{singular_table_name}_params") %>
respond_with(<%= singular_table_name %>)
end
def destroy
<%= orm_instance.destroy %>
respond_with(<%= singular_table_name %>)
end
private
def <%= plural_table_name %>
@<%= plural_table_name %> ||= <%= orm_class.all(class_name) %>
end
def <%= singular_table_name %>
@<%= singular_table_name %> ||= <%= orm_class.find(class_name, "params[:id]") %>
end
def build_<%= singular_table_name %>(attributes = nil)
@<%= singular_table_name %> ||= <%= orm_class.build(class_name, "attributes") %>
end
def <%= "\#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params[:<%= singular_table_name %>]
<%- else -%>
params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":\#{name}" }.join(', ') %>)
<%- end -%>
end
end
<% end -%>
EOF
uncomment_lines 'spec/rails_helper.rb', /spec\/support\/\*/
file 'spec/support/factory_girl.rb', <<-EOF
require 'factory_girl_rails'
EOF
file 'spec/support/active_record_shared_connection.rb', <<-EOF
# https://gist.github.com/3049152
require 'connection_pool'
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
Spring.after_fork do
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
end
EOF
file 'spec/support/capybara.rb', <<-EOF
require 'capybara/rails'
require 'capybara/rspec'
Capybara.javascript_driver = :selenium
Capybara.default_selector = :css
Capybara.default_max_wait_time = 5
Capybara.register_driver :selenium do |app|
require 'selenium/webdriver'
profile = Selenium::WebDriver::Firefox::Profile.new
profile['permissions.default.image'] = 2 # Disable images
Capybara::Selenium::Driver.new(app, :profile => profile)
end
EOF
file 'spec/support/devise.rb', <<-EOF
# Devise.stretches = 1
EOF
file 'spec/support/omniauth.rb', <<-EOF
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock :google_oauth2, :provider => 'google_oauth2', :uid => 'fxposter@gmail.com', :info => { :email => 'fxposter@gmail.com' }
EOF
file 'spec/support/rails.rb', <<-EOF
Rails.logger.level = 4
EOF
file 'spec/support/rspec.rb', <<-EOF
RSpec.configure do |config|
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# config.filter_run_including :focus => true
# config.add_should_and_should_not_to Delegator
# config.include Devise::TestHelpers, :type => :controller
# config.after :each do
# ActionMailer::Base.deliveries.clear
# end
config.include Rails.application.routes.url_helpers, :type => :request
end
EOF
file 'spec/support/simplecov.rb', <<-EOF
require 'simplecov'
SimpleCov.start 'rails'
EOF
file 'spec/support/devise.rb', <<-EOF
# Devise.stretches = 1
EOF
file 'spec/factories_spec.rb', <<-EOF
require 'rails_helper'
FactoryGirl.factories.map(&:name).each do |factory_name|
describe "\#{factory_name} factory" do
it 'is valid' do
FactoryGirl.build(factory_name).should be_valid
end
end
end
EOF
remove_file 'app/assets/javascripts/application.js'
remove_file 'app/assets/stylesheets/application.css'
remove_dir 'test'
file 'app/assets/stylesheets/application.css.scss', <<-EOF
@import "bootstrap-sprockets";
@import "bootstrap";
body {
padding-top: 50px;
}
EOF
file 'app/assets/javascripts/application.js.coffee', <<-EOF
#= require jquery
#= require jquery_ujs
#= require bootstrap/dropdown
#= require_tree .
EOF
file 'app/views/layouts/application.html.erb', <<-EOF, :force => true
<!DOCTYPE html>
<html>
<head>
<title>#{app_name.humanize}</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= csrf_meta_tags %>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="navbar-header">
<%= link_to '#{app_name.humanize}', root_path, :class => 'navbar-brand' %>
</div>
<ul class="nav navbar-nav">
<li><%= link_to 'LINKS', '#' %></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<% if true # current_user %>
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
CURRENT USER
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to 'LOGOUT', '#' %></li>
</ul>
<% end %>
</li>
</ul>
</nav>
<div class="container">
<% flash.each do |type, message| %>
<div class="alert alert-<%= type %>"><%= message %></div>
<% end %>
<%= yield %>
</div>
<%= javascript_include_tag "application" %>
</body>
</html>
EOF
git :init
git :add => ".", :commit => "-m 'Initial commit'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment