acts_as_taggable

plugin があるらしいんですが、gem のナニを実装してみた。
気づきを以下に列挙。

DynamicCalendarHelper なサンプルに盛り込んでみる。
構成としては events を tagging 対応とゆー事で。

  • gem install acts_as_taggable でインストール
  • config/environment.rb を以下のように修正。 (一部のみ引用)
require_gem 'acts_as_taggable'

# Be sure to restart your web server when you modify this file.

# Uncomment below to force Rails into production mode when 
# you don't control web/app server and can't set it the proper way
# ENV['RAILS_ENV'] ||= 'production'
  • app/models/event.rb を以下のように修正。
class Event < ActiveRecord::Base
  acts_as_taggable
end
  • テーブル作成。あえて migrate に頼らず。
mysql> create table tags (
    -> id int(11) not null auto_increment,
    -> name varchar(255) default null,
    -> primary key (id)
    ->);
Query OK, 0 rows affected (0.04 sec)

mysql> create table events_tags (
    -> tag_id int(11) not null,
    -> event_id int(11) not null
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> 
    • LL フレームワーク・エクスプローラの付録を見て作成してみたのですが、インストールしたバージョンと記事の記述が若干異なる部分がありました。上記のケースではテーブル名が違いました。サーチかけた Web コンテンツでも tags_events となっているナニもあり。
  • model 作成 (記事には記述なし)。ま、作るの当たり前だよな。作成したのは以下。
$ ./script/generate model tag
  • 不具合
    • model 作成してなかった。
    • require 'acts_as_taggable' としていた。
  • 実装
    • ajax なナニで events.tag_names.join(' ') した値を出力
    • update_tags アクションで更新処理
  def update_tags
   obj = Event.find(@params[:id])
   obj.tag @params[:tags]
   if obj.save
     render :nothing => true
   end
 end
    • 追加はできるが、修正とか削除ができん。ググっても今イチ。
    • ソースのコメントに解あり。
obj.tag @params[:tags], :clear => true

tag cloud なナニは未実装。TODO は整理できてるんですが、今週中に何とかなるかどうかは微妙。(何とかしたい)