Skip to content

Instantly share code, notes, and snippets.

@jinto
Created October 24, 2012 01:17
Show Gist options
  • Save jinto/3943126 to your computer and use it in GitHub Desktop.
Save jinto/3943126 to your computer and use it in GitHub Desktop.
rails 시작
rails new darrac -d=postgresql
Gem 파일 적용
rails generate devise user
migration_user.rb 수정
config/ini../devise.rb 수정
edit config/routes.rb # home/index
rm public/index.html
rails g controller home index test bootstrap
app/assets/stylesheets/bootstrap_custom.less
edit app/view/layout/ap...
model/user.rb
production.rb
...
config.action_mailer.default_url_options = { :host => "OOOOOOOOOOO.com" }
# ActionMailer Config
# Setup for productiontion - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :defaultcharset => "utf-8"
##################
config.middleware.use ExceptionNotifier,
:email_prefix => "[OOOOOOOOOOOOOO.com Error] ",
:sender_address => %{"OOOOOOOOOO Noti" <noti@OOOOOOOOOOOO.com>},
:ignore_crawlers => %w{Googlebot bingbot YandexBot Baiduspider},
:exception_recipients => %w{OOOOOOOOOOOO@gmail.com}
....
development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
.........
source 'https://rubygems.org'
gem 'rails', '3.2.8'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'
gem 'cancan'
gem 'paperclip'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
gem 'therubyracer', :platform => :ruby
gem "twitter-bootstrap-rails", '2.1.0' # 버전업이 되면서 망가진걸까?
end
gem 'jquery-rails'
gem 'kaminari'
gem 'thin', '1.3.1'
gem "eventmachine", "1.0.0.rc.4"
gem 'exception_notification'
config.mailer_sender = "OOOO 닷컴 <register@OOOOOOOOOOOO.com>"
config.sign_out_via = :get
config.omniauth :facebook, "OOOOOOOOOOOOOO", "OOOOOOOOOOOOO",
:client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}}
config.omniauth :twitter, "OOOOOOOOOOOOO", "OOOOOOOOOOOOOOOOOOOOO",
:client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}}
# 유저에 이것 추가.
## Token authenticatable
# t.string :authentication_token
t.string :name
t.string :image
t.string :nick, :unique=>true, :null=>false
t.string :lang
t.string :provider
t.string :uid
t.timestamps
end
add_index :users, :provider
add_index :users, :uid
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
attr_accessible :name, :image, :lang, :nick
attr_accessible :role, :provider, :uid
validates :email,:uniqueness=>true
validates :nick, :length => { :minimum => 4 }, :allow_blank => false
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
useremail = User.where(:email => auth.info.email).first
user = User.where(:provider => auth.provider, :uid => auth.uid).first
if user.nil? and useremail
useremail.update_attributes({
:name=>auth.extra.raw_info.name,
:nick=>auth.info.nickname,
:provider => auth.provider,
:uid => auth.uid
})
user = User.where(:provider => auth.provider, :uid => auth.uid).first
elsif user.nil?
#unless user
user = User.new(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
nick:auth.info.nickname,
lang:auth.extra.raw_info.locale,
password:Devise.friendly_token[0,20])
user.skip_confirmation!
user.save
end
if user and auth.info.image.present?
user.update_attributes({:image=>auth.info.image, #항상 새로운 프로필 이미지를 가져오자
:nick=>auth.info.nickname})
#:lang=>auth.extra.raw_info.locale)
end
user
end
def self.find_for_twitter_oauth(auth, signed_in_resource=nil)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
unless user
user = User.new(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.extra.raw_info.name+"@eta_meta_twitter.com",
nick:auth.info.nickname,
#lang:auth.extra.raw_info.locale,
password:Devise.friendly_token[0,20])
user.skip_confirmation!
user.save
end
if auth.info.image.present? # update the user's image every time he logs in
#user.image = auth.info.image #항상 새로운 프로필 이미지를 가져오자
user.update_attributes(:image=>auth.info.image)
#user.update_attributes(:lang=>auth.extra.raw_info.locale)
user.update_attributes(:nick=>auth.info.nickname)
end
user
end
# bypasses Devise's requirement to re-enter current password to edit
def update_with_password(params={})
if params[:password].blank?
params.delete(:password)
params.delete(:password_confirmation) if params[:password_confirmation].blank?
end
update_attributes(params)
end
end
root :to => 'home#index'
devise_for :users, :controllers =>{ :registrations => "users/registrations",
:omniauth_callbacks => "users/omniauth_callbacks" }
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def after_sign_in_path_for(resource)
request.env['omniauth.origin'] || root_url
end
def facebook
#raise request.env["omniauth.auth"].to_yaml
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
#flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in @user, :event => :authentication
redirect_to "/"
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def twitter
#raise request.env["omniauth.auth"].to_yaml
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_twitter_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
#flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in @user, :event => :authentication
redirect_to "/"
else
session["devise.twitter_data"] = request.env["omniauth.auth"].except('extra')
#env["omniauth.auth"].except("extra")
redirect_to new_user_registration_url
end
end
end
#coding: utf-8
class Users::RegistrationsController < Devise::RegistrationsController
def create
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment