カテゴリー : Ruby

[Ruby on Rails][Haml] text mail 内で改行するにはバックスラッシュを使う

Ruby on Rails + Haml で、text mail 内で改行するには、バックスラッシュを使います。

Dear
= @user.name,
\
Your username is
= @user.username
\
\
\
\
Your status is
= @user.status

[参考]:HAML: Line breaks in text mail

[Ruby] Regexp.union で配列から正規表現の作成方法

Ruby で配列から正規表現の作成は Regexp.union を使ってできます。

# 画像ファイルの拡張子の形式が正しいかチェック
# @param [String] ファイルの拡張子
# @return [Boolean] 正常: true, 異常: false
def valid_image_file_extension?(file_extension)
  regex = Regexp.union(extension_white_list)
  (file_extension =~ regex) ? true : false
end
 
# 許可する拡張子のリスト
def extension_white_list
  %w(jpg jpeg gif png)
end

Regexp – Rubyリファレンスマニュアル

Regexp.unionで配列から正規表現をつくる方法 – memo.yomukaku.net

[Ruby] CarrierWaveでローカルファイルを登録する方法

CarrierWaveでローカルファイルを登録する方法をメモ。

@user = User.new
@user.image.store! File.open(path_to_file)

[参考]:carrierwaveでローカルファイルを登録 #Rails – Qiita

[Ruby on Rails] Action Mailer でのメール送信処理

Ruby on Rails の Action Mailer でのメール送信処理をする方法をメモ。

主にこのへんを読む。

Action Mailer Basics — Ruby on Rails Guides

Rails 3 の Action Mailerまとめ – おもしろWEBサービス開発日記

ActionMailer Railsアプリからメールを送信 – 酒と泪とRubyとRailsと

Action Mailer

Gmail で送信する設定方法

config/environments/production.rb に下記のような感じで設定する。

MyApp::Application.configure do
  # Mailer
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
 
  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: 'example.com',
    user_name: "gmail_user",
    password: "gmail_pass",
    authentication: 'plain',
    enable_starttls_auto: true,
  }
end

[参考]

Rails 3.0 でメールの送信に Gmail を使う – LazyLoadLife

railsでgmailをメールクライアントとして設定する | simpleplay's Log

[Ruby on Rails] ActionMailer views で helper メソッドを使う方法

Ruby on Rails の ActionMailer views で helper メソッドを使う方法をメモ。

例えば、app/views/contact_mailer/inquiry.text.haml で ApplicationHelper に定義したメソッドを使いたい場合は、下記のように add_template_helper メソッドを使います。

class ContactMailer < ActionMailer::Base
  add_template_helper(ApplicationHelper)
 
  def inquiry(contact)
  end
end

[参考]:ruby on rails – How to use my view helpers in my ActionMailer views? – Stack Overflow

[Ruby on Rails] RSpec でのファイルアップロードの方法

Ruby on Rails + RSpec でのファイルアップロードするためのメソッドをメモ。

fixture_file_upload
Rack::Test::UploadedFile.new
ActionController::TestUploadedFile.new
ActionDispatch::Http::UploadedFile.new

[参考]:ruby – How do I test a file upload in rails? – Stack Overflow はてなブックマーク - ruby - How do I test a file upload in rails? - Stack Overflow

[Ruby] will_paginate を nested resources で使う方法

ページネーション機能を提供する gem「will_paginate」を nested resources で使う方法をメモ。

下記のように params に hash でルーティングを指定してあげればOKです。

will_paginate @tasks,
  params: { controller: 'tasks', action: 'index', project_id: @project.id }

[Ruby] Ransack の sort_link を nested resources で使う方法

Ransack の sort_link を nested resources で使う方法をメモ。

下記のように、hash で controller, action, project_id などを指定します。

sort_link @search, :name, 'タスク名',
  { controller: 'tasks', action: 'index', project_id: @project.id }

[参考]

ruby on rails – Ransack with sort_link for nested resources – Stack Overflow はてなブックマーク - ruby on rails - Ransack with sort_link for nested resources - Stack Overflow

ransack/lib/ransack/helpers/form_helper.rb at v0.6.0 · ernie/ransack · GitHub はてなブックマーク - ransack/lib/ransack/helpers/form_helper.rb at v0.6.0 · ernie/ransack · GitHub

[RSpec] stub_const で定数の stub(スタブ)化

RSpec で、定数を stub(スタブ)化するには stub_const メソッドを使います。

(例)定数 FOO をスタブ化

FOO = 7
 
describe "stubbing FOO" do
  it "can stub FOO with a different value" do
    stub_const("FOO", 5)
    FOO.should eq(5)
  end
 
  it "restores the stubbed constant when the example completes" do
    FOO.should eq(7)
  end
end

(例)Class や Module の定数をスタブ化

module MyGem
  class SomeClass
    FOO = 7
  end
end
 
module MyGem
  describe SomeClass do
    it "stubs the nested constant when it is fully qualified" do
      stub_const("MyGem::SomeClass::FOO", 5)
      SomeClass::FOO.should eq(5)
    end
  end
end

[参考]:Stub Defined Constant – Stubbing constants – RSpec Mocks – RSpec – Relish はてなブックマーク - Stub Defined Constant - Stubbing constants - RSpec Mocks - RSpec - Relish

[Ruby on Rails] Boolean型の presence validation

Ruby on Rails で、Boolean型の presence validation は false が invalid になってしまう。

なので、

validates :possible, presence: true

presence を指定するのではなく、

validates :possible, inclusion: {in: [true, false]}

というように inclusion で true, false の2値のみに限定してあげれば良い。

[参考]:validation – Rails: how do I validate that something is a boolean? – Stack Overflow はてなブックマーク - validation - Rails: how do I validate that something is a boolean? - Stack Overflow