Skip to content

Instantly share code, notes, and snippets.

@jonathannelson
Created April 21, 2010 01:11
Show Gist options
  • Save jonathannelson/373297 to your computer and use it in GitHub Desktop.
Save jonathannelson/373297 to your computer and use it in GitHub Desktop.
## Rails 3.0 Template
# Usage: rails app_name -m http://gist.github.com/373297.txt
# Remove normal files we don't want
%w(README public/index.html public/favicon.ico public/robots.txt).each do |f|
remove_file f
end
# Copy database.yml to sample
inside "config" do
run "cp database.yml database.yml.sample"
end
# Add standard ignores
append_file ".gitignore", <<-EOF
config/database.yml
doc/*
EOF
# Add default gems
gem "inherited_resources", ">=1.1.0"
gem "rails3-generators", :group => :dev
gem "shoulda", :group => :test
gem "factory_girl", :group => :test
## FormError overrides
initializer "form_errors.rb" do
<<-EOF
# ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
# msg = instance.error_message
# title = msg.kind_of?(Array) ? '* ' + msg.join("\\n* ") : msg
# "<span class=\"fieldWithErrors\" title=\"\#{title}\">\#{html_tag}</span>"
# end
#
# http://dev.rubyonrails.org/ticket/7425
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
error_class = "errors"
if html_tag =~ /<(input|textarea|select)[^>]+class=/
class_attribute = html_tag =~ /class=['"]/
html_tag.insert(class_attribute + 7, "\#{error_class} ")
elsif html_tag =~ /<(input|textarea|select)/
first_whitespace = html_tag =~ /\s/
html_tag[first_whitespace] = " class='\#{error_class}' "
end
html_tag
end
EOF
end
## JQuery Setup
# Remove Prototype Javascripts
Dir['public/javascripts/*.js'].each do |js|
remove_file(js) unless File.basename(js) == 'application.js'
end
# Add config/initializer to setup jQuery files
initializer "jquery.rb" do
<<-EOF
javascript_defaults = %w[jquery jquery.livequery jquery.metadata rails]
javascript_defaults.map! { |js| "\#{js}.min.js" } if Rails.env.production?
ActionView::Helpers::AssetTagHelper.register_javascript_expansion(:defaults => javascript_defaults)
EOF
end
# Fetch and setup the jQuery files
inside "public/javascripts" do
get "http://code.jquery.com/jquery-latest.js", "jquery.js"
get "http://github.com/brandonaaron/livequery/raw/master/jquery.livequery.js", "jquery.livequery.js"
get "http://github.com/lawrencepit/jquery.metadata.js/raw/master/jquery.metadata.js", "jquery.metadata.js"
get "http://github.com/rails/jquery-ujs/raw/master/src/rails.js", "rails.js"
append_file "application.js", <<-EOF
// Behaviours
jQuery(document).ready(function($) {
// Automatically add a "close" link to each flash
$("ul#flashes li").livequery(function() {
el = document.createElement('span');
dismiss = setTimeout(function() {
jQuery(el).click();
}, 10000); // 10 seconds
jQuery(el)
.text("close")
.click(function() {
clearTimeout(dismiss);
slideElement = jQuery(this).parent();
if (slideElement.parent().children().length == 1) {
slideElement = slideElement.parent();
}
slideElement.slideUp("normal", function() {
jQuery(this).remove();
});
})
.appendTo(this);
});
});
EOF
end
## HAML Setup
gem "haml"
# Initialize HAML
in_root { run "haml --rails ." }
# Setup directory for SCSS files
empty_directory('app/stylesheets')
# Create starting SCSS file
file "app/stylesheets/screen.scss", ""
# Set our SASS defaults
initializer "sass.rb" do
<<-EOF
Sass::Plugin.options = {
:css_location => Rails.root + '/public/stylesheets',
:cache_location => Rails.root + '/tmp/sass-cache',
:template_location => Rails.root + '/app/stylesheets'
}
EOF
end
# Create our application layout
file "app/views/layouts/application.html.haml", <<-HAML
!!! 5
%html
%head
%title #{app_const_base}
%meta{"http-equiv" => "Content-Type", :content => "text/html; charset=utf-8"}
= csrf_meta_tag
= javascript_include_tag :defaults
%body{:id => controller_name}
- if notice
%p.notice= notice
= yield
HAML
# Add our helper for handling flashes
file "app/helpers/flash_helper.rb", <<-RB
module FlashHelper
def yield_flashes
return if flash.empty?
haml_tag(:ul, :id => 'flashes') do
flash.each do |key, message|
haml_tag(:li, message, :class => key)
end
end
end
end
RB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment