• ブログ
  • Photoshare
  • キーワード
  • ブックマーク
  • ふぉとす
  • 写真
  • ログイン

kaeruspoon

« restful_authenticationを使ってみた | ちょっと調べ物中 »
  • acts_as_state_machineのfind_in_stateを複数状態指定可能にする 2008-06-14 08:58:44 Append 507

    acts_as_state_machineのfind_in_stateは便利ですが、stateはひとつしか指定できません。たとえば、登録ユーザと管理者ユーザの両方でログイン可能なときなどはちょっとメンドくさいです。
    なので、find_in_stateで複数のstate指定をできるようにしてみました。
    config/initializers/acts_as_state_machine.rbを新規作成して以下のコードを書きます。

    module ScottBarron
      module Acts
        module StateMachine
          module ClassMethods
            protected
            def with_state_scope(target_states)
              target_states = [target_states] unless target_states.is_a?(Array)
              raise InvalidState unless target_states.all? {|s| states.include?(s)}
    
              cond = []
              cond_param = []
              target_states.each do |st|
                cond << "#{table_name}.#{state_column} = ?"
                cond_param << st.to_s
              end
    
              with_scope :find => {:conditions => [cond.join(" OR "), cond_param].flatten} do
                yield if block_given?
              end
            end
          end
        end
      end
    end
    

    これで、複数の指定をarrayで指定できるようになります。

    u = find_in_state :first, [:active, :admin], :conditions => {:email => email}
    

    count_in_stateもcalculate_in_stateも複数指定可になります。ちょっと便利になりました。

    Commentコメント(0) Pageリンク元(234)
« restful_authenticationを使ってみた | ちょっと調べ物中 »

プロフィール

おおいしつかさ

過去の記事

2006年
12月
2007年
1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
2008年
1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
2009年
1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
2010年
1月 2月 3月

キーワード一覧

ActionScript Ajax AmazonResources Erlang git github iPhone javascript Javascript Kaerukeyword kaeruspoon Mac merb milook MiyazakiResistance NSR Objective-C perl Perl Rails Ruby Ruby on Rails RubyKaigi speedpetal subversion Thin Thinkpad tokyobike TRPG twitter ubuntu Ubuntu VAIO VAIO typeZ Waves WordScoop Xen ぐりぐり しろさん カンタロー スノボー ドトール ドライブ バイク プログラミング ユルさん 執筆 宮崎 日本酒 模型 猫 町田 真中洋嗣 自転車
Baner