2014年7月20日 星期日

git 常用指令

學習網站



git checkout --track -b  develop origin/develop

tig

git rebase HEAD^

git reset
git revert

git fetch

git pull —rebase

git tag

git rebase

origin(remote) 是 Repository 的版本
master(branch) 是 local 端, 正在修改的版本

Git 產生新的 branch

git branch # 列出目前有多少 branch
git branch -d new-branch # 刪除 new-branch
git branch new-branch # 產生新的 branch (名稱: new-branch), 若沒有特別指定, 會由目前所在的 branch / master 直接複製一份.

Git checkout 切換 branch

git checkout branch-name # 切換到 branch-name

Git reset 還原

git reset --hard HEAD # 還原到最前面
git reset --hard HEAD~3
git reset --soft HEAD~3
git reset HEAD filename # 從 staging area 狀態回到 unstaging 或 untracked (檔案內容並不會改變)

遠端 Repository 相關

Git remote 維護遠端檔案

git remote
git remote add new-branch http://git.example.com.tw/project.git # 增加遠端 Repository 的 branch(origin -> project)
git remote show # 秀出現在有多少 Repository
git remote rm new-branch # 刪掉
git remote update # 更新所有 Repository branch
git branch -r # 列出所有 Repository branch

去拉遠端最新的分支與同步

git fetch -p

把本機的develop跟遠端最新的develop同步

git pull origin develop

把本機的XX分支推上遠端

git push origin XX分支名XX(如果遠端沒有XX分支的話,那就用幫建一個,如果有的話,就會被蓋過去)

Git 還原已被刪除的檔案

  • git ls-files -d # 查看已刪除的檔案
  • git ls-files -d | xargs git checkout -- # 將已刪除的檔案還原

2014年7月19日 星期六

ruby . array.all , bsearch


data.select { |h| h[:sex] == sex}.all? { |a| a[:age] > age_is_greater_than }
先select出來 再用all來找出是否全部符合條件
all?() public
Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is all? will return true only if none of the collection members are false or nil.)
bsearch
ary = [0, 4, 7, 10, 12]
# try to find v such that 4 <= v < 8
ary.bsearch {|x| 1 - x / 4 } #=> 4 or 7
# try to find v such that 8 <= v < 10
ary.bsearch {|x| 4 - x / 2 } #=> nil
我簡單的理解成當要用binary search來找array時 如何設計條件
要找出4 <= v < 8的 就是條件中{|x| 1 - x / 4 } x的範圍為多少時 結果為0
In find-any mode (this behaves like libc’s bsearch(3)), the block must return a number, and there must be two indices i and j (0 <= i <= j <= ary.size) so that:
the block returns a positive number for ary if 0 <= k < i,
the block returns zero for ary if i <= k < j, and
the block returns a negative number for ary if j <= k < ary.size.
Under this condition, this method returns any element whose index is within i…j. If i is equal to j (i.e., there is no element that satisfies the block), this method returns nil.

2014年7月1日 星期二

devise omniauth-fb carrierwave 登入


安裝 gem
/Gemfile
gem "devise" gem "omniauth-facebook"
設定 devise
/config/initializiers/devise.rb
config.omniauth :facebook, Settings.story.fb_app_id, Settings.story.fb_secret, {:provider_ignores_state => true}
add the omniauthable model to the devise method in our User model.
/app/models/user.rb
class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :omniauthable, :recoverable, :rememberable, :trackable, :validatable end
如此會產生兩個routes
user_omniauth_authorize
GET|POST /users/auth/:provider(.:format) omniauth_callbacks#passthru {:provider=>/facebook/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) omniauth_callbacks#(?-mix:facebook)
建立一個omniauth_callbacks的controller來處理以上的這些資訊
設定
/routes.rb
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
/app/controllers/omniauth_callbacks_controller.rb
1 class OmniauthCallbacksController < Devise::OmniauthCallbacksController 2 3 def all 4 user = User.from_omniauth(request.env["omniauth.auth"]) 5 if user.persisted? 6 sign_in_and_redirect user, notice: "Signed in!" 7 else 8 session["devise.user_attributes"] = user.attributes 9 redirect_to new_user_registration_url 10 end 11 end 12 13 alias_method :facebook, :all 14 end
user要多增加欄位來存provider和uid
增加上述的from_omniauth
/app/models/user.rb
19 def self.from_omniauth(auth) 20 where(auth.slice(:provider, :uid)).first_or_create do |user| 21 user.provider = auth.provider 22 user.uid = auth.uid 23 user.email = auth.info.email 24 user.name = auth.info.name 25 user.remote_image_url = auth.info.image.gsub('http://','https://') 26 end 27 end
增加new_with_session
/app/models/user.rb
29 def self.new_with_session(params, session) 30 if session["devise.user_attributes"] 31 new(session["devise.user_attributes"], without_protection: true) do |user| 32 user.attributes = params 33 user.valid? 34 end 35 else 36 super 37 end 38 end
password_required?
/app/models/user.rb
40 def update_with_password(params, *options) 41 if encrypted_password.blank? 42 update_attributes(params, *options) 43 else 44 super 45 end 46 end 47 48 def password_required? 49 super && provider.blank? 50 end 51 52 def email_required? 53 super && provider.blank? 54 end