下記URLから入手できます。
http://svn
仕様
URLを渡すとTinyURLに変換してリンクを作ります。
使い方
- applicatio
n_helper.r bでinclude 1 module Applicatio
nHelper 2 include TinyurlHel per 3 end - View内で
1 <%= link_to_ti
nyurl "http://www .yahoo.co. jp" %>
作り方
- 類似するプラグインを探してコーディングの検討をつける
- script/gen
erateする 1 script/gen
erate plugin tinyurl_he lper - 上で生成されたlib/tinyur
l_helper.r bを編集 1 require 'net/http' 2 3 module TinyurlHel
per 4 def link_to_ti nyurl(url, html_optio ns = nil) 5 uri = 'http://tin yurl.com/a pi-create. php?url=' + url 6 uri = URI.parse(uri) 7 tiny_url = Net::HTTP.get_respon se(uri).body 8 options = {:title => url, :alt => url} 9 options = html_optio ns.nil? ? options : options.merge(html_optio ns) 10 link_to tiny_url, tiny_url, options 11 end 12 end テストを書く(アプリテスト、プラグイン単体テスト両方通るように書くと吉)
1 - 2 require 'test/unit' 3 require File.expand_pat
h(File.dirname(__FILE__) + "/../lib/ti nyurl_help er") 4 5 class TinyurlHel perTest < Test::Unit ::TestCase 6 include TinyurlHel per 7 8 #dummy link_to 9 def link_to(name, options = {}, html_optio ns = nil) 10 [name, html_optio ns[:title]] 11 end 12 13 def test_link_ to_tinyurl 14 url = 'http://www .yahoo.co. jp/' 15 tiny_url, title = link_to_ti nyurl(url) 16 assert_equ al 'http://tin yurl.com/9 10', tiny_url 17 assert_equ al url, title 18 end 19 20 def test_link_ to_tinyurl _with_titl e 21 url = 'http://www .yahoo.co. jp/' 22 tiny_url, title = link_to_ti nyurl(url, {:title => 'title'}) 23 assert_equ al 'http://tin yurl.com/9 10', tiny_url 24 assert_equ al 'title', title 25 end 26 end README, MIT-LICENS
Eを書く - READMEは名前と、概要・使い方を書く
- MIT-LICENS
Eは名前だけ変更
公開用リポジトリにコミット
- pluginディレクトリに登録する
Tips
- プラグインはweb serverを再起動しないとリロードされない
- helperを使えるようにする方法は2つ
- app/helper
/applicati on_helper. rbでinclude(上でやった方法) - plugins/ti
nyurl_help er/init.rb でsendする 1 ActionView
::Base.send :include, TinyurlHel per
- app/helper
posted by
satoko
on Tue 26 Feb 2008
at 05:12