Introduction of record_with_operator

44
2009-08-25;Mitaka.rb#04;Bistro Epices Meadowy.org ユーザー依存ロジック をモデルに集める - record_with_operatorの紹介 大場光一郎 大場寧子 株式会社万葉 2009826日水曜日

description

Rails plugin to set created_by, updated_by, deleted_by to ActiveRecord objects. Supports associations.

Transcript of Introduction of record_with_operator

Page 1: Introduction of record_with_operator

2009-08-25;Mitaka.rb#04;Bistro Epices

Meadowy.org

ユーザー依存ロジックをモデルに集める

- record_with_operatorの紹介

大場光一郎大場寧子 株式会社万葉

2009年8月26日水曜日

Page 2: Introduction of record_with_operator

多くのWebアプリケーションではユーザーを扱う

2009年8月26日水曜日

Page 3: Introduction of record_with_operator

ユーザーとモデルにまつわるニーズ

2009年8月26日水曜日

Page 4: Introduction of record_with_operator

例1:データを作成/更新したユーザーを記録したい

2009年8月26日水曜日

Page 5: Introduction of record_with_operator

例2:許す操作を

ユーザー毎に変えたい

2009年8月26日水曜日

Page 6: Introduction of record_with_operator

そうなると

2009年8月26日水曜日

Page 7: Introduction of record_with_operator

モデルではユーザーが知りたい

2009年8月26日水曜日

Page 8: Introduction of record_with_operator

こんな風にdef some_callback if user && user.admin? .... endend

2009年8月26日水曜日

Page 9: Introduction of record_with_operator

実装戦略

2009年8月26日水曜日

Page 10: Introduction of record_with_operator

ActiveScaffold方式

2009年8月26日水曜日

Page 11: Introduction of record_with_operator

モデルがControllerのcurrent_userメソッドをたたく

2009年8月26日水曜日

Page 12: Introduction of record_with_operator

MVC的にスマートでない

2009年8月26日水曜日

Page 13: Introduction of record_with_operator

モデルにとっての「ユーザー」はcurrent_user

か?2009年8月26日水曜日

Page 14: Introduction of record_with_operator

常にそうとはいえない

•モデルだけを動作させるときは?•ログインユーザーではないユーザーのための権限をチェックするときは?

2009年8月26日水曜日

Page 15: Introduction of record_with_operator

そこで

2009年8月26日水曜日

Page 16: Introduction of record_with_operator

record_with_operator

2009年8月26日水曜日

Page 17: Introduction of record_with_operator

record_with_operator

•Railsプラグイン•MITライセンス• http://github.com/nay/record_with_operator/tree

2009年8月26日水曜日

Page 18: Introduction of record_with_operator

installgem install nay-record_with_operator --source http://gems.github.com

2009年8月26日水曜日

Page 19: Introduction of record_with_operator

ActiveRecordオブジェクトに operator という属性を追加

2009年8月26日水曜日

Page 20: Introduction of record_with_operator

データベースにcreated_by カラムがあれば自動的に operator.idを記録

2009年8月26日水曜日

Page 21: Introduction of record_with_operator

同じくupdated_bydeleted_byも記録

2009年8月26日水曜日

Page 22: Introduction of record_with_operator

created_byがあれば関連creatorでユーザーオブジェクトを取得できる

2009年8月26日水曜日

Page 23: Introduction of record_with_operator

同じくupdaterdeleterもとれる

2009年8月26日水曜日

Page 24: Introduction of record_with_operator

operatorは伝搬する

2009年8月26日水曜日

Page 25: Introduction of record_with_operator

operatorの伝搬

•関連オブジェクト•関連越しに得たオブジェクト

2009年8月26日水曜日

Page 26: Introduction of record_with_operator

こんな風にparent.operator = userparent.children.each do |c| c.operator # => userend

2009年8月26日水曜日

Page 27: Introduction of record_with_operator

つまり

2009年8月26日水曜日

Page 28: Introduction of record_with_operator

1回入れればOKdef some_filter @my_record = MyModel.find(params[:id]) @my_record.operator = current_userend

2009年8月26日水曜日

Page 29: Introduction of record_with_operator

:forオプションdef some_filter @my_record = MyModel.find(params[:id], :for => current_user)end

2009年8月26日水曜日

Page 30: Introduction of record_with_operator

ここまでがrecord_with_operatorの仕事

2009年8月26日水曜日

Page 31: Introduction of record_with_operator

応用

2009年8月26日水曜日

Page 32: Introduction of record_with_operator

1. named_scope

2009年8月26日水曜日

Page 33: Introduction of record_with_operator

こんなのが作れる# @company の operator に# セットされたユーザーに許された# ノートを検索するnotes = @company.notes.authorized

2009年8月26日水曜日

Page 34: Introduction of record_with_operator

関連の拡張とか無名スコープとか使うとできる

2009年8月26日水曜日

Page 35: Introduction of record_with_operator

2. 必ず操作者を記録させる

2009年8月26日水曜日

Page 36: Introduction of record_with_operator

適当にモジュールつくって例外なげる

2009年8月26日水曜日

Page 37: Introduction of record_with_operator

例外@company.save

# operator入れてないと例外出す# ように自分で制限を強くする# こともできる

2009年8月26日水曜日

Page 38: Introduction of record_with_operator

堅くなるけど、現実的には常に操作者がいるとは限らないので注意

2009年8月26日水曜日

Page 39: Introduction of record_with_operator

3. フラグとの合わせ技

2009年8月26日水曜日

Page 40: Introduction of record_with_operator

operator が管理者ユーザーでも管理機能でなかったら無視する

2009年8月26日水曜日

Page 41: Introduction of record_with_operator

管理機能フラグdef some_admin_filter current_user.as_admin = trueend

2009年8月26日水曜日

Page 42: Introduction of record_with_operator

モデルで判定

if operator.as_admin? ...end

2009年8月26日水曜日

Page 43: Introduction of record_with_operator

実績

•某EC•某ポータル

2009年8月26日水曜日