LoginSignup
92
103

More than 5 years have passed since last update.

Rails初心者がチェックしておくべき便利なGem一覧

Last updated at Posted at 2016-06-23
1 / 19

Annotate

おすすめ度 : ★★★★★
テーブルのスキーマをモデルのファイルの先頭にコメントとして挿入してくれます。
後述するFactoryGirlなどにも対応していて、テスト実装の際にも役立ちます。
また、ルーティングもroute.rbに書き出してくれるので、rake routeで確認する手間が省けます。


# == Schema Info
#
# Table name: line_items
#
#  id                  :integer(11)    not null, primary key
#  quantity            :integer(11)    not null
#  product_id          :integer(11)    not null
#  unit_price          :float
#  order_id            :integer(11)
#

 class LineItem < ActiveRecord::Base
   belongs_to :product
  . . .

Ridgepole

おすすめ度 : ★★★★☆
DBのスキーマ定義を1つの設定ファイルに集約できて、
冪等な設定で運用できるようになります。

参考
クックパッドにおける最近のActiveRecord運用事情


activerecord-import

おすすめ度 : ★★★★☆
複数レコードのBULK INSERTが簡単に実装できるようになります。

books = []
100.times do |_i|
  books << Book.new(:name => "book_#{i}_#{_i}")
end

Book.import books

Squeel

おすすめ度 : ★★★★☆
複雑なクエリが直感的な記述で実装できるようになります。

# before
Article.where ['created_at >= ?', 2.weeks.ago]
Person.where(
  '(name LIKE ? AND salary < ?) OR (name LIKE ? AND salary > ?)',
  'Ernie%', 50000, 'Joe%', 100000
)

# after
Article.where { created_at >= 2.weeks.ago }
Person.where { (name =~ 'Ernie%') & (salary < 50000) | (name =~ 'Joe%') & (salary > 100000) }

ActiveHash

おすすめ度 : ★★★★☆
yamlやJSON、Rubyのハッシュを、ActiveRecordのように扱えるようになります。

class Country < ActiveHash::Base
  self.data = [
    {:id => 1, :name => "US"},
    {:id => 2, :name => "Canada"}
  ]
end

Country.count
# => 2

Octopus

おすすめ度 : ★★★★☆
レプリケーション構成でDBを管理している場合のMaster/Slave切り替えや、
DBをシャーディングで運用している場合など、
簡単に接続先を切り替えられるようになります。
似た用途のGemとしてSwitchPointなどもあります。


Unicorn

おすすめ度 : ★★★★★
実質Rails標準のWebサーバです。
Rails5からActionCableの追加もあり、Pumaが推奨されています。


Dalli

おすすめ度 : ★★★★★
Railsで使えるmemcachedクライアントです。

require 'dalli'
options = {
  namespace: "app_v1",
  compress: true,
}
dc = Dalli::Client.new('localhost:11211', options)
dc.set('abc', 123)
value = dc.get('abc')

Kaminari

おすすめ度 : ★★★★☆
ページネーションを簡単に実装できるようになります。

# To fetch the 7th page of users
# (default per_page is 25)
User.page(7)

# To show a lot more users per each page
# (change the per_page value)
User.page(7).per(50)

factory_girl_rails

おすすめ度 : ★★★★★
テスト用のデータが簡単に生成できるようになります。

FactoryGirl.define do
  factory :user do
    first_name "John"
    last_name  "Doe"
  end
end

user = create :user

Faker

おすすめ度 : ★★★★☆
一般的にありがちなテストデータを簡単に作成できるようになります。

Faker::Name.name
#=> "Christophe Bartell"

Faker::Business.credit_card_number
#=> "1228-1221-1221-1431"

Faker::Internet.email
#=> "eliza@mann.net"

Faker::SlackEmoji.people
#=> ":sleepy:"

WebMock

おすすめ度 : ★★★★☆
簡単にHTTP通信をモックできるようになります。
外部APIの実行が必要なテストなどで、不要な通信をなくすことができます。

stub_request(:any, "www.example.com")

Net::HTTP.get("www.example.com", "/")
# ===> Success

timecop

おすすめ度 : ★★★★★
好きな時刻にタイムスリップできるようになります。
テスト実装時などに有用です。

new_time = Time.local(2008, 9, 1, 12, 0, 0)

# Freeze time to a specific point.
Timecop.freeze(new_time)

# Turn off Timecop
Timecop.return

SimpleCov

おすすめ度 : ★★★★★
テストを実行すると、カバレッジを計測してcoverage/index.htmlにレポートを出力してくれます。


autodoc

おすすめ度 : ★★★★☆
APIのテストを実装すると関連するAPIドキュメントを自動生成してくれるライブラリです。

describe "GET /entries", autodoc: true do
  it "returns entries" do
    get "/entries"
    last_response.status.should == 200
  end
end

RuboCop

おすすめ度 : ★★★★★
ソースコードを静的解析してくれます。
標準でのコーディング規約もありますが、
.rubocop.ymlを編集することでチームのコーディング規約を定義することもできます。


MessagePack

おすすめ度 : ★★★★☆
MessagePackをRubyで使えるようになります。

# Serializing objects
msg = MessagePack.pack(obj)  # or
msg = obj.to_msgpack

# Deserializing objects
obj = MessagePack.unpack(msg)
92
103
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
92
103