Skip to content

Instantly share code, notes, and snippets.

@dirtyhenry
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirtyhenry/b05e6eee82ce22944f37 to your computer and use it in GitHub Desktop.
Save dirtyhenry/b05e6eee82ce22944f37 to your computer and use it in GitHub Desktop.
The Rails' Bootstrap
{
"directory": "vendor/assets/components"
}

The Rails' Bootstrap

Create the project

rails new myapp --database=postgresql

Gemfile Additions

gem 'devise'
gem 'rails_admin'
gem 'bower'
group :development, :test do
    gem 'rspec-rails', '~> 3.0'
    gem 'capybara'
    gem 'factory_girl_rails'
end

First steps

rm README.rdoc
touch README.md
bundle install
rails generate rspec:install
rails generate devise:install
# Do manual stuff recommended by devise gem
rails g devise:views
bin/rake db:create
rails generate devise Admin
# Remove the :registerable option from the model
bin/rake db:migrate
rails generate bower:install
bin/rake db:create RAILS_ENV=test
rspec
rm -rf test
# Then, at the end...
git add .bowerrc .gitignore .rspec Gemfile Gemfile.lock README.md Rakefile app/ bin bower.json config.ru config db lib log public/ spec vendor/
<%# lib/templates/erb/scaffold/_form.html.erb %>
<%%= form_for(@<%= singular_table_name %>, role: "form") do |f| %>
<%% if @<%= singular_table_name %>.errors.any? %>
<div class="alert alert-danger" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Fermer</span></button>
<strong><%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</strong>
<ul>
<%% @<%= singular_table_name %>.errors.full_messages.each do |message| %>
<li><%%= message %></li>
<%% end %>
</ul>
</div>
<%% end %>
<% attributes.each do |attribute| -%>
<div class="form-group">
<% if attribute.password_digest? -%>
<%%= f.label :password, class: "control-label" %>
<%%= f.password_field :password, class: "form-control" %>
</div>
<div class="field">
<%%= f.label :password_confirmation, class: "control-label" %>
<%%= f.password_field :password_confirmation, class: "form-control" %>
<% else -%>
<%%= f.label :<%= attribute.column_name %>, class: "control-label" %>
<%%= f.<%= attribute.field_type %> :<%= attribute.column_name %>, class: "form-control" %>
<% end -%>
</div>
<% end -%>
<%%= f.submit(nil, class: "btn btn-primary") %>
<%% end %>
# spec/models/admin_spec.rb
require 'rails_helper'
RSpec.describe Admin, :type => :model do
it "fails when creating an admin with invalid password confirmation" do
invalid_admin = Admin.create( { email: 'toto@tata.com', password: 'proute', password_confirmation: 'prouta' } )
expect(invalid_admin.persisted?).to eq(false)
end
end
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require bootstrap/dist/css/bootstrap.min
*= require components-font-awesome/css/font-awesome.min
*= require_tree .
*= require_self
*/
@import url(http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic);
@import url(http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic);
body {
padding-top: 1em;
font-family: 'Lora', serif;
}
h1, h2, h3, h4, h5, h6 {
font-family: 'PT Sans', sans-serif;
font-weight: bold;
}
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>MyApp</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<!-- Place favicon.ico in the root directory -->
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
</head>
<body>
<!--[if lt IE 8]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<div class="container">
<% if alert.present? %>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<%= alert %>
</div>
<% end %>
<% if notice.present? %>
<div class="alert alert-info alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<%= notice %>
</div>
<% end %>
<%= yield %>
</div>
</body>
</html>
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap/dist/js/bootstrap.min
//= require_tree .
{
"name": "myapp",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"vendor/assets/components",
"test",
"tests"
],
"dependencies": {
"bootstrap": "~3.3.1",
"components-font-awesome": "~4.2.0"
}
}
<h1>Editing <%= singular_table_name.titleize %></h1>
<%%= render 'form' %>
<%%= link_to 'Show', @<%= singular_table_name %> %> |
<%%= link_to 'Back', <%= index_helper %>_path %>
<h1>Listing <%= plural_table_name.titleize %></h1>
<table class="table table-hover">
<thead>
<tr>
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<th><%= attribute.human_name %></th>
<% end -%>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
<%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
<tr>
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end -%>
<td><%%= link_to 'Show', <%= singular_table_name %> %></td>
<td>
<%%= link_to edit_<%= singular_table_name %>_path(<%= singular_table_name %>) do %>
<i class="fa fa-pencil-square-o"></i>
<%% end %>
</td>
<td>
<%%= link_to <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' } do %>
<i class="fa fa-trash"></i>
<%% end %>
</td>
</tr>
<%% end %>
</tbody>
</table>
<br>
<%%= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path, class: 'btn btn-primary' %>
<h1>New <%= singular_table_name.titleize %></h1>
<%%= render 'form' %>
<%%= link_to 'Back', <%= index_helper %>_path %>
# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
config.main_app_name = Proc.new { |controller| [ "Cool app", "BackOffice - #{controller.params[:action].try(:titleize)}" ] }
config.authenticate_with do
warden.authenticate! scope: :admin
end
config.current_user_method(&:current_admin)
end
# spec/features/rails_admin_spec.rb
require 'rails_helper'
describe "the Rails Admin page", :type => :feature do
it "is restricted to Admins only" do
visit rails_admin_path
expect(page).to have_content "Log in"
end
end
mount RailsAdmin::Engine => '/data', as: 'rails_admin'
devise_for :admins
# db/seeds.rb
admins = Admin.create([{ email: 'bootstabot@gmail.com', password: 'bpC6+7buCmJxKe;MVP48wqat', password_confirmation: 'bpC6+7buCmJxKe;MVP48wqat'}])
admins.each do |admin|
puts "Created admin: #{admin.email} (success: #{admin.persisted?})"
end
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<p>
<strong><%= attribute.human_name %>:</strong>
<%%= @<%= singular_table_name %>.<%= attribute.name %> %>
</p>
<% end -%>
<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> |
<%%= link_to 'Back', <%= index_helper %>_path %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment