Upload
yoji-shidara
View
5.967
Download
7
Embed Size (px)
DESCRIPTION
Ruby勉強会@札幌-5
Citation preview
Pluginが広げる
Railsの魅力
しだら ようじ (dara)
2007-09-29Ruby勉強会@札幌-5
Railsプログラミングは楽しい
だからサクサク書ける
認証機能
タグ
ファイルアップロード
書けないことはない
でも
それ、前も書いたよ
あるいは
それ、安全?
国際化対応
非同期処理
携帯電話対応
まじめにやると大変
Plugin
誰が書いても同じようなこと自分には書けないようなこと
誰かが上手に書いてくれる
みんなで書いてみんなで使おう
でも沢山ありすぎて
http://noplans.org/product/t-shirt/
そこでご紹介
認証
acts_as_authenticated
現時点ではこれが定番
http://technoweenie.stikipad.com/plugins/show/Acts+as+Authenticated
使い方
インストール% ./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated (一行で)
どんなプラグインでもインストールは
./script/plugin install [URL]
準備% script/generate authenticated user account% rake db:migrate
Userモデル、ログイン画面、
サインアップ画面等ができあがる
http://localhost:3000/account/
http://localhost:3000/account/login
http://localhost:3000/account/signup
画面はシンプルでもちゃんと動く
ログインした後の処理
class ApplicationController include AuthenticatedSystem before_filter :login_from_cookie
class MyController < ApplicationController before_filter :login_required
認証を要求したいControllerに
ApplicationControllerに
before_filter :login_required, :only =>[:edit, :update]
あるいは
(AccountControllerから移動)
<h3>Welcome user</h3><% if logged_in? %> <p> Welcome, <%=h self.current_user.login %> </p><% else %> <!-- login form goes here --><% end %>
ユーザ名を表示するとき
メールによるactivationも(詳しくはドキュメントを)
タグ
acts_as_taggable_on_steroids
DHH氏作acts_as_taggable
の後継
acts_as_taggable_redux もありますが今回は_on_steroids
http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids
準備
./script/plugin install http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids (一行で)
ruby script/generate acts_as_taggable_migration
rake db:migrate
class Post < ActiveRecord::Base acts_as_taggable belongs_to :user end
p = Post.find(:first) p.tag_list.to_s # "" p.tag_list = "Funny, Silly" p.save p.reload.tag_list.to_s # "Funny, Silly"
p.tag_list.add("Great", "Awful") p.tag_list.remove("Funny")
Postをタグ付け可能にする
使用例
Post.find_tagged_with('Funny, Silly')
Post.find_tagged_with('Funny, Silly', :match_all => true)
タグを持つPost(いずれかの)
あるユーザのPostに関するタグクラウド的なもの
User.find(:first).posts.tag_counts .map{|t| "#{t.name}(#{t.count})"}
タグを持つPost(全て)
データ構造
class ActsAsTaggableMigration < ActiveRecord::Migration def self.up create_table :tags do |t| t.column :name, :string end create_table :taggings do |t| t.column :tag_id, :integer t.column :taggable_id, :integer t.column :taggable_type, :string t.column :created_at, :datetime end add_index :taggings, :tag_id add_index :taggings, [:taggable_id, :taggable_type] end
その他カラム追加でキャッシュ可
デリミタ変更可
国際化対応
Ruby-GetText-Package
http://www.yotabanana.com/hiki/ja/ruby-gettext.html
インストール
# gem install gettext
$KCODE=’u’require 'gettext/rails'
init_gettext "example" # アプリケーション名
config/environment.rbに
app/controllers/application.rbに
poファイルのファイル名と一致させる
これだけでvalidatesのエラーメッセージが日本語に
lang=ko
lang=ja
msgid "message"msgstr "メッセージ"
msgid "Message|Text"msgstr "テキスト"
その他翻訳の方法は今回は省略
参考になります:
http://d.hatena.ne.jp/secondlife/20070207/1170835130
http://www.yotabanana.com/hiki/ja/ruby-gettext-howto-ror.html
携帯電話対応
キャリア判別, 位置情報,端末情報, IPアドレス検査,ディスプレイ情報,
文字コード変換, 絵文字 ...
Jpmobile
携帯サイト開発の面倒事をまとめてサポート
http://jpmobile-rails.org/
どうやってプラグインを見つけていますか?
完成度が低いと自分で書いた方が早かったり
よいプラグインの情報源
人に聞くblog
勉強会のログや報告Rails Chat
Agile Web DevelopmentRAILSify
http://blog.netswitch.jp/articles/tag/rails_plugin
http://andworkspace.net/
http://agilewebdevelopment.com/plugins/top_rated
http://www.railsify.com/
まとめ
プラグインの使い方プラグインのご紹介プラグイン情報について
良いプラグインを共有しましょう
みんなで書いてみんなで使おう
Plugin