Skip to content

Instantly share code, notes, and snippets.

View monkbroc's full-sized avatar

Julien Vanier monkbroc

View GitHub Profile
@monkbroc
monkbroc / ugly_env_vars.rb
Last active August 29, 2015 14:13
Environment Variables
if ENV['ENABLE_GC_PROFILING'] == 'yes'
GC::Profiler.enable
end
if Nenv.enable_gc_profiling?
GC::Profiler.enable
end
# Replace this
Pusher.app_id = ENV['PUSHER_APP_ID']
Pusher.key = ENV['PUSHER_KEY']
Pusher.secret = ENV['PUSHER_SECRET']
# By this
Nenv :pusher do |p|
Pusher.app_id = p.app_id
Pusher.key = p.key
Pusher.secret = p.secret
@monkbroc
monkbroc / devise_create_users.rb
Created February 18, 2015 16:23
OmniAuth migration devise_create_users.rb
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Your your user fields
t.string :email, default: ""
## OmniAuth-able
t.string :provider
t.string :uid
@monkbroc
monkbroc / devise.rb
Last active August 29, 2015 14:15
OmniAuth devise initializer
Devise.setup do |config|
# Other configuration...
# ==> OmniAuth
# Add a OmniAuth providers.
User.omniauth_providers.each do |provider_name|
if provider_name == :developer
config.omniauth :developer
else
api_key = ENV["#{provider_name.upcase}_API_KEY"]
@monkbroc
monkbroc / routes.rb
Created February 18, 2015 17:23
OmniAuth routes
Rails.application.routes.draw do
devise_for :users, :controllers => {
:omniauth_callbacks => "users/omniauth_callbacks"
}
devise_scope :user do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
delete 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
root 'home#index'
@monkbroc
monkbroc / omniauth_callbacks_controller.rb
Created February 18, 2015 17:26
OmniAuth callback controller
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_action :verify_authenticity_token
def sign_in_with(provider_name)
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user, :event => :authentication
set_flash_message(:notice, :success, :kind => provider_name) if is_navigational_format?
end
def facebook
@monkbroc
monkbroc / application.html.erb
Created February 18, 2015 17:44
OmniAuth sign in buttons
<ul class="nav">
<% if user_signed_in? -%>
<li><%= link_to 'Sign out', destroy_user_session_path, :method => :delete %></li>
<% else -%>
<li><%= link_to 'Sign in', new_user_session_path %></li>
<% end -%>
</ul>
@monkbroc
monkbroc / new.html.erb
Created February 18, 2015 17:45
OmniAuth sign in view
<div class="signin-container">
<h2>Sign in with</h2>
<%- if devise_mapping.omniauthable? %>
<ul class="image-list signin-icons">
<%- resource_class.omniauth_providers.each do |provider| %>
<li><%= link_to omniauth_authorize_path(resource_name, provider), :title => "Sign in with #{provider.to_s.titleize}" do %>
<%= image_tag "oauth_#{provider}.png", :alt => "Sign in with #{provider.to_s.titleize}" %><% end -%>
</li>
<% end %>
@monkbroc
monkbroc / bookshelf.rb
Last active August 29, 2015 14:15
Chunk example
Book = Struct.new(:title, :year_published) do
def decade_published
year_published - year_published % 10
end
def to_s
"#{title} (#{year_published})"
end
end
bookshelf = [