Railsのacts_as_se
http://git
今後はこちらをお使いいただけるとありがたいです。
Railsのacts_as_se
http://git
今後はこちらをお使いいただけるとありがたいです。
久々にRailsプラグインの紹介です。
This is not simple hack of named scope like you might find in WillPagina
te. This is a fully backported and tested version of NamedScope that will work on rails 1.2.6 or 2.0.4.
Rails-2.0.
This release includes a small spec change.
I changed spec of count method. Now it works without :group option. If you want old behaviour, you can use count_ids method instead.
http://git
Good luck ;-)
named_scop
使い方
まず、モデルクラスの中でinclude
します。
1 class Post 2 include PaginationScope 3 end
これによって、Postクラスにpaginate
というnamed_scop
続いて、Pagination
1 class PostsController < Applicatio nControlle r 2 def index 3 @posts = Post.not_delete d.paginate(params[:page], 10)
のようにScopeを取得します。
続いて、Viewで以下のようにPagination
1 <%= paginate @posts %>
paginateメソッドは Pagination
named_scop
1 User.active do 2 User.count # => Not run 3 end 4 5 User.active.with_scopedo 6 User.count #=> Not scoped 7 end
そこで、昔ながらのwith_scope
これを使うと、以下のように期待通りに動きます。
1 User.active.with do 2 User.count #=> User.active.count 3 end
なかなか便利だと思うので、Rails本家にパッチを送っておきました。
ちょっとActiveScaf
まずは配布サイト。
インストール方法。
1 % ./script/plugin install http://act ivescaffol d.googleco de.com/svn /tags/acti ve_scaffol d
利用レポート、チュートリアルなど。
ActiveScaf
久々にRailsプラグインの紹介です。
Background
使い方ですが、まずはインストール。
1 % svn export https://svn.trix.pl/ public/bac kground_fu vendor/plu gins/backg round_fu
続いて、各種ファイルを生成するGeneratorを起動します。
1 % ./script/generate background
バックグラウンド処理の情報を管理するテーブルを作成するためのmigration ファイルが生成されるので、マイグレーションを実行します。
1 % rake db:migrate
これでひとまず準備完了。あとは、lib/worker
の下に生成されている
exampleファイルや
README
を参考にしながら、Workerクラスを作ります。
Workerクラスの例
1 class ExampleWorker 2 def add(a, b) 3 a + b 4 end 5 end
このWorkerを呼び出す場合は、以下のようにします。
1 Job.enqueue!(ExampleWorker, :add, 1, 2)
単純に時間がかかる処理を裏で実行したいだけならこれでOKです。 簡単ですね。 必要であれば、JobのIDを使ってもうちょっと複雑な処理も実行可能です。
Workerプロセスの起動は、以下のコマンドで行います。
1 % ./script/daemons start
stop
で停止、restart
で再起動です。RAILS_ENVを指定する場合は、
1 % RAILS_ENV=production ./script/d aemons start
こんな感じですね。デフォルトではdevelopmen
環境になります。
See Also
While creating AR extending plugin, I had some trouble to test it. Because testing a such plugin needs creating db and its absurd. So here's my solution.
Tip #1: First, extend the AR, and name it as a MockBase. Then extend MockBase to test your class. This prevents other tests like your rails app or other plugins from failing.
Tip #2. Requiring environmen
1 #acts_as_notifiable_t est.rb 2 begin 3 require File.dirname(__FILE__) + '/../../../ ../config/ environmen t' 4 rescue LoadError 5 require 'rubygems' 6 gem 'activereco rd' 7 require 'active_rec ord' 8 end 9 10 class MockBase < ActiveReco rd::Base; end 11 MockBase.class_eval do 12 alias_meth od :save, :valid? 13 def self.colum ns() @columns ||= []; end 14 15 def self.colum n(name, sql_type = nil, default = nil, null = true) 16 columns << ActiveReco rd::Connection Adapters::Column.new(name.to_s, default, sql_type, null) 17 end 18 end 19 20 class MockModel < MockBase 21 acts_as_no tifiable :callback => :after_crea te, :message => "hello", :recipients => "test@gmail.com" 22 end 23 24 class ActsAsNoti fiableTest < Test::Unit ::TestCase 25 def test_optio n_message_ string 26 assert_equ al "hello", MockModel.new.instance_e val{jabber_mes sage} 27 end
数年に渡ってメンテナンスされているようなRailsアプリケーションの
config/rou
は、
徐々にスパゲッティ化していく傾向があるように思えます。
ということで、今回は、
コントローラごとにRoutesをまとめて綺麗に記述する方法を紹介します。
方法は簡単です。with_optio
を使って、
prefixオプションを指定するようにします。
1 map.with_options(:controller => 'posts', 2 :name_prefi x => 'posts_', 3 :path_prefi x => 'posts') do |posts| 4 # posts_rss_ path => 'posts/rss' 5 posts.rss 'rss', :action => 'rss' 6 end
しかし、全部のController
このプラグインを使えば、以下のようにRoutesを書くことができます。
1 map.with_controller('posts') do |posts| 2 posts.rss # posts_rss_ path => 'posts/rss' 3 end
map.namesp
を使うと似たようなことができるのですが、
これは名前空間の作成に特化されているので、
今回の目的のために使用すると、若干思わしくない挙動をするようです。
RaPT については以前 「RaPT: Railsプラグイン管理ツール」 で紹介しましたが、 手短にRaPTを使ってRailsプラグインを使う方法をまとめておきます。
インストール
1 # gem install rapt
プラグインをインストール
RAILS_ROOT
1 $ rapt install http://repo.pragprog .com/svn/P ublic/plug ins/annota te_models
プラグインをアップデート
まさにこの為にRaptを使う価値があるという代物。
1 $ rapt update annotate_models
便利です。