Created
September 17, 2012 21:09
-
-
Save jaigouk/3739797 to your computer and use it in GitHub Desktop.
rails template for ember.js, mongoid, omniauth. generated rails app is at https://github.com/jaigouk/rails-mongoid-omniauth-emberjs
This file contains 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 script is designed to be used with Ruby on Rails' new project generator: | |
# | |
# rails new my_app -m thisfile -T -O | |
# | |
# For more information about the template API, please see the following Rails | |
# guide: | |
# | |
# http://edgeguides.rubyonrails.org/rails_application_templates.html | |
# create rvmrc file | |
create_file ".rvmrc", "rvm 1.9.3" | |
run "rm public/index.html" | |
# Install required gems | |
gem "active_model_serializers" | |
gem_group :assets do | |
gem 'twitter-bootstrap-rails' | |
gem "ember-rails", github: "emberjs/ember-rails" | |
end | |
gem 'turbolinks' | |
gem "mongoid", ">= 3.0.5" | |
gem "omniauth", ">= 1.1.1" | |
gem "omniauth-google-oauth2" | |
gem "google-api-client" | |
gem "ruby-trello" | |
gem "rails_config" | |
gem "puma", ">= 1.6.3" | |
gem_group :development, :test do | |
gem 'guard' | |
gem 'jasminerice' | |
gem 'guard-jasmine' | |
gem "jasmine-stories" | |
gem 'rb-fsevent' | |
gem 'rr' | |
gem 'terminal-notifier-guard' | |
gem 'guard-minitest' | |
gem 'guard-shell' | |
gem 'minitest-rails' | |
gem 'minitest-rails-capybara' | |
gem "capybara_minitest_spec" | |
gem "minitest" | |
gem "mongoid-minitest" | |
gem "database_cleaner" | |
end | |
run "bundle install" | |
########################################### | |
# | |
# TWITTER BOOTSTRAP | |
# | |
########################################### | |
generate "bootstrap:install" | |
# userd at home | |
run "rm app/views/layouts/application.html.erb" | |
file 'app/views/layouts/application.html.erb', <<-CODE | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<!--[if lt IE 9]> | |
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js" type="text/javascript"></script> | |
<![endif]--> | |
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Lean Canvas" %>"> | |
<title><%= content_for?(:title) ? yield(:title) : "Lean Canvas" %></title> | |
<%= javascript_include_tag "home" %> | |
<%= stylesheet_link_tag "application", :media => "all" %> | |
<%= csrf_meta_tags %> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="navbar navbar-fixed-top"> | |
<div class="navbar-inner"> | |
<div class="container" style="width: auto;"> | |
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</a> | |
<a class="brand" href="#">Project name</a> | |
<div class="nav-collapse in collapse" style="height: auto; "> | |
<ul class="nav"> | |
<li class="active"><a href="#">Home</a></li> | |
<li><a href="#">About</a></li> | |
<li><a href="#">Contact</a></li> | |
<li><a href="/signin">Login</a></li> | |
</ul> | |
<ul class="nav pull-right"> | |
<li><a href="#">Link</a></li> | |
</ul> | |
</div><!-- /.nav-collapse --> | |
</div> | |
</div><!-- /navbar-inner --> | |
</div> | |
<div class="container"> | |
<!-- Main hero unit for a primary marketing message or call to action --> | |
<div class="hero-unit"> | |
<h1>Hello, world!</h1> | |
<p>This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique.</p> | |
<p><a class="btn btn-primary btn-large">Learn more »</a></p> | |
</div> | |
<!-- Example row of columns --> | |
<div class="row"> | |
<div class="span4"> | |
<h2>Heading</h2> | |
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p> | |
<p><a class="btn" href="#">View details »</a></p> | |
</div> | |
<div class="span4"> | |
<h2>Heading</h2> | |
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p> | |
<p><a class="btn" href="#">View details »</a></p> | |
</div> | |
<div class="span4"> | |
<h2>Heading</h2> | |
<p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p> | |
<p><a class="btn" href="#">View details »</a></p> | |
</div> | |
</div> | |
<%= yield %> | |
<hr> | |
<footer> | |
<p>© Company 2012</p> | |
</footer> | |
</div> <!-- /container --> | |
</div> | |
</body> | |
</html> | |
CODE | |
inject_into_file "app/assets/stylesheets/bootstrap_and_overrides.css.less", :after => '@import "twitter/bootstrap/bootstrap";' do | |
<<-eos | |
body{ padding-top: 60px; } | |
eos | |
end | |
generate "rails_config:install" | |
########################################### | |
# | |
# Mongoid / Controllers | |
# | |
########################################### | |
generate "mongoid:config" | |
inject_into_file "config/application.rb", :before => "require 'rails/all'" do | |
"# going to use mongoid" | |
end | |
inject_into_file "config/application.rb", :after => "require 'rails/all'" do | |
<<-eos | |
# for mongoid | |
require "action_controller/railtie" | |
require "action_mailer/railtie" | |
require "active_resource/railtie" | |
eos | |
end | |
generate :model, "user", "provider:string", "uid:string", "name:string", "email:string" | |
inject_into_file "app/models/user.rb", :after => "include Mongoid::Document" do | |
<<-eos | |
#prevent malicious hackers from creating fake web forms that would allow changing of attributes through the mass-assignment operations | |
attr_protected :provider, :uid, :name, :email | |
eos | |
end | |
file 'config/initializers/omniauth.rb', <<-CODE | |
Rails.application.config.middleware.use OmniAuth::Builder do | |
# provider :twitter, Settings.apis.twitter.consumer_key, Settings.apis.twitter.consumer_secret | |
provider :google_oauth2, Settings.apis.google.api_key, Settings.apis.google.secret_key, {access_type: 'online', approval_prompt: ''} | |
end | |
CODE | |
prepend_file 'config/settings.local.yml' do | |
<<-eos | |
apis: | |
google : | |
api_key: "xxxxx.apps.googleusercontent.com" | |
secret_key: "xxxxxx" | |
eos | |
end | |
file 'app/controllers/sessions_controller.rb', <<-CODE | |
class SessionsController < ApplicationController | |
def new | |
redirect_to "/auth/google_oauth2" | |
end | |
def create | |
auth = request.env["omniauth.auth"] | |
user = User.where(:provider => auth['provider'], | |
:uid => auth['uid'].to_s).first || User.create_with_omniauth(auth) | |
session[:user_id] = user.id | |
if user.email.blank? | |
redirect_to edit_user_path(user), :alert => "Please enter your email address." | |
else | |
redirect_to ember_url, :notice => 'Signed in!' | |
end | |
end | |
def destroy | |
reset_session | |
redirect_to root_url, :notice => 'Signed out!' | |
end | |
def failure | |
redirect_to root_url, :alert => 'Authentication error' | |
end | |
end | |
CODE | |
inject_into_file "app/models/user.rb", :before => "end" do | |
<<-eos | |
# saves a new user to the datastore | |
def self.create_with_omniauth(auth) | |
create! do |user| | |
user.provider = auth['provider'] | |
user.uid = auth['uid'] | |
if auth['info'] | |
user.name = auth['info']['name'] || "" | |
user.email = auth['info']['email'] || "" | |
end | |
end | |
end | |
eos | |
end | |
inject_into_file "app/controllers/application_controller.rb", :after => "protect_from_forgery | |
" do | |
<<-eos | |
# authentication helpers | |
helper_method :current_user | |
helper_method :user_signed_in? | |
helper_method :correct_user? | |
private | |
def current_user | |
begin | |
@current_user ||= User.find(session[:user_id]) if session[:user_id] | |
rescue Mongoid::Errors::DocumentNotFound | |
nil | |
end | |
end | |
def user_signed_in? | |
return true if current_user | |
end | |
def correct_user? | |
@user = User.find(params[:id]) | |
unless current_user == @user | |
redirect_to root_url, :alert => "Access denied." | |
end | |
end | |
def authenticate_user! | |
if !current_user | |
redirect_to root_url, :alert => 'You need to sign in for access to this page.' | |
end | |
end | |
eos | |
end | |
########################################### | |
# | |
# EMBER.JS | |
# | |
########################################### | |
# This needs to be done outside the bootstrap generator | |
# to avoid an initial "unknown variant" error. | |
environment <<-RUBY.strip_heredoc, :env => :development | |
config.ember.variant = :development | |
RUBY | |
environment <<-RUBY.strip_heredoc, :env => :test | |
config.ember.variant = :development | |
RUBY | |
environment <<-RUBY.strip_heredoc, :env => :production | |
config.ember.variant = :production | |
RUBY | |
# Configure the app to serve Ember.js and app assets from an AssetsController | |
generate "ember:bootstrap" | |
generate "ember:install", "--head" | |
generate :controller, "Assets", "index" | |
inject_into_file "app/controllers/assets_controller.rb", :after => '< ApplicationController' do | |
<<-eos | |
layout false | |
eos | |
end | |
run "rm app/views/assets/index.html.erb" | |
file 'app/views/assets/index.html.erb', <<-CODE | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<!--[if lt IE 9]> | |
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js" type="text/javascript"></script> | |
<![endif]--> | |
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Lean Canvas" %>"> | |
<title><%= content_for?(:title) ? yield(:title) : "Lean Canvas" %></title> | |
<%= stylesheet_link_tag "application", :media => "all" %> | |
<%= csrf_meta_tags %> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="navbar navbar-fixed-top"> | |
<div class="navbar-inner"> | |
<div class="container" style="width: auto;"> | |
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</a> | |
<a class="brand" href="#">Project name</a> | |
<div class="nav-collapse in collapse" style="height: auto; "> | |
<ul class="nav"> | |
<li class="active"><a href="#">Home</a></li> | |
<li><a href="#">Link</a></li> | |
<li><a href="#">Link</a></li> | |
<li><a href="#">Link</a></li> | |
</ul> | |
<ul class="nav pull-right"> | |
<li><a href="#">Link</a></li> | |
</ul> | |
</div><!-- /.nav-collapse --> | |
</div> | |
</div><!-- /navbar-inner --> | |
</div> | |
<%= javascript_include_tag "application" %> | |
</div> | |
</body> | |
</html> | |
CODE | |
remove_file 'app/assets/javascripts/templates/application.handlebars' | |
file 'app/assets/javascripts/templates/application.handlebars', <<-CODE | |
<div style="width: 600px; border: 6px solid #eee; margin: 0 auto; padding: 20px; text-align: center; font-family: sans-serif;"> | |
<img src="http://emberjs.com/images/about/ember-productivity-sm.png" style="display: block; margin: 0 auto;"> | |
<h1>Welcome to Ember.js!</h1> | |
<p>You're running an Ember.js app on top of Ruby on Rails. To get started, replace this content | |
(inside <code>app/assets/javascripts/templates/application.handlebars</code>) with your application's | |
HTML.</p> | |
</div> | |
CODE | |
# Generate a default serializer that is compatible with ember-data | |
generate :serializer, "application", "--parent", "ActiveModel::Serializer" | |
inject_into_class "app/serializers/application_serializer.rb", 'ApplicationSerializer' do | |
" embed :ids, :include => true\n" | |
end | |
run "wget -O app/assets/javascripts/ember-bootstrap-0.0.1.js http://cloud.github.com/downloads/emberjs-addons/ember-bootstrap/ember-bootstrap-0.0.1.js" | |
inject_into_file "app/assets/javascripts/application.js", :after => 'require ember-data' do | |
<<-eos | |
//= require ember-bootstrap-0.0.1 | |
eos | |
end | |
########################################### | |
# | |
# Guard | |
# | |
########################################### | |
run "bundle exec guard init jasmine" | |
run "mkdir -p test/javascripts" | |
run "echo '#=require application\n#=require_tree ./' > test/javascripts/spec.js.coffee" | |
run "echo '/*\n *=require application\n */' > test/javascripts/spec.css" | |
run "mkdir -p test/javascripts/stories" | |
run "echo '#=require_tree ./stories' > test/javascripts/stories.js.coffee" | |
run "echo '/*\n *=require application\n */' > test/javascripts/spec.css" | |
prepend_file "Guardfile" do | |
<<-eos | |
ignore %r{^ignored/path/}, /public/ | |
filter /\.txt$/, /.*\.zip/ | |
guard 'minitest' do | |
watch(%r|^test/test_(.*)\.rb|) | |
watch(%r|^test/(.*)_test\.rb|) | |
watch(%r|^test/spec_(.*)\.rb|) | |
watch(%r|^test/(.*)_spec\.rb|) | |
watch(%r|^test/test_helper\.rb|){ "test" } | |
end | |
eos | |
end | |
inject_into_file "Guardfile", :after => ":jasmine " do | |
", :jasmine_url => 'http://127.0.0.1:8888/jasmine-stories'" | |
end | |
file 'test/test_helper.rb', <<-CODE | |
ENV['RAILS_ENV'] ||= 'test' | |
gem 'minitest' # demand gem version | |
require 'minitest/autorun' | |
require 'rr' | |
require 'ostruct' | |
# $: << File.expand_path('../lib', File.dirname(__FILE__)) | |
class MiniTest::Unit::TestCase | |
include RR::Adapters::MiniTest | |
end | |
def stub_module(full_name) | |
full_name.to_s.split(/::/).inject(Object) do |context, name| | |
begin | |
context.const_get(name) | |
rescue NameError | |
context.const_set(name, Module.new) | |
end | |
end | |
end | |
CODE | |
########################################### | |
# | |
# Routes / etc | |
# | |
########################################### | |
file 'app/assets/javascripts/home.js', <<-CODE | |
// | |
//= require jquery | |
//= require jquery_ujs | |
//= require twitter/bootstrap | |
//= require_self | |
CODE | |
generate :controller, "Home", "index" | |
route "root :to => 'home#index'" | |
inject_into_file "config/routes.rb", :before => ' get "home/index"'do | |
"#root " | |
end | |
inject_into_file "config/routes.rb", :after => 'get "assets/index"' do | |
", :as => :ember" | |
end | |
inject_into_file "config/routes.rb", :before => " # Sample of regular route:" do | |
<<-eos | |
resources :users, :only => [:index, :show, :edit, :update ] | |
# omniauth routes | |
match '/auth/:provider/callback' => 'sessions#create' | |
match '/auth/failure' => 'sessions#failure' | |
match '/signout' => 'sessions#destroy', :as => :signout | |
match '/signin' => 'sessions#new', :as => :signin | |
eos | |
end | |
remove_file 'README.rdoc' | |
file 'README.md', <<-CODE | |
![Ember.js](http://emberjs.com/images/about/ember-productivity-sm.png "Ember.js logo") | |
Rails App for Omniauth with Mongoid and Ember.js | |
================== | |
## Project Dependencies | |
* [Ember.js](http://emberjs.com/) | |
* [Ember Bootstrap](http://emberjs-addons.github.com/ember-bootstrap/) | |
* Omniauth, Mongoid, Rails 3.2.8 | |
### One line command for generating this rails structure | |
The template file is at https://gist.github.com/3739797 | |
rails new my_app -m template_file -T -O | |
CODE | |
# commit to git | |
git :init | |
git :add => "." | |
git :commit => "-a -m 'create initial application'" | |
say <<-eos | |
============================================================================ | |
Your new Rails application is ready to go. | |
Don't forget to scroll up for important messages from installed generators. | |
eos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment