Skip to content

Instantly share code, notes, and snippets.

@pachisaez
pachisaez / Gemfile
Last active August 29, 2015 14:06
Configuring subdomains so we can access our API thorugh URLs like http://api.facilethings.com/v1
gem "subdomain-fu"
gem "subdomain_routes"
@pachisaez
pachisaez / application_controller.rb
Last active August 29, 2015 14:06
Detecting the device that is accessing the app so you can adapt views and behavior to it, via the Mobvious plugin (https://github.com/jistr/mobvious). Rendering .mobile files only if the device is a smartphone. session[:device] will contain "desktop", "tablet" or "mobile".
class ApplicationController < ActionController::Base
before_filter :prepare_for_mobile
# we'll render *.mobile.erb files instead of *.html.erb files when the
# device session variable is set to "mobile"
def prepare_for_mobile
session[:device] = params[:device] if params[:device]
request.format = :mobile if mobile_device?
end
@pachisaez
pachisaez / application_controller.rb
Last active August 29, 2015 14:06
Internationalization for two languages: EN (english) and ES (spanish), taking EN by default.
class ApplicationController < ActionController::Base
before_filter :set_locale
protected
def set_locale
locale = params[:locale] || session[:locale] ||
(extact_locale_from_accept_language_header=='es' ? 'es' : 'en')
session[:locale] = locale
I18n.locale = locale
end
@pachisaez
pachisaez / users.rb
Last active August 29, 2015 14:06
Encrypting user passwords. How to encrypt and save passwords, and how to authenticate users checking the encrypted password.
require 'digest/sha1'
class User < ActiveRecord::Base
attr_accessible :password
# password is a virtual attribute just to hold the plain password
# typed by the user
def password
@password