内容へ移動
Cat Paw Software
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
トレース:
rails:plugin
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== Rails2 の Plugin ====== ===== インストール ===== script/plugin install hogehoge ===== ActiveRecordプラグインが要求するカラム ===== ^プラグイン^カラム名^型^ |acts_as_list|postion|integer| |acts_as_paranoid|deleted_at|datetime| |acts_as_state_machine|state|string| ===== acts_as_paranoid ===== ActiveRecordに論理削除機能を追加する。destroyで論理削除し、find時に自動的に論理削除されたレコード除外する。deleted_atという名前のnull可なdatetime型カラムが必要 ===== acts_as_list ===== ActiveRecordの順番指定、入れ替えを簡単に記述できる。 インストール script/plugin install acts_as_list 多テーブルにカラム追加 add_column :many_items, :position, :integer モデルを修正 class ManyItems < ActiveRecord::Base belongs_to :one_item acts_as_list :scope => "one_item" end モデルにこんなメソッドが付く many_item.move_lower many_item.move_higher many_item.move_to_top many_item.move_to_bottom many_item.higher_item many_item.lower_item many_item.first? many_item.last? many_item.decrement_position many_item.increment_position many_item.insert_at many_item.remove_from_list ===== acts_as_state_machine / aasm ===== ステートマシンの実装。状態管理を行う処理を簡単に記述できる。 acts_as_state_machineは開発が終了している。現在は、aasmプラグインが使われる。[[http://github.com/rubyist/aasm/tree/master|github]]参照 $ sudo gem sources -a http://gems.github.com # (you only need to do this once) $ sudo gem install rubyist-aasm gemに入れたくない場合はgitから直接railsアプリに入れてしまう。 $ script/plugin install git://github.com/rubyist/aasm.git ==== 以下古い情報 ==== trunkをインストールしていいものなのかは知らんが、以下のコマンドでインストールできる。 script/plugin install http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk/ ActiveRecordにステートマシンを実装、テーブルhogesにstateというStringカラムを作っておくこと。カラム名は任意に指定出来るが、ここは規約に従っておこう。 <code ruby> class Hoge < ActiveRecord::Base acts_as_state_machine :initial => :ready state :ready state :running state :idle state :disposed event :run do transition :from => :ready, :to => :running end event :suspend do transition :from => :running, :to => idle end event :resume do transition :from => :idle, :to => running end event :dispose do transition :from => :idle, :to => :disposed end end </code> 定義したevent名 + !という名前のメソッドが作られる。それを呼び出すと定義したtransitionの通りにstateが変化する。state変更メソッドを呼んだ時にsaveされるのでデータベースに即座に反映される。 <code> hoge = Hoge.new hoge.state => # ready hoge.run! # 定義したevent名 + ! で状態変化メソッドを呼ぶ hoge.state => # running hoge.dispose! # disposeイベントはidle状態の時だけ有効なので変化しない hoge.state => # running </code> イベント変更と同時にメソッドを呼ぶ事が出来る。 <code ruby> class Hoge < ActiveRecord::Base acts_as_state_machine :initial => :ready state :ready state :running state :idle, :exit => :exit_idle state :disposed, :enter => :enter_dispose, :after => :after_dispose def exit_idle p "idle状態からexit" end def enter_dispose p "disposeします。" end def after_dispose p "disposeされました。" end </code> 状態が変化したとき 新しい状態の:enter、データ保存、新しい状態の:after、古い状態の:exit のタイミングで呼ばれる。 イベントの:guardオプションでメソッドを指定すると、そのメソッドがtrueを返したときのみステートが変化する <code ruby> event :run do transition :from => :ready, :to => :running, :gurad => :block_run end def block_run # 真偽値を返す end </code> ===== restful_authentication ===== http://github.com/technoweenie/restful-authentication/tree/master ログイン処理を簡単に記述できるプラグイン。 インストール $ script/plugin install git://github.com/technoweenie/restful-authentication.git この後プラグインディレクトリ名をrestful_authenticationに修正する 使い方 <code> $ ./script/generate authenticated user sessions \ --include-activation \ --stateful \ --aasm --rspec \ --skip-migration \ --skip-routes \ --old-passwords </code> aasmとstatefulはステートマシンを使った状態管理を行うかどうかのオプション。機能は同じだが利用するプラグインが違う。新しく作るアプリではaasmを推奨。予めaasmプラグインをインストールしておくこと。 Controllerに追記 <code ruby> class HogeController < ApplicationController include AuthenticatedSystem // このクラスのすべてのアクションに認証をかける before_filter :login_required // 特定のアクションを除外する場合 before_filter :login_required, except => [:index, :show] end </code> ==== 以下ふるい情報 ==== $ script/plugin install restful_authentication # こっちかも $ script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/ 使い方 $ script/generate authenticated user sessions --include-activation --stateful 以下のものが生成される * ユーザ情報を格納するusersテーブルを作成するマイグレーション * ログイン・ログアウト処理を行うsessions_controller 作成される機能 * ログイン・ログアウト機能 * acts_as_state_machineプラグインを使ったユーザ状態の管理 * メールによるアカウントアクティベーション ===== 国際化(gettext) ===== 2.1ではruby-gettextを使うのが一般的。Rails 2.2では動作しない。2.2からはrails独自の国際化処理が実装されている。 http://www.yotabanana.com/hiki/ja/ruby-gettext-howto-ror.html ruby-gettextはgemでinstallする sudo gem install gettext gettextプラグインはActionMailerの送信メールを自動的にiso-2022-jpにする機能がある。 ===== ページ送り(will_paginate) ===== 色々なライブラリがあるがRails 2.1ではwill_paginateが良く使われているようだ。will_paginateはgemでインストールする。 sudo gem install will_paginate config/environment.rbを修正 <code ruby> Rails::Initializer.run do |config| # これを追加(:version => '~> 2.2' はバージョン2.2以降という意味) config.gem "will_paginate", :version => '~> 2.2' end </code> コントローラ、conditionは無くても良い <code ruby> @hoges = Hoge.paginate(:page => params[:page], :order => "id" , :per_page => 5) </code> ビュー <code ruby> <%= will_paginate(@hoges) %> </code> これだけ! また、繰り返し要素に class="autopagerize_page_element" を付けておくとAutoPagerize対応に! 条件を入れて絞り込み検索するときは… コントローラ <code ruby> @hoge = Hoge.new(params[:hoge]) @hoges = Hoge.paginate( :page => params[:page], :order => "id", :per_page => 20, :conditions => ["code = ? AND name = ?", params[:hoge][:code]], params[:hoge][:name]) </code> ビュー <code html> <% form_for @hoge, :url => {:action => :search} do |f| %> ほげコード:<%= f.text_field :code, :size => 10 %> ほげ名称:<%= f.text_field :name, :size => 10 %> <% end %> <!-- 途中省略 --> <%= will_paginate(@hoges), :params => {:hoge => params[:hoge]} %> </code> ===== open_id_authentication ===== OpenIDを使ったログインを提供。ruby-openidをインストールする必要がある。 * http://github.com/rails/open_id_authentication/tree/master ruby-openidのインストール $ sudo gem install ruby-openid プラグインのインストール $ script/plugin install open_id_authentication githubに移動したらしいので、こっちでインストール $ script/plugin install git://github.com/rails/open_id_authentication.git 使い方…、これ参照 http://gihyo.jp/dev/feature/01/openid/0004 ====== rails3 の plugin ====== Rails3では多くのプラグインが gem で管理されるようになっているが、railsのプラグイン機構で管理されているものも残っている。 ===== acts_as_list ===== インストール $ rails plugin install https://github.com/rails/acts_as_list.git
rails/plugin.txt
· 最終更新:
2011/04/30 12:21
by
nullpon
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ