SlideShare uma empresa Scribd logo
1 de 44
Baixar para ler offline
ユーザー依存ロジック
をモデルに集める
- record_with_operatorの紹介

大場光一郎                                   Meadowy.org

大場寧子                                        株式会社万葉

2009-08-25;Mitaka.rb#04;Bistro Epices
2009年8月26日水曜日
多くのWebアプ
          リケーションでは
          ユーザーを扱う

2009年8月26日水曜日
ユーザーとモデル
          にまつわるニーズ


2009年8月26日水曜日
例1:
          データを作成/更新した
           ユーザーを記録したい


2009年8月26日水曜日
例2:
                   許す操作を
                ユーザー毎に変えたい


2009年8月26日水曜日
そうなると

2009年8月26日水曜日
モデルでは
          ユーザーが知りたい


2009年8月26日水曜日
こんな風に
                def some_callback
                 if user && user.admin?
                   ....
                 end
                end


2009年8月26日水曜日
実装戦略

2009年8月26日水曜日
ActiveScaffold
               方式


2009年8月26日水曜日
モデルが
                 Controllerの
                current_user
                メソッドをたたく
2009年8月26日水曜日
MVC的に
                スマート
                 でない
2009年8月26日水曜日
モデルにとっての
                「ユーザー」は
                current_user
                    か?
2009年8月26日水曜日
常にそうとはいえない

       •モデルだけを動作させると
        きは?
       •ログインユーザーではない
        ユーザーのための権限を
        チェックするときは?
2009年8月26日水曜日
そこで

2009年8月26日水曜日
record
        _with
        _operator
2009年8月26日水曜日
record_with_operator


                •  Railsプラグイン

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



2009年8月26日水曜日
install
                gem install nay-
                record_with_operator --
                source http://
                gems.github.com

2009年8月26日水曜日
ActiveRecordオブ
          ジェクトに operator
          という属性を追加


2009年8月26日水曜日
データベースに
          created_by カラムが
          あれば自動的に
          operator.idを記録

2009年8月26日水曜日
同じく
          updated_by
          deleted_by
          も記録

2009年8月26日水曜日
created_byがあれば
          関連creatorで
          ユーザーオブジェクトを
          取得できる

2009年8月26日水曜日
同じく
          updater
          deleter
          もとれる

2009年8月26日水曜日
operator
          は伝搬する
2009年8月26日水曜日
operatorの伝搬


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

2009年8月26日水曜日
こんな風に
                parent.operator = user
                parent.children.each do ¦c¦
                 c.operator # => user
                end



2009年8月26日水曜日
つまり

2009年8月26日水曜日
1回入れればOK
                def some_filter
                 @my_record =
                MyModel.find(params[:id])
                 @my_record.operator =
                   current_user
                end

2009年8月26日水曜日
:forオプション
                def some_filter
                 @my_record =
                MyModel.find(params[:id],
                 :for => current_user)
                end


2009年8月26日水曜日
ここまでが
          record_with_o
          peratorの仕事

2009年8月26日水曜日
応用

2009年8月26日水曜日
1.
          named_s
          cope
2009年8月26日水曜日
こんなのが作れる
         # @company の operator に
         # セットされたユーザーに許された
         # ノートを検索する
         notes =
         @company.notes.authorized


2009年8月26日水曜日
関連の拡張とか
          無名スコープとか
          使うとできる

2009年8月26日水曜日
2. 必ず
          操作者を記
          録させる
2009年8月26日水曜日
適当にモジュール
          つくって例外なげ
          る

2009年8月26日水曜日
例外
         @company.save


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


2009年8月26日水曜日
堅くなるけど、現
          実的には常に操作
          者がいるとは限ら
          ないので注意
2009年8月26日水曜日
3. フラグと
          の合わせ技
2009年8月26日水曜日
operator が管
          理者ユーザーでも
          管理機能でなかっ
          たら無視する
2009年8月26日水曜日
管理機能フラグ
         def some_admin_filter
          current_user.as_admin
            = true
         end



2009年8月26日水曜日
モデルで判定

         if operator.as_admin?
           ...
         end




2009年8月26日水曜日
実績

                •某EC

                • 某ポータル


2009年8月26日水曜日
recode_with_operator



                •  協力者募集!
                • http://github.com/nay/
                  record_with_operator/tree




2009年8月26日水曜日

Mais conteúdo relacionado

Mais de Koichiro Ohba

グリーを支えるソーシャルコーディングのすべて
グリーを支えるソーシャルコーディングのすべてグリーを支えるソーシャルコーディングのすべて
グリーを支えるソーシャルコーディングのすべてKoichiro Ohba
 
Agile Shibuya github_enterprise
Agile Shibuya github_enterpriseAgile Shibuya github_enterprise
Agile Shibuya github_enterpriseKoichiro Ohba
 
Cuvic OnDemand on JRuby
Cuvic OnDemand on JRubyCuvic OnDemand on JRuby
Cuvic OnDemand on JRubyKoichiro Ohba
 
Social Coding With JRuby
Social Coding With JRubySocial Coding With JRuby
Social Coding With JRubyKoichiro Ohba
 
MessagePack for Ruby
MessagePack for RubyMessagePack for Ruby
MessagePack for RubyKoichiro Ohba
 
Abstraction of JRuby Kaigi2010
Abstraction of  JRuby Kaigi2010Abstraction of  JRuby Kaigi2010
Abstraction of JRuby Kaigi2010Koichiro Ohba
 
Introduction of JRuby Kaigi 2010
Introduction of JRuby Kaigi 2010Introduction of JRuby Kaigi 2010
Introduction of JRuby Kaigi 2010Koichiro Ohba
 
AppEngine/Jruby at SendaiRubyKaigi02
AppEngine/Jruby at SendaiRubyKaigi02AppEngine/Jruby at SendaiRubyKaigi02
AppEngine/Jruby at SendaiRubyKaigi02Koichiro Ohba
 
The Power to Produce Termtter
The Power to Produce TermtterThe Power to Produce Termtter
The Power to Produce TermtterKoichiro Ohba
 
Happy Life Hacking Ruby on Rails
Happy Life Hacking Ruby on RailsHappy Life Hacking Ruby on Rails
Happy Life Hacking Ruby on RailsKoichiro Ohba
 
JRuby Hot Topics 2008-12-12
JRuby Hot Topics 2008-12-12JRuby Hot Topics 2008-12-12
JRuby Hot Topics 2008-12-12Koichiro Ohba
 
The Essence of Using Ruby on Rails in Corporations 2nd
The Essence of Using Ruby on Rails in Corporations 2ndThe Essence of Using Ruby on Rails in Corporations 2nd
The Essence of Using Ruby on Rails in Corporations 2ndKoichiro Ohba
 
The Essence of Using Ruby on Rails in Corporations
The Essence of Using Ruby on Rails in CorporationsThe Essence of Using Ruby on Rails in Corporations
The Essence of Using Ruby on Rails in CorporationsKoichiro Ohba
 
「愛されたい!」と思ったときにJavaで書くRubyクラス
「愛されたい!」と思ったときにJavaで書くRubyクラス「愛されたい!」と思ったときにJavaで書くRubyクラス
「愛されたい!」と思ったときにJavaで書くRubyクラスKoichiro Ohba
 
逆引きクイックセミナー
逆引きクイックセミナー逆引きクイックセミナー
逆引きクイックセミナーKoichiro Ohba
 

Mais de Koichiro Ohba (16)

グリーを支えるソーシャルコーディングのすべて
グリーを支えるソーシャルコーディングのすべてグリーを支えるソーシャルコーディングのすべて
グリーを支えるソーシャルコーディングのすべて
 
Agile Shibuya github_enterprise
Agile Shibuya github_enterpriseAgile Shibuya github_enterprise
Agile Shibuya github_enterprise
 
Cuvic OnDemand on JRuby
Cuvic OnDemand on JRubyCuvic OnDemand on JRuby
Cuvic OnDemand on JRuby
 
Social Coding With JRuby
Social Coding With JRubySocial Coding With JRuby
Social Coding With JRuby
 
MessagePack for Ruby
MessagePack for RubyMessagePack for Ruby
MessagePack for Ruby
 
Rava on JRuby
Rava on JRubyRava on JRuby
Rava on JRuby
 
Abstraction of JRuby Kaigi2010
Abstraction of  JRuby Kaigi2010Abstraction of  JRuby Kaigi2010
Abstraction of JRuby Kaigi2010
 
Introduction of JRuby Kaigi 2010
Introduction of JRuby Kaigi 2010Introduction of JRuby Kaigi 2010
Introduction of JRuby Kaigi 2010
 
AppEngine/Jruby at SendaiRubyKaigi02
AppEngine/Jruby at SendaiRubyKaigi02AppEngine/Jruby at SendaiRubyKaigi02
AppEngine/Jruby at SendaiRubyKaigi02
 
The Power to Produce Termtter
The Power to Produce TermtterThe Power to Produce Termtter
The Power to Produce Termtter
 
Happy Life Hacking Ruby on Rails
Happy Life Hacking Ruby on RailsHappy Life Hacking Ruby on Rails
Happy Life Hacking Ruby on Rails
 
JRuby Hot Topics 2008-12-12
JRuby Hot Topics 2008-12-12JRuby Hot Topics 2008-12-12
JRuby Hot Topics 2008-12-12
 
The Essence of Using Ruby on Rails in Corporations 2nd
The Essence of Using Ruby on Rails in Corporations 2ndThe Essence of Using Ruby on Rails in Corporations 2nd
The Essence of Using Ruby on Rails in Corporations 2nd
 
The Essence of Using Ruby on Rails in Corporations
The Essence of Using Ruby on Rails in CorporationsThe Essence of Using Ruby on Rails in Corporations
The Essence of Using Ruby on Rails in Corporations
 
「愛されたい!」と思ったときにJavaで書くRubyクラス
「愛されたい!」と思ったときにJavaで書くRubyクラス「愛されたい!」と思ったときにJavaで書くRubyクラス
「愛されたい!」と思ったときにJavaで書くRubyクラス
 
逆引きクイックセミナー
逆引きクイックセミナー逆引きクイックセミナー
逆引きクイックセミナー
 

Introduction of record_with_operator