public
Last active

rails template for ember.js, mongoid, omniauth. generated rails app is at https://github.com/jaigouk/rails-mongoid-omniauth-emberjs

  • Download Gist
edge_template.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
# 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 &raquo;</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 &raquo;</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 &raquo;</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 &raquo;</a></p>
</div>
</div>
<%= yield %>
<hr>
 
<footer>
<p>&copy; 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.