最新 / RSS

MuraTaka 速記メモ

2009-01 / 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

最近 7 日分 / 今月の一覧

2009-01-02 Fri

ferret を Ruby On Rails で試してみる [RubyOnRails]

- /bin/recykl Blog Archive Live fulltext search in Ruby on Rails
上記サイトをそのままやっているだけ。

ferret と acts_as_ferret の gem を追加で入れておく。

$ sudo gem install ferret
$ sudo gem install acts_as_ferret

project を作成し、Book モデルに ferret を使うことを記載しておく。
require 'acts_as_ferret'

class Book < ActiveRecord::Base
  acts_as_ferret
end

データを数件入れたところで、console で試してみる。
$ ruby script/console 
Loading development environment (Rails 2.2.2)
>> Book.find_by_contents("book")
=> #<ActsAsFerret::SearchResults:0x34e96a4 @per_page=2, @current_page=nil, @total_hits=2, @total_pages=1, @results=[#<Book id: 2, title: "Book secondo", abstract: "Book about book.", created_at: "2009-01-02 13:53:27", updated_at: "2009-01-02 13:53:27">, #<Book id: 1, title: "First Book", abstract: "This is a first book.", created_at: "2009-01-02 13:52:58", updated_at: "2009-01-02 13:52:58">]>
ほぉ、該当モデルに acts_as_ferret の呼び出しを入れるだけで、ferret と ActiveRecord が連携している。。

この後、上記サイトでは、auto_comlete によるインターフェイスを作成する。
auto_complete プラグインをインストールする。
- rails's auto_complete at master - GitHub
$ ruby script/plugin install git://github.com/rails/auto_complete.git
Initialized empty Git repository in /Users/taka/Dropbox/App/books/vendor/plugins/auto_complete/.git/
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 13 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (13/13), done.
From git://github.com/rails/auto_complete
 * branch            HEAD       -> FETCH_HEAD

■ auto_complete の検索窓を用意する

app/views/books に _search_pane.html.erb を作成。
中身は以下の通り。
<%= text_field_with_auto_complete :search, :query %>

検索窓の表示に layout を利用する。
app/views/layouts/books.html.erb は以下の内容。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Books: <%= controller.action_name %></title>
    <%= stylesheet_link_tag 'scaffold' %>
    <%= javascript_include_tag :defaults %>
  </head>
  <body>
    <!-- 検索窓 -->
    <%= render :partial=>"books/search_pane" %>

    <p style="color: green"><%= flash[:notice] %></p>

    <%= yield  %>

  </body>
</html>

■ 結果を表示する

検索結果の内容だけを表示する部分を app/views/books/_search_results.html.erb で用意する。
<ul>
<% for book in @books %>
  <li>
    <%= link_to h(book.title),
            :controller => "books", :action => "show", :id => book %>
  </li>
  <% end %>
</ul>

■ Controller を対応させる

Ajax で呼び出されるメソッドを書く。

  def auto_complete_for_search_query
    @books = Book.find_by_contents(params["search"]["query"] + "*",
                                   { :limit => 5 })
    render :partial => "search_results"
  end

CSRF への対応への対応(?)と、、layout の指定を行っておく。
  layout 'books', :except => [:auto_complete_for_search_query]
  protect_from_forgery :only => [:create, :update, :destroy]

以上で auto_complete の検索、その検索に全文検索を利用したサンプルが完成する。


■ さらに

日本語での全文検索の場合、Tokennizer として、MeCab 使いたい。
- 晴れ 暑い Spare The Air Day - masayangの日記(ピスト通勤他
- ferretを試してみる + MeCabでTokenize - dara日記

- ActsAsFerret - 概要 - rm.jkraemer.net

2009-01-01 Thu

git を MacPorts で入れておく [Tool]

$ sudo port install git-core +gitweb +svn
かなりの量のインストールが行われたが、無事完了。

- hikariworksblog MacPortsでgitをインストール

2008-12-31 Wed

RubyGems 1.3 バージョンアップ [Ruby]

$ sudo gem update --system
ではうまくいかない。

以下で実施する。
$ sudo gem install rubygems-update
$ sudo update_rubygems

Ubuntu では、インストール後、以下の状態となった。
$ gem -v
/usr/local/lib/site_ruby/1.8/rubygems.rb:274:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)

/usr/bin/gem を少し書き変える。
$ diff -u /usr/bin/gem /usr/bin/gem.20090101 
--- /usr/bin/gem        2009-01-01 00:26:15.000000000 +0900
+++ /usr/bin/gem.20090101       2008-07-12 01:27:24.000000000 +0900
@@ -5,9 +5,9 @@
 # See LICENSE.txt for permissions.
 #++
 
-require 'rubygems'
+
 require 'rubygems/gem_runner'
-#Gem.manage_gems
+Gem.manage_gems
 
 required_version = Gem::Version::Requirement.new(">= 1.8.0")
 unless  required_version.satisfied_by?(Gem::Version.new(RUBY_VERSION))

Gem.manage_gems は deprecated らしい。コメントアウトしておいた。
/usr/bin/gem:10:Warning: Gem::manage_gems is deprecated and will be removed on or after March 2009.1.3.1

以下は、Mac での ログ。
RubyGems は MacPort を使ってインストールしている。

$ gem -v
1.1.1

tiger:~/tmp hoge$ gem -v
1.1.1
tiger:~/tmp hoge$ sudo gem install rubygems-update
Password:
Bulk updating Gem source index for: http://gems.rubyforge.org/
Updating metadata for 13 gems from http://gems.rubyonrails.org/
.............
complete
Updating metadata for 13 gems from http://gems.rubyonrails.org/
.............
complete
Successfully installed rubygems-update-1.3.1
1 gem installed

で、

$ sudo update_rubygems

$ gem -v
1.3.1

OK。

RubyGems パッケージ バージョンアップ [Ruby]

$ sudo gem update
Password:
Updating installed gems
Bulk updating Gem source index for: http://gems.rubyforge.org/
Updating metadata for 13 gems from http://gems.rubyonrails.org/
.............
complete
Updating metadata for 13 gems from http://gems.rubyonrails.org/
.............
complete
Updating actionmailer
Bulk updating Gem source index for: http://gems.rubyforge.org/
Updating metadata for 50 gems from http://gems.rubyonrails.org/
..................................................
complete
Successfully installed activesupport-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Updating activerecord
Successfully installed activesupport-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activerecord-2.2.2
Updating activeresource
Successfully installed activesupport-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activerecord-2.2.2
Successfully installed activeresource-2.2.2
Updating gettext
Successfully installed activesupport-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activerecord-2.2.2
Successfully installed activeresource-2.2.2
Successfully installed gettext-1.93.0
Updating rails
Successfully installed activesupport-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activerecord-2.2.2
Successfully installed activeresource-2.2.2
Successfully installed gettext-1.93.0
Successfully installed rake-0.8.3
Successfully installed rails-2.2.2
Updating rmagick
Building native extensions.  This could take a while...
Successfully installed activesupport-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activerecord-2.2.2
Successfully installed activeresource-2.2.2
Successfully installed gettext-1.93.0
Successfully installed rake-0.8.3
Successfully installed rails-2.2.2
Successfully installed rmagick-2.8.0
Updating sqlite3-ruby
Building native extensions.  This could take a while...
Successfully installed activesupport-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activerecord-2.2.2
Successfully installed activeresource-2.2.2
Successfully installed gettext-1.93.0
Successfully installed rake-0.8.3
Successfully installed rails-2.2.2
Successfully installed rmagick-2.8.0
Successfully installed sqlite3-ruby-1.2.4
Gems updated: activesupport, actionpack, actionmailer, activesupport, actionpack, actionmailer, activerecord, activesupport, actionpack, actionmailer, activerecord, activeresource, activesupport, actionpack, actionmailer, activerecord, activeresource, gettext, activesupport, actionpack, actionmailer, activerecord, activeresource, gettext, rake, rails, activesupport, actionpack, actionmailer, activerecord, activeresource, gettext, rake, rails, rmagick, activesupport, actionpack, actionmailer, activerecord, activeresource, gettext, rake, rails, rmagick, sqlite3-ruby

Mephisto を v0.8.1 に [Tool]

- Mephisto - Mephisto 0.8.1: Drax relaunched
ということで、v0.8 から v0.8.1 にしておく。
# といっても、バージョンアップではなく、新規にインストール。

DB の初期化を行なおうとしたところ、

$ rake db:bootstrap
(in /Users/taka/tmp/mephisto/mephisto)
Rails requires RubyGems >= 1.3.1 (you have 1.1.1). Please `gem update --system` and try again.
とのことなので、RubyGems のバージョンアップも行っておく。
$ gem -v
1.3.1

Mephisto のインストールに必要な gem は、tzinfo だったが、
rake db:bootstrap 後、
no such file to load -- liquid
ということなので、必要なようなので入れておく。
$ sudo gem install liquid

ブログのデザインを変えてみる。
- evil.che.lu - projects - Skittlish
がいい感じなのでこれを。

管理画面の [Design]->[Manage thems] の [Import new theme] から、上記サイトでダウンロードした
- skittlish-0.4.zip
を読み込むと、Skittlish が Theme として追加される。

[Setteings]->[General Setteings] の Current Them Path で、追加した Skittlish を設定しておく。
ここでは、パスを指定する必要ある。Import を行った Theme は、
- <rails project root>/themes/development/site-1/skittlish-0.4
に取り込まれているので、
- skittlish-0.4
を指定しておく。

更新ピング用のプラグイン(mephisto_post_ping)を入れようと思ったのだが、
- tech addict mephisto plugins
にあるように、subversion でのレポジトリ
- svn://hasno.info/mephisto/plugins/mephisto_post_ping
は、今は公開されておらず、git での提供になっている。
- segfault's mephisto_post_ping at master - GitHub
なので、git でインストール。
$ ruby script/plugin install git://github.com/segfault/mephisto_post_ping.git
Initialized empty Git repository in /Users/taka/Dropbox/App/mephisto/trunk/vendor/plugins/mephisto_post_ping/.git/
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 13 (delta 1), reused 13 (delta 1)
Unpacking objects: 100% (13/13), done.
From git://github.com/segfault/mephisto_post_ping
 * branch            HEAD       -> FETCH_HEAD

更新ピングの送信先の記載は、lib/config.rb で行う。

送信先は下記のページを参考にした。
- 更新Pingを送信しよう。SEO対策、アクセスアップに。キクの戯れ言 - 雑記

2008-12-28 Sun

2008-12-24 Wed

Ruby での使用可能なメソッドの調べ方整理 [Ruby]

リフレクションを使用し、確認するのが手っ取り早い。
使い方が微妙なときは、ri で確認する。

- クラス、モジュールで使用可能なインスタンスメソッド

class A
  def instnace_method
  end
  
  def self.class_method
  end
end

a = A.new
b = A.new

class << a
  def singleton_method
  end
end

# クラスで調べる
# A クラスのみが持つインスタンスメソッド
# 全てのメソッドを見る場合には、引数の false を省略(デフォルト true なので)
A.instance_methods(false)       # => ["instnace_method"]
# A クラスのみが持つ特異メソッド
# 全てのメソッドを見る場合には、引数の false を省略。(デフォルト true なので)
A.singleton_methods(false)      # => ["class_method"]

# インスタンスで調べる
# a インスタンスが使用できる公開メソッド
a.methods - Object.methods    # => ["instnace_method", "singleton_method"]
# a インスタンスの特異メソッドのみ表示
# false を指定することで、特異メソッドのみの表示となる
a.methods(false)                # => ["singleton_method"]

b.methods - Object.methods      # => ["instnace_method"]
b.methods(false)                # => []

2008-12-22 Mon

実践 Rails の「第1章 基本的な手法」には驚いた [Ruby][RubyOnRails]

実践 Rails -強力なWebアプリケーションをすばやく構築するテクニック
Brad Ediger
オライリージャパン
売り上げランキング: 23752
おすすめ度の平均: 5.0
5 「Railsの外側」の問題解決の宝庫

この本は凄い。
本書の冒頭「はじめに」で、

本書のタイトルが示すように、本書は初心者を対象としていない。
Web のアーキテクチャーを理解し、Ruby 1.8 に精通し、Ruby on Rails で Web アプリケーションを構築した経験が必要である。

と書かれているように、初心向けでは無いかもしれない。
Ruby On Rails を触り始めてこれまでの Ruby の見方がまた一つ変わってきていた。
そう、なかなか踏み込めなかったメタプログミングの世界に足を突っ込み始めた。
別につっこむ必要性は無いかもしれないが、Rails を使っていると、どうしてこういうことができるのか?といろいろな局面で疑問が出てくる。
また、ソースを読むにあたっての基礎知識として、欠けているところがあることを痛感する。

本書の第1章「基本的な手法」はまさにその疑問を解き明かしていくための基礎知識を植えつけてくれる。
「基本」と言いつつ、とても基本とは言えないレベルの内容だが、「1.2.2 メソッド参照」の説明などは非常にうまいものだった。

2008-12-18 Thu

text-translator で英文チェック [Emacs]

英文でメールを書く時など、送信ボタンを押す前に必ず翻訳サイトで書いた内容をチェックしている。
スペルチェックにもなるし、文法の間違い、とんでもない誤訳など。。を見つけれる。

翻訳サイトを開き、書いたメールをサイトに貼りつけて、翻訳ボタンを押す、誤りを見つけたら、修正、
ひととり確認を終えたら、元のメール本文にもう一度貼りつけ。
読み直して、内容を変える時などは、また最初から同じ作業のやり直し。
いや、これは、面倒臭い・・・と思いつつ、いつも同じことの繰り返し。

メーラーは Mac、Windows とも Mew を使っており、Emacs ならあるはず、便利なツールが。

- Emacs でテキスト翻訳をする elisp - とりあえず暇だったし何となく始めたブログ

早速ダウンロードし、使ってみる。

- text-translator.el
- text-translator-vars.el
をロードパスに置き、.emacs.el に下記を追記。

;; for text-translator
(require 'text-translator)
(global-set-key "\C-x\M-t" 'text-translator)
(global-set-key "\C-x\M-T" 'text-translator-translate-last-string)

翻訳したい英文をリージョンに入れておき、C-x M-t。
これだけでいい。あぁ、楽だ。。
メール本文を選択した状態で、C-x M-t。
書き直したら、また C-x M-t。
快適。

翻訳エンジンを切り換えたい場合には、C-c C-a で行う。
結果を見比べてみて、なんとなくだが、翻訳エンジンにもそれぞ得意なジャンルがあるようだ。

2009 : 01 02 03 04 05 06 07 08 09 10 11 12
2008 : 01 02 03 04 05 06 07 08 09 10 11 12
2007 : 01 02 03 04 05 06 07 08 09 10 11 12
2006 : 01 02 03 04 05 06 07 08 09 10 11 12
2005 : 01 02 03 04 05 06 07 08 09 10 11 12
2004 : 01 02 03 04 05 06 07 08 09 10 11 12

最終更新時間: 2009-01-02 23:56