Created
October 18, 2010 00:45
-
-
Save alexcrichton/631511 to your computer and use it in GitHub Desktop.
Basic new rails application 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
<% if target.errors.any? %> | |
<div id="errorExplanation"> | |
<h2> | |
<%= pluralize(target.errors.count, 'error') %> | |
prohibited this record from being saved: | |
</h2> | |
<ul> | |
<% target.errors.full_messages.each do |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> |
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
.field_with_errors { | |
padding: 2px; | |
margin: 2px; | |
input, textarea { | |
background: #ffdfdf; | |
} | |
} | |
#errorExplanation { | |
border: 2px solid #a00; | |
padding: 7px; | |
padding-bottom: 3px; | |
margin-bottom: 20px; | |
background-color: #f0f0f0; | |
overflow: auto; | |
h2 { | |
text-align: left; | |
font-weight: bold; | |
padding: 5px 5px 5px 15px; | |
font-size: 12px; | |
margin: -7px; | |
background-color: #c00; | |
color: #fff; | |
} | |
p { | |
color: #333; | |
margin-top: 10px; | |
margin-bottom: 0; | |
padding: 5px; | |
} | |
ul li { | |
font-size: 12px; | |
list-style: square; | |
} | |
} |
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
class Ability | |
include CanCan::Ability | |
def initializer user | |
return if user.nil? | |
can :manage, :all | |
end | |
end |
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
(function(){ | |
<%= yield %> | |
})(); |
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
@import "blueprint/typography"; | |
@import "compass/css3"; | |
@import "errors"; | |
body { | |
@include blueprint-typography-body(12px); | |
} |
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
class AuthenticationsController < ApplicationController | |
def create | |
omniauth = request.env['omniauth.auth'] | |
authentication = Authentication.find_by_provider_and_uid( | |
omniauth['provider'], omniauth['uid']) | |
if authentication | |
flash[:notice] = 'Signed in successfully.' | |
sign_in_and_redirect(:user, authentication.user) | |
elsif current_user | |
current_user.authentications.create!( | |
:provider => omniauth['provider'], :uid => omniauth['uid']) | |
flash[:notice] = "Authentication successful." | |
redirect_to authentications_url | |
else | |
user = User.new | |
user.apply_omniauth(omniauth) | |
user.save! | |
flash[:notice] = 'Signed in successfully.' | |
sign_in_and_redirect(:user, user) | |
end | |
end | |
end |
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
remove_file 'README' | |
remove_file 'public/index.html' | |
remove_file 'public/favicon.ico' | |
remove_file 'public/images/rails.png' | |
remove_file 'public/javascripts/*.js' | |
remove_dir 'doc' | |
remove_dir 'test' | |
create_file 'public/javascripts/.gitkeep' | |
run 'cp config/database.yml config/database.yml.example' | |
gem 'paste', :git => 'git://github.com/alexcrichton/paste.git' | |
gem 'compass' | |
gem 'cancan' | |
get 'http://code.jquery.com/jquery-latest.min.js', 'app/javascripts/jquery.js' | |
get 'http://www.ajaxload.info/download.php?img=cache/FF/FF/FF/00/00/00/2-0.gif', | |
'public/images/ajax-small.gif' | |
get 'http://github.com/rails/jquery-ujs/raw/master/src/rails.js', | |
'app/javascripts/rails.js' | |
append_file '.gitignore' do | |
"public/javascripts/*\n" + | |
"public/stylesheets/*\n" + | |
"config/database.yml" | |
end | |
gsub_file 'app/views/layouts/application.html.erb', /stylesheet.*/, | |
'stylesheet_tags %>' | |
gsub_file 'app/views/layouts/application.html.erb', /javascript.*/, | |
'javascript_tags %>' | |
source_paths << File.expand_path('../templates', __FILE__) | |
copy_file 'application.js.erb', 'app/views/layouts/application.js.erb' | |
copy_file 'ability.rb', 'app/models/ability.rb' | |
copy_file 'application.scss', 'app/stylesheets/application.scss' | |
copy_file '_errors.scss', 'app/stylesheets/_errors.scss' | |
copy_file '_error_messages.html.erb', | |
'app/views/shared/_error_messages.html.erb' | |
file '.rvmrc', "rvm 1.9.2@#{app_name}" | |
def rvm cmd | |
run cmd, :with => "rvm 1.9.2@#{app_name} exec" | |
end | |
in_root do | |
rvm "bundle install --without production" | |
rvm "compass config --sass-dir app/stylesheets --app rails " + | |
'--css-dir public/stylesheets --images-dir public/images ' + | |
'--javascripts-dir public/javascripts' | |
append_file 'config/compass.rb', | |
'output_style = :compressed if Rails.env.production?' | |
end | |
inject_into_file 'config/environments/development.rb', | |
"\n config.action_mailer.default_url_options = " + | |
"{ :host => 'localhost:3000' }\n", | |
:after => "config.action_dispatch.best_standards_support = :builtin\n" | |
data = <<-FILE | |
rescue_from CanCan::AccessDenied do |e| | |
redirect_to new_user_session_path | |
end | |
def current_ability | |
@current_ability ||= Ability.new current_user | |
end | |
FILE | |
inject_into_file 'app/controllers/application_controller.rb', data, | |
:after => "protect_from_forgery\n" | |
git :init | |
git :add => '.' | |
git :commit => '-a -m "Initial commit"' |
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
source_paths << File.dirname(__FILE__) | |
gem 'devise' | |
apply 'base.rb' | |
rvm 'rails g devise:install' | |
rvm 'rails g devise user' | |
rvm 'rake db:migrate' | |
git :commit => '-a -m "Adding devise scaffolding"' |
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
source_paths << File.dirname(__FILE__) | |
gem 'oa-oauth', :require => 'omniauth/oauth' | |
initializer 'omniauth.rb', <<-FILE | |
Rails.application.config.middleware.use OmniAuth::Builder do | |
provider :facebook, ENV['API_KEY'], ENV['APP_SECRET'] | |
end | |
FILE | |
apply 'devise.rb' | |
rvm 'rails g model authentication user_id:integer provider:string uid:string' | |
route "match 'auth/:provider/callback' => 'authentications#create'" | |
data = <<-FILE | |
has_many :authentications | |
def apply_omniauth(omniauth) | |
if email.blank? | |
self.email = omniauth['user_info']['email'] || | |
omniauth['extra']['user_hash']['email'] | |
end | |
if name.blank? | |
self.name = omniauth['user_info']['first_name'] + ' ' + | |
omniauth['user_info']['last_name'] | |
end | |
authentications.build( | |
:provider => omniauth['provider'], | |
:uid => omniauth['uid'] | |
) | |
end | |
def password_required? | |
(authentications.empty? || !password.blank?) && super | |
end | |
FILE | |
inject_into_file 'app/models/user.rb', data, :after => /attr_access.*\n/ | |
data = <<-FILE | |
belongs_to :user | |
validates_presence_of :provider, :uid | |
FILE | |
inject_into_class 'app/models/authentication.rb', data, 'Authentication' | |
copy_file 'facebook/authentications_controller.rb', | |
'app/controllers/authentications_controller.rb' |
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
source_paths << File.dirname(__FILE__) | |
gem 'pg', :group => :production | |
gsub_file 'Gemfile', /.*sqlite.*/, '' | |
append_file 'Gemfile' do | |
"group :development do\n" + | |
" gem 'heroku'\n" + | |
" gem 'sqlite3-ruby', :require => 'sqlite3'\n" + | |
"end\n" | |
end | |
apply 'base.rb' | |
gsub_file 'config/environments/production.rb', | |
/config.serve_static_assets = false/, | |
'config.serve_static_assets = true' | |
data = <<-FILE | |
Paste.config.serve_assets = true | |
Paste.config.no_cache = true | |
ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false | |
config.app_middleware.insert_before Rack::Runtime, | |
::Rack::Static, | |
:urls => ['/stylesheets'], | |
:root => Rails.root.join('tmp').to_s | |
FILE | |
inject_into_file 'config/environments/production.rb', data, | |
:after => 'config.active_support.deprecation = :notify' | |
data = <<-FILE | |
if Rails.env.production? | |
output_style = :compressed | |
css_dir = "tmp/stylesheets" | |
end | |
FILE | |
gsub_file 'config/compass.rb', /output_style = :compressed.*/, data | |
git :commit => '-a -m "Adding heroku configuration"' |
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
class User < ActiveRecord::Base | |
# Include default devise modules. Others available are: | |
# :token_authenticatable, :confirmable, :lockable and :timeoutable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
# Setup accessible (or protected) attributes for your model | |
attr_accessible :email, :password, :password_confirmation, :remember_me | |
has_many :authentications | |
def apply_omniauth(omniauth) | |
if email.blank? | |
self.email = omniauth['user_info']['email'] || | |
omniauth['extra']['user_hash']['email'] | |
end | |
if name.blank? | |
self.name = omniauth['user_info']['first_name'] + ' ' + | |
omniauth['user_info']['last_name'] | |
end | |
authentications.build( | |
:provider => omniauth['provider'], | |
:uid => omniauth['uid'] | |
) | |
end | |
def password_required? | |
(authentications.empty? || !password.blank?) && super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment