2016年6月23日 星期四

Published 晚上10:42 by with 0 comment

Ruby & Rails Coding Style



對於團隊來說,相同的 Coding Style 可以增加工作效率, 也可以減少髒code的產生.

以下是整理網路上的訊息 (筆者最近才開始研究)

1.  Ruby & Rails 風格指南

2. Ruby Coding Style Guides

3. 如何在Rails專案中使用Rubocop統一程式風格?  ( gem rubocop )

    => 原文作者有熱心提供設定檔, 可惜的是因為 rubocop有改版, 所以很多需要rename.

    => Gem Rubocop

    => 安裝步驟 1. /gemfile 新增 gem 'rubocop', '~> 0.40.0', require: false
                           2. 執行 bundle
                           3. 在該資料夾跟目錄底下新增 .rubocop.yml (注意rubocop前方有一點)
                               (位置會在 , 你的app名稱/ 底下直接新增)
                           4. 撰寫  .rubocop.yml
                           5. 執行 rubocop
                         
                           備註:可以跳過 3. & 4. ,直接執行5. ... 但你會發現你想先設定一下

4. 簡易教學 rubocop 

5. 業界案例 : T客邦



後續再補詳細的介紹
Read More
      edit

2016年6月15日 星期三

Published 上午10:50 by with 0 comment

Ruby on Rails - Devise 設定忘記密碼Email

Rails 本身就透過 Action Mailer 支援 E-mail.

這篇主要是在講解 如何設定 Devise 寄送忘記密碼的信件

本篇來源:

1. Rails Guides

2. ihower Ruby on Rails 實戰聖經

3. How To: Mass password reset and email notification

前情提要:

已經安裝過 gem devise
尚未設定 actionmailer



Step 1.

在/config/environments/production.rb


config.action_mailer.raise_delivery_errors = false 改成 true

Step 2.

在Step 那行code的下方新增:


config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: "http://localhost:3000"} #你的網址,絕對網址 
config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => "587",
    :domain => "gmail.com",
    :authentication => "plain",
    :user_name => "example@gmail.com", #你的信箱
    :password => "123456", #信箱密碼
    :enable_starttls_auto => true
 }

這邊的範例使用 gmail 來設定

Step 3.

正如同上面所看到的會需要輸入使用的信箱跟密碼, 所以需要把這些資訊藏起來

所以我們會新增 email.yml 放在 congig/ 底下


production:
  address: "smtp.mailgun.org"
  port: 587"
  domain: "gmail.com"
  authentication: "plain"
  user_name: "example@gmail.com", #你的信箱
  password: "123456", #信箱密碼 
  enable_starttls_auto: true 

完成後我們要到 .gitignore 新增


/config/email.yml
回到原本的地方寫成


config.action_mailer.smtp_settings = config_for(:email).symbolize_keys
最後我們在/config/environments/production.rb


  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_url_options = { host: "http://localhost:3000"}
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = config_for(:email).symbolize_keys
設定完environments之後

要開始建立一個 mailer 寄信程式

Step 4.

輸入:

rails generate mailer UserMailer
這會在 app/mailers/ 新增 user_mailer.rb

架構類似 controllers內的檔案

class UserMailer < ActionMailer::Base
    default :from => "http://localhost:3000" #你的網址
end
之後我們新增

def password_reset(user, token) 
    @resource = user 
    @token = token 
    mail(:to => user.email, 
         :subject => 'Password Reset Notification') 
end
到 user_mailer.rb 內

Step 5.

還有到 lib/tasks/ 新增 devise.rake 內容:

namespace :device do
  desc "Mass password reset and send email instructions"
  task mass_password_reset: :environment do
    model = ENV['MODEL'] || 'User'
    begin
      model_mailer = "#{model}Mailer".constantize
      model = model.constantize

      model.where(id: 1).each do |record|
        raw, enc = Devise.token_generator.generate(model, :reset_password_token)

          record.reset_password_token = enc
          record.reset_password_sent_at = Time.now.utc
          record.save

        # Send change notification (Ensure you have created #{model}Mailer e.g. UserMailer)
        model_mailer.password_reset(record, raw).deliver_now
      end
    rescue Exception => e
      puts "Error: #{e.message}"
    end
  end
end
Step 6.

到config/initializers/devise.rb 修改

config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' 
=> '12345@gamil.com' #你的信箱
到config/locales/devise.en.yml 修改

reset_password_instructions: 
 subject: "Reset password instructions"=>"信的主旨"
Step 7.

目前完成 production的功能, 但是在開發時也想要做測試 (development )

我們會安裝 letter_opener

到gemfile 目錄底下新增


gem "letter_opener", :group => :development
在/config/environments/development.rb 修改成

config.action_mailer.delivery_method = :letter_opener
 config.action_mailer.default_url_options = { :host => 'localhost:3000' }
這樣在開發模式底下就會另開新頁 顯示信的內容.

Step 8.

完成!!!










Read More
      edit
Published 上午9:32 by with 0 comment

ALPHAcamp Demo Day, 今天是我的主場! - 我是AC Bootcamp web #8 這不是最後一天 , 是起點

(-Demo Day 活動現場-) 史蒂芬: 這是我最愛的功能. 史蒂芬講完這句話之後, 這三個月來的壓力一瞬間煙消雲散. 還記得二月底跟前公司離職, 一週之後踏入南京東路二段97號五樓. 之後的每天過著這輩子最用功的一段時間, 也認識了一群一輩子的朋友. 你畢業之後要做什麼? 這問題早在六年多前被問過. 殊不知...六年後的今天也可以再被問到. 會慌嗎? 那當然, 因為未來會怎樣你也不知道. 但慌的點是不知道自己是否準備好了. 所以呢? 你幹嘛來? 你的目的又是什麼? ...還記得嗎? 三個月前的面試, Tim Du 最後說了一句話: 看來你自己很清楚自己要幹嘛.. 謝謝這陣子願意跟我討論的所有人, 你/妳們提醒了我,自己當初的承諾 不要往簡單的路走. 如果要走, 為什麼我要來這? 但要如何定義簡單? 這答案沒有一定, 但是走了不要後悔 謝謝 ALPHA Camp 帶給我們這麼好的師資, 課程規劃, 還有環境. 謝謝 Bernard 在去年跟我說: 如果你真的有想做的idea, 那就自己動手做吧! 謝謝 Hugo 盧毅 卓均 這三個月有你/妳們一起唸書, 真的很開心,我愛你/妳們! 謝謝 張文鈿 謝昇佑 Taker Li-Yi 翁麒皓 教會我Rails & 解了無數個Bug 謝謝 PiHan 林思妤 Damon 林士庭 Stephanie 方心妤, 很珍惜與你們一起創造我的記分板! 謝謝同屆的所有同學, 還有在AC的所有同仁~ 這段時間有你們的陪伴,過得很充實! 當然謝謝的最後是家人 李秀英 Li Hung Chen 我爸, 謝謝你們的支持!



  • 靠左
  • 置中
  • 靠右
移除
本魯第一次揪了50+人吃熱炒....


Read More
      edit