Merbのプラグインの多くは、Merb以外の場所でも使えるように設計されています。 また、いくつかのRailsプラグインも、Merbや他のプラットフォームで利用できたり、単体でも利用可能になっていたりします。

これらのプラグインは、Agnosticなプラグインと呼ばれています。 従来のプラグイン機構が、特定のフレームワークの機能を拡張する役割にとどまっていたのに対して、Agnosticなプラグインは、複数のプラットフォームを股に掛けて活躍します。

fig

このような動きは、従来のフレームワークとプラグインの関係を改めるものとして注目に値するものだと思います。 フレームワーク間で共通に必要となるようなプラグインの重要性が高まり、 今まで分散していたプラグイン開発リソースが、統合されることによって効率化がはかられたと見る事も出来ます。

これからは、フレームワークの知識だけでなく、 主要なAgnosticプラグインの利用法に習熟する事が 生産性の向上のために必要になってくるかもしれません。

posted by Png genki on Wed 11 Mar 2009 at 08:06

ちまたで話題のCanonical URLですが、既存のRailsアプリケーションを Canonical URLに対応させるためのプラグインを紹介します。

config/environment.rbの中で、以下の行を追加します。

   1  config.gem 'mbleigh-canonical-url',
   2    :lib => 'canonical_url',
   3    :source => 'http://gems.github.com'

続いて、sudo rake gems:install でGemをインストールします。 次に、ViewのHEADの中に以下の一行を追加します。

   1  <%= canonical_link_tag %>

最後に、Controllerのshowアクションの中で、 respond_toやrenderの呼び出しより上に以下の一行を追加します。

   1  canonical_url article_path(@article)

これはarticlesというリソースの例ですが、 実際のアプリケーションで使っているリソース名に置き換えてください。

これで完了。 Slagを使っていたりする場合に、Permalinkをcanonical urlとして指定したりするのが典型的な使い方になるのかな。

See Also

posted by Png genki on Sat 14 Feb 2009 at 22:40

I made a simple plugin to explain a concept of render-filters.

merb_render_filter

In your controller,

   1  class Posts < Application
   2    before_render :set_title1, :only => :show
   3    before :set_title2, :only => :show
   4  
   5    def show(id)
   6      @post = Post.get(id)
   7      display @post
   8    end
   9  
  10  private
  11    def set_title1
  12      @title = @post.title # <= you can access to @post here
  13    end
  14  
  15    def set_title2
  16      @title = @post.title #=> @post is nil!
  17    end
  18  end

Without this plugin, you couldn't access to instance variables from before-filters. Of course you can prepare @post in the first before-filter so that you can access from other before-filters. But why Merb has the action-args? They are ignored in such case. This was the problem I wanted to solve by the plugin.

posted by Png takiuchi on Sun 1 Feb 2009 at 10:19

Merbを使ってWebアプリケーションを開発している or しようと考えている方の為に、 お勧めのプラグイン/Slice情報を共有するサービス Merbist Plugins をテスト公開いたします。

ss

デザインもScaffoldに毛が生えた程度で、 この手のサービスには欠かせないRatingなどの基本機能が無い状態なのですが、 そもそもMerbのプラグイン情報を共有する場所が無いので、 無いよりはちょっとましかなという事で公開しておきます。

merb_rating, dm-has-rating のようなプラグインを見つけたら、 あるいは作ったら、Rating出来るようになると思います。

ちなみに、新着プラグインのFeedも配信しています。

よろしくお願いします。

posted by Png genki on Sat 24 Jan 2009 at 02:50

I released the merb_full_url plugin that provides URL which has origin (scheme, host and port)

You can install this gem like this;

   1  % sudo gem install merb_full_url --source http://merbi.st

And you get full URLs by calling full_url/full_resource methods instead of url/resource.

But I think, it is better for merb to become providing such methods in advance :-)

posted by Png takiuchi on Sat 24 Jan 2009 at 02:30

Merbのプラグインを作る場合、merb-gen plugin plugin-name でひな形が生成されますが、現状では生成されるspecがほとんど空っぽなので、 ちゃんとしたspecを書くための足場の作り方を紹介します。

まずは、spec/spec_helper.rb を以下のような感じに準備します (これはdm-has-versionsの例です)

   1  $:.push File.join(File.dirname(__FILE__), '..', 'lib')
   2  
   3  require 'rubygems'
   4  require 'merb-core'
   5  require 'dm-core'
   6  require "spec"
   7  require 'dm-has-versions/has/versions'
   8  require 'dm-aggregates'
   9  
  10  DataMapper::Model.append_extensions DataMapper::Has::Versions
  11  Merb.disable(:initfile)
  12  Merb.start_environment(
  13    :testing      => true,
  14    :adapter      => 'runner',
  15    :environment  => ENV['MERB_ENV'] || 'test',
  16    :merb_root    => File.dirname(__FILE__) / 'fixture',
  17    :log_file     => File.dirname(__FILE__) / "merb_test.log"
  18  )
  19  DataMapper.setup(:default, "sqlite3::memory:")
  20  
  21  Spec::Runner.configure do |config|
  22    config.include(Merb::Test::ViewHelper)
  23    config.include(Merb::Test::RouteHelper)
  24    config.include(Merb::Test::ControllerHelper)
  25  
  26    DataMapper.auto_migrate!
  27  end

この例では、DataMapperを使う事を前提としています。 "sqlite3::memory:" を指定することで、テストのための データベースファイルなどを用意する必要がないので楽です。

テストで利用されるクラス群は、spec/fixture 以下に、 通常のMerbアプリケーションと同様のディレクトリ階層で用意します。

   1  % tree spec/fixture [~/project/dm-has-versions:master]
   2  spec/fixture
   3  `-- app
   4      `-- models
   5          |-- comment.rb
   6          `-- story.rb

posted by Png genki on Fri 16 Jan 2009 at 13:35

Merbでタイムゾーンを選択するためのプラグインを作りました。

USAGE:

Gem(genki-merb_timezone_select)をインストールして、dependencyを設定し、form_forブッロクの中で、以下のように使います。

   1    <%= timezone_select :timezone %>

以下のような感じの結果が得られます。

ss

posted by Png genki on Tue 6 Jan 2009 at 03:33

MerbのコントローラをParts的に利用するプラグイン merb_component を作りました。 Railsのcomponentに似てますが、Railsと比べて大分シンプルに実現できています。

使い方

githubからgenki-merb_componentをインストールし、 config/dependencies.rbの中で、以下のように記述します。

   1  dependency "genki-merb_component", :require_as => "merb_component"

あとは、Viewの中から以下のような感じで呼び出せばOKです

   1  <%= component Users, :show, :id => 1 %>

merb_partsと違って、全てのコントローラに対して利用出来ます。

posted by Png genki on Mon 5 Jan 2009 at 06:28

以前、 benchmarkforrails というRailsプラグインを紹介した事がありました。 しばらく互換性の問題があって使うのをやめていたのですが、 久々にRails-2.2.2環境で使ってみたら動いたので、 最新版のリポジトリの場所を紹介します。

gitで公開されているので、以下のようにインストールします。

   1  % ./script/plugin install git://github.com/cainlevy/benchmarkforrails.git 

See Also

posted by Png genki on Fri 26 Dec 2008 at 14:29

I made a SIMBL plugin for Terminal.app which enable us to copy on selection. Here is the plugin named TerminalCopyOnSelect.

TerminalCopyOnSelect.bundle.zip

INSTALLATION

  1. At first, you should install SIMBL.
  2. Download the zipped plugin file I mentioned above and expand it.
  3. Copy the extracted file "TerminalCopyOnSelect.bundle" to the plugin directory which is located at ~/Library/Application Support/SIMBL/Plugins/
  4. Terminate all processes of Terminal.app and restart it.

You're done! Now you can get copied selected text from the terminal when you released mouse button on it.

You are also able to disable this feature by turning off the following menu "Copy On Select".

ss01

Finally, I opened the source of this plugin on the GitHub under the MIT license.

Enjoy!

posted by Png takiuchi on Tue 25 Nov 2008 at 19:11 with 8 comments