Created
December 8, 2011 07:08
-
-
Save milep/1446350 to your computer and use it in GitHub Desktop.
Rails 3.1 starter 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
# >---------------------------------------------------------------------------< | |
# | |
# _____ _ _ __ ___ _ | |
# | __ \ (_) | \ \ / (_) | | | |
# | |__) |__ _ _| |___\ \ /\ / / _ ______ _ _ __ __| | | |
# | _ // _` | | / __|\ \/ \/ / | |_ / _` | '__/ _` | | |
# | | \ \ (_| | | \__ \ \ /\ / | |/ / (_| | | | (_| | | |
# |_| \_\__,_|_|_|___/ \/ \/ |_/___\__,_|_| \__,_| | |
# | |
# This template was generated by rails_apps_composer, a custom version of | |
# RailsWizard, the application template builder. For more information, see: | |
# https://github.com/RailsApps/rails_apps_composer/ | |
# | |
# >---------------------------------------------------------------------------< | |
# >----------------------------[ Initial Setup ]------------------------------< | |
initializer 'generators.rb', <<-RUBY | |
Rails.application.config.generators do |g| | |
end | |
RUBY | |
@recipes = ["haml", "application_layout", "devise", "navigation", "rails_admin", "settingslogic", "inherited_resources", "cleanup"] | |
def recipes; @recipes end | |
def recipe?(name); @recipes.include?(name) end | |
def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end | |
def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end | |
def say_wizard(text); say_custom(@current_recipe || 'wizard', text) end | |
def ask_wizard(question) | |
ask "\033[1m\033[30m\033[46m" + (@current_recipe || "prompt").rjust(10) + "\033[0m\033[36m" + " #{question}\033[0m" | |
end | |
def yes_wizard?(question) | |
answer = ask_wizard(question + " \033[33m(y/n)\033[0m") | |
case answer.downcase | |
when "yes", "y" | |
true | |
when "no", "n" | |
false | |
else | |
yes_wizard?(question) | |
end | |
end | |
def no_wizard?(question); !yes_wizard?(question) end | |
def multiple_choice(question, choices) | |
say_custom('question', question) | |
values = {} | |
choices.each_with_index do |choice,i| | |
values[(i + 1).to_s] = choice[1] | |
say_custom (i + 1).to_s + ')', choice[0] | |
end | |
answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer) | |
values[answer] | |
end | |
@current_recipe = nil | |
@configs = {} | |
@after_blocks = [] | |
def after_bundler(&block); @after_blocks << [@current_recipe, block]; end | |
@after_everything_blocks = [] | |
def after_everything(&block); @after_everything_blocks << [@current_recipe, block]; end | |
@before_configs = {} | |
def before_config(&block); @before_configs[@current_recipe] = block; end | |
case Rails::VERSION::MAJOR.to_s | |
when "3" | |
case Rails::VERSION::MINOR.to_s | |
when "1" | |
say_wizard "You are using Rails version #{Rails::VERSION::STRING}." | |
@recipes << 'rails 3.1' | |
when "0" | |
say_wizard "You are using Rails version #{Rails::VERSION::STRING}." | |
@recipes << 'rails 3.0' | |
else | |
say_wizard "You are using Rails version #{Rails::VERSION::STRING} which is not supported." | |
end | |
else | |
say_wizard "You are using Rails version #{Rails::VERSION::STRING} which is not supported." | |
end | |
# show which version of rake is running | |
# with the added benefit of ensuring that the Gemfile's version of rake is activated | |
gemfile_rake_ver = run 'bundle exec rake --version', :capture => true, :verbose => false | |
say_wizard "You are using #{gemfile_rake_ver.strip}" | |
say_wizard "Checking configuration. Please confirm your preferences." | |
# >---------------------------[ Autoload Modules/Classes ]-----------------------------< | |
inject_into_file 'config/application.rb', :after => 'config.autoload_paths += %W(#{config.root}/extras)' do <<-'RUBY' | |
config.autoload_paths += %W(#{config.root}/lib) | |
RUBY | |
end | |
# >---------------------------[ Javascript Runtime ]-----------------------------< | |
prepend_file 'Gemfile' do <<-RUBY | |
require 'rbconfig' | |
HOST_OS = RbConfig::CONFIG['host_os'] | |
RUBY | |
end | |
if recipes.include? 'rails 3.1' | |
append_file 'Gemfile' do <<-RUBY | |
# install a Javascript runtime for linux | |
if HOST_OS =~ /linux/i | |
gem 'therubyracer', '>= 0.9.8' | |
end | |
RUBY | |
end | |
end | |
# >---------------------------------[ Recipes ]----------------------------------< | |
# >---------------------------------[ HAML ]----------------------------------< | |
@current_recipe = "haml" | |
@before_configs["haml"].call if @before_configs["haml"] | |
say_recipe 'HAML' | |
config = {} | |
config['haml'] = yes_wizard?("Would you like to use Haml instead of ERB?") if true && true unless config.key?('haml') | |
@configs[@current_recipe] = config | |
# Application template recipe for the rails_apps_composer. Check for a newer version here: | |
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/haml.rb | |
if config['haml'] | |
if recipes.include? 'rails 3.0' | |
# for Rails 3.0, use only gem versions we know that work | |
gem 'haml', '3.1.1' | |
gem 'haml-rails', '0.3.4', :group => :development | |
else | |
# for Rails 3.1+, use optimistic versioning for gems | |
gem 'haml', '>= 3.1.2' | |
gem 'haml-rails', '>= 0.3.4', :group => :development | |
end | |
else | |
recipes.delete('haml') | |
end | |
# >---------------------------[ ApplicationLayout ]---------------------------< | |
@current_recipe = "application_layout" | |
@before_configs["application_layout"].call if @before_configs["application_layout"] | |
say_recipe 'ApplicationLayout' | |
@configs[@current_recipe] = config | |
# Application template recipe for the rails_apps_composer. Check for a newer version here: | |
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/application_layout.rb | |
after_bundler do | |
say_wizard "ApplicationLayout recipe running 'after bundler'" | |
# Set up the default application layout | |
if recipes.include? 'haml' | |
remove_file 'app/views/layouts/application.html.erb' | |
remove_file 'app/views/layouts/application.html.haml' | |
# There is Haml code in this script. Changing the indentation is perilous between HAMLs. | |
create_file 'app/views/layouts/application.html.haml' do <<-HAML | |
!!! 5 | |
%html | |
%head | |
%title #{app_name} | |
= stylesheet_link_tag :application | |
= javascript_include_tag :application | |
= csrf_meta_tags | |
%body | |
- flash.each do |name, msg| | |
= content_tag :div, msg, :id => "flash_\#{name}" if msg.is_a?(String) | |
= yield | |
HAML | |
end | |
if recipes.include? 'rails 3.0' | |
gsub_file 'app/views/layouts/application.html.haml', /stylesheet_link_tag :application/, 'stylesheet_link_tag :all' | |
gsub_file 'app/views/layouts/application.html.haml', /javascript_include_tag :application/, 'javascript_include_tag :defaults' | |
gsub_file 'app/views/layouts/application.html.haml', /csrf_meta_tags/, 'csrf_meta_tag' | |
end | |
else | |
unless recipes.include? 'html5' | |
inject_into_file 'app/views/layouts/application.html.erb', :after => "<body>\n" do | |
<<-ERB | |
<%- flash.each do |name, msg| -%> | |
<%= content_tag :div, msg, :id => "flash_\#{name}" if msg.is_a?(String) %> | |
<%- end -%> | |
ERB | |
end | |
end | |
end | |
end | |
# >--------------------------------[ Devise ]---------------------------------< | |
@current_recipe = "devise" | |
@before_configs["devise"].call if @before_configs["devise"] | |
say_recipe 'Devise' | |
config = {} | |
config['devise'] = yes_wizard?("Would you like to use Devise for authentication?") if true && true unless config.key?('devise') | |
@configs[@current_recipe] = config | |
# Application template recipe for the rails_apps_composer. Check for a newer version here: | |
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/devise.rb | |
if config['devise'] | |
if recipes.include? 'rails 3.0' | |
# for Rails 3.0, use only gem versions we know that work | |
gem 'devise', '1.3.4' | |
else | |
# for Rails 3.1+, use optimistic versioning for gems | |
gem 'devise', '>= 1.5.0' | |
end | |
else | |
recipes.delete('devise') | |
end | |
if config['devise'] | |
after_bundler do | |
say_wizard "Devise recipe running 'after bundler'" | |
# Run the Devise generator | |
generate 'devise:install' | |
if recipes.include? 'mongo_mapper' | |
gem 'mm-devise' | |
gsub_file 'config/initializers/devise.rb', 'devise/orm/', 'devise/orm/mongo_mapper_active_model' | |
generate 'mongo_mapper:devise User' | |
elsif recipes.include? 'mongoid' | |
# Nothing to do (Devise changes its initializer automatically when Mongoid is detected) | |
# gsub_file 'config/initializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongoid' | |
end | |
# Prevent logging of password_confirmation | |
gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation' | |
if recipes.include? 'cucumber' | |
# Cucumber wants to test GET requests not DELETE requests for destroy_user_session_path | |
# (see https://github.com/RailsApps/rails3-devise-rspec-cucumber/issues/3) | |
gsub_file 'config/initializers/devise.rb', 'config.sign_out_via = :delete', 'config.sign_out_via = Rails.env.test? ? :get : :delete' | |
end | |
end | |
after_everything do | |
say_wizard "Devise recipe running 'after everything'" | |
if recipes.include? 'rspec' | |
say_wizard "Copying RSpec files from the rails3-devise-rspec-cucumber examples" | |
begin | |
# copy all the RSpec specs files from the rails3-devise-rspec-cucumber example app | |
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/factories.rb', 'spec/factories.rb' | |
remove_file 'spec/controllers/home_controller_spec.rb' | |
remove_file 'spec/controllers/users_controller_spec.rb' | |
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/controllers/home_controller_spec.rb', 'spec/controllers/home_controller_spec.rb' | |
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/controllers/users_controller_spec.rb', 'spec/controllers/users_controller_spec.rb' | |
remove_file 'spec/models/user_spec.rb' | |
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/models/user_spec.rb', 'spec/models/user_spec.rb' | |
rescue OpenURI::HTTPError | |
say_wizard "Unable to obtain RSpec example files from the repo" | |
end | |
remove_file 'spec/views/home/index.html.erb_spec.rb' | |
remove_file 'spec/views/home/index.html.haml_spec.rb' | |
remove_file 'spec/views/users/show.html.erb_spec.rb' | |
remove_file 'spec/views/users/show.html.haml_spec.rb' | |
remove_file 'spec/helpers/home_helper_spec.rb' | |
remove_file 'spec/helpers/users_helper_spec.rb' | |
end | |
end | |
end | |
# >------------------------------[ Navigation ]-------------------------------< | |
@current_recipe = "navigation" | |
@before_configs["navigation"].call if @before_configs["navigation"] | |
say_recipe 'Navigation' | |
@configs[@current_recipe] = config | |
# Application template recipe for the rails_apps_composer. Check for a newer version here: | |
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/navigation.rb | |
after_bundler do | |
say_wizard "Navigation recipe running 'after bundler'" | |
if recipes.include? 'devise' | |
# Create navigation links for Devise | |
if recipes.include? 'haml' | |
# There is Haml code in this script. Changing the indentation is perilous between HAMLs. | |
# We have to use single-quote-style-heredoc to avoid interpolation. | |
create_file "app/views/shared/_navigation.html.haml" do <<-'HAML' | |
- if user_signed_in? | |
%li | |
= link_to('Logout', destroy_user_session_path, :method=>'delete') | |
- else | |
%li | |
= link_to('Login', new_user_session_path) | |
- if user_signed_in? | |
%li | |
= link_to('Edit account', edit_user_registration_path) | |
- else | |
%li | |
= link_to('Sign up', new_user_registration_path) | |
HAML | |
end | |
else | |
create_file "app/views/shared/_navigation.html.erb" do <<-ERB | |
<% if user_signed_in? %> | |
<li> | |
<%= link_to('Logout', destroy_user_session_path, :method=>'delete') %> | |
</li> | |
<% else %> | |
<li> | |
<%= link_to('Login', new_user_session_path) %> | |
</li> | |
<% end %> | |
<% if user_signed_in? %> | |
<li> | |
<%= link_to('Edit account', edit_user_registration_path) %> | |
</li> | |
<% else %> | |
<li> | |
<%= link_to('Sign up', new_user_registration_path) %> | |
</li> | |
<% end %> | |
ERB | |
end | |
end | |
else | |
# Create navigation links | |
if recipes.include? 'haml' | |
# There is Haml code in this script. Changing the indentation is perilous between HAMLs. | |
# We have to use single-quote-style-heredoc to avoid interpolation. | |
create_file "app/views/shared/_navigation.html.haml" do <<-'HAML' | |
- if user_signed_in? | |
%li | |
Logged in as #{current_user.name} | |
%li | |
= link_to('Logout', signout_path) | |
- else | |
%li | |
= link_to('Login', signin_path) | |
HAML | |
end | |
else | |
create_file "app/views/shared/_navigation.html.erb" do <<-ERB | |
<% if user_signed_in? %> | |
<li> | |
Logged in as <%= current_user.name %> | |
</li> | |
<li> | |
<%= link_to('Logout', signout_path) %> | |
</li> | |
<% else %> | |
<li> | |
<%= link_to('Login', signin_path) %> | |
</li> | |
<% end %> | |
ERB | |
end | |
end | |
end | |
# Add navigation links to the default application layout | |
if recipes.include? 'html5' | |
if recipes.include? 'haml' | |
# There is Haml code in this script. Changing the indentation is perilous between HAMLs. | |
inject_into_file 'app/views/layouts/application.html.haml', :after => "%header\n" do <<-HAML | |
%nav | |
%ul.hmenu | |
= render 'shared/navigation' | |
HAML | |
end | |
else | |
inject_into_file 'app/views/layouts/application.html.erb', :after => "<header>\n" do <<-ERB | |
<nav> | |
<ul class="hmenu"> | |
<%= render 'shared/navigation' %> | |
</ul> | |
</nav> | |
ERB | |
end | |
end | |
else | |
if recipes.include? 'haml' | |
# There is Haml code in this script. Changing the indentation is perilous between HAMLs. | |
inject_into_file 'app/views/layouts/application.html.haml', :after => "%body\n" do <<-HAML | |
%ul.hmenu | |
= render 'shared/navigation' | |
HAML | |
end | |
else | |
inject_into_file 'app/views/layouts/application.html.erb', :after => "<body>\n" do | |
<<-ERB | |
<ul class="hmenu"> | |
<%= render 'shared/navigation' %> | |
</ul> | |
ERB | |
end | |
end | |
end | |
# Throw it all away and create new navigation if we're enabling subdomains | |
if recipes.include? 'subdomains' | |
remove_file 'app/views/shared/_navigation.html.haml' | |
# There is Haml code in this script. Changing the indentation is perilous between HAMLs. | |
# We have to use single-quote-style-heredoc to avoid interpolation. | |
create_file 'app/views/shared/_navigation.html.haml' do <<-'HAML' | |
%li | |
= link_to 'Main', root_url(:host => request.domain) | |
- if request.subdomain.present? && request.subdomain != "www" | |
- if user_signed_in? | |
%li | |
= link_to('Edit account', edit_user_registration_url) | |
%li | |
= link_to('Logout', destroy_user_session_url, :method=>'delete') | |
- else | |
%li | |
= link_to('Login', new_user_session_url) | |
- else | |
%li | |
= link_to('Sign up', new_user_registration_url(:host => request.domain)) | |
- if user_signed_in? | |
%li | |
= link_to('Logout', destroy_user_session_url, :method=>'delete') | |
HAML | |
end | |
end | |
end | |
# >------------------------------[ RailsAdmin ]-------------------------------< | |
@current_recipe = "rails_admin" | |
@before_configs["rails_admin"].call if @before_configs["rails_admin"] | |
say_recipe 'RailsAdmin' | |
config = {} | |
config['ckeditor'] = yes_wizard?("Install CKEditor?") if true && true unless config.key?('ckeditor') | |
@configs[@current_recipe] = config | |
gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git' | |
after_bundler do | |
generate 'rails_admin:install' | |
end | |
# >-----------------------------[ Settingslogic ]-----------------------------< | |
@current_recipe = "settingslogic" | |
@before_configs["settingslogic"].call if @before_configs["settingslogic"] | |
say_recipe 'Settingslogic' | |
@configs[@current_recipe] = config | |
gem 'settingslogic' | |
say_wizard "Generating config/application.yml..." | |
append_file "config/application.rb", <<-RUBY | |
require 'settings' | |
RUBY | |
create_file "lib/settings.rb", <<-RUBY | |
class Settings < Settingslogic | |
source "#\{Rails.root\}/config/application.yml" | |
namespace Rails.env | |
end | |
RUBY | |
create_file "config/application.yml", <<-YAML | |
defaults: &defaults | |
cool: | |
saweet: nested settings | |
neat_setting: 24 | |
awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %> | |
development: | |
<<: *defaults | |
neat_setting: 800 | |
test: | |
<<: *defaults | |
production: | |
<<: *defaults | |
YAML | |
# >--------------------------[ Inherited Resources ]--------------------------< | |
@current_recipe = "inherited_resources" | |
@before_configs["inherited_resources"].call if @before_configs["inherited_resources"] | |
say_recipe 'Inherited Resources' | |
config = {} | |
config['inherited_resources'] = yes_wizard?("Install Inherited Resources?") if true && true unless config.key?('inherited_resources') | |
@configs[@current_recipe] = config | |
gem 'inherited_resources' | |
# >--------------------------------[ Cleanup ]--------------------------------< | |
@current_recipe = "cleanup" | |
@before_configs["cleanup"].call if @before_configs["cleanup"] | |
say_recipe 'Cleanup' | |
@configs[@current_recipe] = config | |
# Application template recipe for the rails_apps_composer. Check for a newer version here: | |
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/cleanup.rb | |
after_bundler do | |
say_wizard "Cleanup recipe running 'after bundler'" | |
# remove unnecessary files | |
%w{ | |
README | |
doc/README_FOR_APP | |
public/index.html | |
}.each { |file| remove_file file } | |
if recipes.include? 'rails 3.0' | |
%w{ | |
public/images/rails.png | |
}.each { |file| remove_file file } | |
else | |
%w{ | |
app/assets/images/rails.png | |
}.each { |file| remove_file file } | |
end | |
# add placeholder READMEs | |
get "https://raw.github.com/RailsApps/rails3-application-templates/master/files/sample_readme.txt", "README" | |
get "https://raw.github.com/RailsApps/rails3-application-templates/master/files/sample_readme.textile", "README.textile" | |
gsub_file "README", /App_Name/, "#{app_name.humanize.titleize}" | |
gsub_file "README.textile", /App_Name/, "#{app_name.humanize.titleize}" | |
# remove commented lines from Gemfile | |
# thanks to https://github.com/perfectline/template-bucket/blob/master/cleanup.rb | |
gsub_file "Gemfile", /#.*\n/, "\n" | |
gsub_file "Gemfile", /\n+/, "\n" | |
end | |
@current_recipe = nil | |
# >-----------------------------[ Run Bundler ]-------------------------------< | |
say_wizard "Running 'bundle install'. This will take a while." | |
run 'bundle install --path vendor/bundle' | |
say_wizard "Running 'after bundler' callbacks." | |
require 'bundler/setup' | |
@after_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call} | |
@current_recipe = nil | |
say_wizard "Running 'after everything' callbacks." | |
@after_everything_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call} | |
@current_recipe = nil | |
say_wizard "Finished running the rails_apps_composer app template." | |
say_wizard "Your new Rails app is ready." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment