Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Forked from paulmars/template.rb
Created May 25, 2011 18:20
Show Gist options
  • Save davidrichards/991544 to your computer and use it in GitHub Desktop.
Save davidrichards/991544 to your computer and use it in GitHub Desktop.
Rails 3, RSpec, Factory_Girl, HAML, SASS, Devise, JQuery, Backbone.js, jammit, haml.js
## Rails App Template
## Useful for Rails 3.0.x and Ruby 1.9.2
## Run using $ rails new [appname] -JT -m tpl-basicapp.rb
# ========
# = Gems =
# ========
# pre-install spork, dydram and elastic_searchable
run "gem install spork -v 0.9.0.rc --pre"
run "gem install dydra elastic_searchable"
# Warden and Devise for security
# gem 'warden', '1.0.5'
# gem 'devise', '1.4.4'
gem 'warden'
gem 'devise'
# HAML and SASS for Templating
gem 'sass'
gem 'haml'
gem 'haml-rails'
# Barista for CoffeeScript
gem 'barista'
# Because I'm addicted to the debugger:
gem "ruby-debug19"
# Testing
gem "rspec-rails", ">= 2.5.0", :group => [:test]
gem "factory_girl_rails", :group => [:test]
gem "spork", :group => [:test]
gem "jasmine", :group => [:test]
gem "faker", :group => [:test]
gem "autotest", :group => [:test]
gem "autotest-fsevent", :group => [:test]
gem "autotest-growl", :group => [:test]
# Development
gem 'rails3-generators', '0.17.4', :group => [:development]
# Test and Development
gem "jasmine-headless-webkit", :group => [:test, :development]
gem "guard", :group => [:test, :development]
gem "guard-coffeescript", :group => [:test, :development]
gem "guard-jasmine-headless-webkit", :group => [:test, :development]
gem 'growl_notify', :group => [:test, :development]
gem 'rb-fsevent', :require => false, :group => [:test, :development]
# all
gem "jammit"
inject_into_file 'Gemfile', :after => %q[gem "jammit"] do
%q{
gem "dydra"
gem "elastic_searchable"
}
end
## Generators
inject_into_file('config/application.rb', :after => "config.filter_parameters += [:password]") do
%q{
config.generators do |g|
g.stylesheets false
g.template_engine :haml
g.test_framework :rspec, :fixture => true, :views => false
g.fixture_replacement :factory_girl, :dir => "spec/factories"
end
# Global Sass Option
Sass::Plugin.options[:template_location] = { 'app/stylesheets' => 'public/stylesheets' }
}
end
# Latest jQuery UJS
# get "https://github.com/rails/jquery-ujs/raw/master/src/rails.js", "public/javascripts/vendor/rails.js"
# HACK: Since the get method hates https and redirects
# jquery = open("https://github.com/rails/jquery-ujs/raw/master/src/rails.js", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
# create_file "public/javascripts/vendor/rails.js", jquery
# Replace the blank one with jQuery served via Google CDN
# gsub_file 'config/application.rb', 'config.action_view.javascript_expansions[:defaults] = %w()', 'config.action_view.javascript_expansions[:defaults] = %w(http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js rails.js)'
## Devise routes
routes = <<-ROUTES
devise_scope :user do
get "signup", :to => "devise/registrations#new"
get "login", :to => "devise/sessions#new"
get "logout", :to => "devise/sessions#destroy"
end
resources :welcome
ROUTES
route routes
# Clear the default index
remove_file "public/index.html"
remove_file "public/images/rails.png"
# Make the SASS directory and base file
empty_directory "app/stylesheets"
default_style = <<-LAYOUT
body
text-align: left
font-size: 12px
a, a:hover, a:visited
color: blue
.horizontal-list li
display: inline
.horizontal-list li a
padding: 0.1em
#hd h1
font-size: 1.5em
.login-bar
float: right
LAYOUT
create_file "app/stylesheets/application.sass", default_style
## Layout
layout = <<-LAYOUT
!!! 5
%html{:lang => "en"}
%head
%meta{:charset => "utf-8"}/
%title Stella
/[if IE]
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
= include_stylesheets :app, :media => 'all'
= include_javascripts :app
- unless Jammit.package_assets
= javascript_tag(Jammit.packager.pack_templates(:templates_js))
= csrf_meta_tag
%body
- yield_body = yield(:body)
- if yield_body.blank?
= yield
- else
= yield_body
%footer
LAYOUT
remove_file "app/views/layouts/application.html.erb"
create_file "app/views/layouts/application.html.haml", layout
## Backbone Directory Structure
empty_directory "public/javascripts/vendor"
empty_directory "app/coffeescripts/models"
empty_directory "app/coffeescripts/collections"
empty_directory "app/coffeescripts/routers"
empty_directory "app/coffeescripts/views"
## welcome router
welcome_router = <<-WELCOME
window.App.Routers.Welcome = Backbone.Router.extend
routes:
"": "index"
index: ->
new window.App.Views.Welcome()
WELCOME
create_file "app/coffeescripts/routers/Welcome.coffee", welcome_router
## welcome view
welcome_view = <<-WELCOME
window.App.Views.Index = Backbone.View.extend
el: '#content'
initialize: ->
_.bindAll this, 'render'
@render()
render: ->
$(@el).html JST['welcome/index']()
@
WELCOME
create_file "app/coffeescripts/views/welcome/Index.coffee", welcome_view
## welcome template
welcome_template = <<-WELCOME
Welcome to this new application. We are so proud of this application. Look around and see what you can find. Soon, this page will be replaced with work that is worthwhile and exciting. In the meantime, welcome, welcome, welcome.
WELCOME
create_file "app/views/welcome/index.jst.haml", welcome_template
## application.js
application_js = <<-JS
window.App =
Models: {}
Routers: {}
Collections: {}
Views:
Welcome: {}
init: ->
new window.App.Routers.Welcome()
Backbone.history.start()
$(->
window.App.init()
)
JS
remove_file "public/javascripts/application.js"
create_file "app/coffeescripts/application.coffee", application_js
# Javascript Assets
get "http://documentcloud.github.com/underscore/underscore-min.js", "public/javascripts/vendor/underscore-min.js"
get "http://documentcloud.github.com/backbone/backbone-min.js", "public/javascripts/vendor/backbone-min.js"
# HACK again
haml_js = open("https://raw.github.com/creationix/haml-js/master/lib/haml.js", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
create_file "public/javascripts/vendor/haml.js", haml_js
## Jammit assets
assets = <<-ASSETS
package_assets: off
template_extension: jst.haml
template_function: Haml
javascripts:
app:
- public/javascripts/vendor/jquery.js
- public/javascripts/vendor/underscore-min.js
- public/javascripts/vendor/backbone-min.js
- public/javascripts/application.js
- public/javascripts/vendor/haml.js
- public/javascripts/models/*.js
- public/javascripts/**/*.js
- app/views/**/*.jst.haml
templates_js:
- app/views/**/*.jst.haml
stylesheets:
app:
- public/stylesheets/style.css
ASSETS
remove_file "config/assets.yml"
create_file "config/assets.yml", assets
## Git
gitignore = <<-END
.bundle
.DS_Store
db/*.sqlite3
log/*.log
tmp/**/*
public/stylesheets/*
.rvmrc
END
# Re-Make gitignore
remove_file ".gitignore"
create_file ".gitignore", gitignore
run "bundle install"
run "rake db:migrate"
# Run all the generators
generate "rspec:install"
generate "devise:install"
generate "devise:views"
generate "devise User"
# Setup a basic Welcome Controller as the default route
generate "controller Welcome index"
inject_into_file('config/routes.rb', :after => %[root :to => "welcome#index"]) do
%q{
root :to => "welcome#index"
}
end
git :init
git :add => "."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment