LoginSignup
17
14

More than 5 years have passed since last update.

管理画面を作るときに使うとヨサソウなGem

Posted at

環境(2016/5/19時点)

  • Ruby 2.3.1
  • Rails 5.0.0.rc1
  • omniauth-google-oauth2 (0.4.0)
  • banken (1.0.2)
  • paper_trail (5.0.1)

Gem

omniauth-google-oauth2

Googleアカウントで認証できるやつです。
Googleアカウントを発行する組織は多いと思うので、使うと良いと思います。
メンバー管理もGoogleアカウントで管理すると楽そうです。
アクセス制限などをかけると思いますが、念のためアドレスのドメインも判定しておくと、プライベートGoogleアカウントを防げて良いと思います。

  def email_check(email)
    email.split('@', 2).last == 'hoge.co.jp'
  end

banken

権限管理ができます。
Controlleraction 単位で権限が制御できるので便利です。 また、Rails4.1 から導入された Enum を組み合わせるともっと便利です。
   

paper_trail

バージョン(モデルの変更)管理できます。
DBを操作するようなものを作るときは、履歴を残すことができるので便利かつ安全です。
最新のバージョンでは、デフォルトでは、 whodunnit (モデル操作した current_user.id が入っていた)
の値は更新されなくなっていたので、使いたい場合は ApplicationController に追記が必要です。

https://github.com/airblade/paper_trail/commit/3ce2b34b98345cad736f64a3a5e1374f40adc652

ApplicationContloller.rb

class ApplicationController
  before_action :set_paper_trail_whodunnit
end

実装

current_user に気をつける

bankenpaper_trailApplicationController などに current_user という名前でログインしているユーザを判定できるメソッドを定義しておく必要があります。もし、名前の違うものを使いたかったら、 alias_method を使って current_user を呼び出せるようにしておくと良いと思います。

ApplicationContloller.rb

  # 通常は current_user でヨサソウ
  def current_operator
    @current_operator ||= Operator.find_by(id: session[:operator_id])
  end

  # View からも呼び出せるように
  helper_method :current_operator

  # 名前が違うときは current_user で呼び出せるように
  alias_method :current_user, :current_operator

paper_trail は、user_for_paper_trail を上書きしてもヨサソウです。

REF

17
14
0

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
17
14