Skip to content

Instantly share code, notes, and snippets.

@AhmedNadar
Forked from fanix/template.rb
Created February 21, 2014 16:33
Show Gist options
  • Save AhmedNadar/9137585 to your computer and use it in GitHub Desktop.
Save AhmedNadar/9137585 to your computer and use it in GitHub Desktop.
# coding: utf-8
# This is a Rails init template
# Usage
#
# $ rails new app_name -d postgresql -m https://raw.github.com/gist/3303948 --skip-bundle
#
# remove files
run "rm README.rdoc"
run "touch README.md"
run "rm public/index.html"
run "rm public/favicon.ico"
#run "rm public/images/rails.png"
run "cp config/database.yml config/database.yml.example"
# Gemfile
run "rm Gemfile"
get "https://raw.github.com/gist/3304095", "Gemfile"
run "bundle install"
# Setup Git
# git :init
run "rm .gitignore"
get 'https://raw.github.com/gist/3304102', '.gitignore'
# Keep tmp and log directory
run "touch tmp/.gitkeep"
run "touch log/.gitkeep"
### Bootstrap
# Assets files
run "rm app/assets/javascripts/application.js"
run "touch app/assets/javascripts/application.coffee"
append_file "app/assets/javascripts/application.coffee", <<-STR
# Include all twitter's javascripts
#= require jquery
#= require jquery_ujs
#= require twitter/bootstrap
$(document).ready ->
$('.dropdown-toggle').dropdown()
STR
run "rm app/assets/stylesheets/application.css"
run "touch app/assets/stylesheets/application.scss"
append_file "app/assets/stylesheets/application.scss", <<-STR
/*
*= require app_bootstrap
*/
body { padding-top: 60px; padding-bottom: 40px }
STR
run "touch app/assets/stylesheets/app_bootstrap.css.scss"
append_file "app/assets/stylesheets/app_bootstrap.css.scss", <<-STR
// change colors
$linkColor: red;
// change grid
$gridColumnWidth: 70px;
$gridGutterWidth: 10px;
// import original bootstrap
@import "twitter/bootstrap";
@import "twitter/bootstrap-responsive";
STR
# rails flash message settings
run "touch app/views/layouts/_flash_messages.html.erb"
run "rm app/helpers/application_helper.rb"
run "touch app/helpers/application_helper.rb"
append_file "app/views/layouts/_flash_messages.html.erb", <<-CODE
<div>
<% flash.each do |key, value| %>
<div class="<%= flash_class(key) %> fade in">
<a href="#" data-dismiss="alert" class="close">×</a>
<%= value %>
</div>
<% end %>
</div>
CODE
append_file "app/helpers/application_helper.rb", <<-CODE
module ApplicationHelper
def flash_class(level)
case level
when :notice then "alert alert-info"
when :success then "alert alert-success"
when :error then "alert alert-error"
when :alert then "alert alert-error"
end
end
end
CODE
#layouts set
run "rm app/views/layouts/application.html.erb"
get "https://raw.github.com/gist/3341328", "app/views/layouts/application.html.erb"
# Simple Form
generate("simple_form:install --bootstrap")
#run "config/initializers/simple_form.rb"
#get "https://raw.github.com/gist/2167880", "config/initializers/simple_form.rb"
# SettingsLogic
run "touch config/setting.yml"
append_file 'config/setting.yml', <<-STR
defaults: &defaults
app_name: "#{app_name.humanize.titleize}"
footer_html: "&copy; #{app_name.humanize.titleize}."
domain: "127.0.0.1:3000"
app_key: ""
app_secret: ""
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
STR
run "cp config/setting.yml config/setting.yml.default"
run "touch app/models/setting.rb"
append_file "app/models/setting.rb", <<-STR
# coding: utf-8
class Setting < Settingslogic
source "\#{Rails.root}/config/setting.yml"
namespace Rails.env
end
STR
###
### Auth
# generate authentication db migration
generate "model User uid name nickname token secret"
generate "model Authentication provider uid:integer user_id:integer"
generate "controller sessions create destroy"
# add routes for auth
route "root :to => 'home#index'"
route "match '/auth/:provider/callback', :to => 'sessions#create'"
route "match '/logout' => 'sessions#destroy', :as => :logout"
# get key and secret for sina weibo
#if yes?("Do you want to set sina weibo app key and app secret ?")
# @app_key = ask("Enter App Key:")
# @app_secret = ask("Enter App Secret:")
#end
create_file "config/initializers/omniauth.rb", <<-Code
Rails.application.config.middleware.use OmniAuth::Builder do
provider :weibo, Setting.app_key, Setting.app_secret
provider :developer unless Rails.env.production?
end
Code
gsub_file "app/controllers/application_controller.rb", /protect_from_forgery/, <<-Code
protect_from_forgery
helper_method :current_user, :check_signed_in
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
Code
gsub_file "app/controllers/sessions_controller.rb", /def create/, <<-Code
def create
auth = request.env["omniauth.auth"]
user = User.find_by_uid(auth["uid"].to_s) || User.create_with_omniauth(auth)
user.token = auth["credentials"]["token"]
if user.save
session[:user_id] = user.id
redirect_to "/", :notice => "Signed in!"
else
redirect_to "/", :notice => "Signed wrong!"
end
Code
gsub_file "app/controllers/sessions_controller.rb", /def destroy/, <<-Code
def destroy
session[:user_id] = nil
session[:reteet] = nil
redirect_to "/", :notice => "Signed out!"
Code
gsub_file "app/models/user.rb", /end/, <<-Code
validates :nickname, :presence => true
def self.create_with_omniauth(auth)
create! do |user|
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.nickname = auth["info"]["nickname"]
user.token = auth["credentials"]["token"]
user.secret = auth["credentials"]["secret"]
end
end
end
Code
gsub_file "app/models/authentication.rb", /end/, <<-Code
validates :provider, :presence => true, :uniqueness => {:scope => :user_id}
validates :uid, :presence => true, :uniqueness => {:scope => :provider}
belongs_to :user
end
Code
# cancan define
generate "cancan:ability"
###
# HomeController
generate("controller home index")
# git commit
git :init
git :add => '.'
git :add => 'tmp/.gitkeep -f'
git :add => 'log/.gitkeep -f'
git :commit => "-a -m 'initial commit'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment