Home > Programming
Programming Archive
UITextFieldに入力されている文字数を動的に、非同期に数える。
- 2010-03-03 (Wed)
- Cocoa
以下はUITextFieldに入力されている文字数を動的に、非同期に数える。 | アクトインディ技術部隊報告書と同じものです。
とあるUITextFieldによる入力フォームと、UIBarButtonItemによる「完了」ボタンがある画面を考えます。
今回のエントリの目的はUITextFieldに文字が入力されていない、つまり空であるときは、「完了」ボタンを表示しない画面を作成するということです。
ポイントはUIControlEventEditingChangedイベントを使うこと。
最初はUITextFieldDelegateのtextField:shouldChangeCharactersInRange:replacementString:とかを使用することを考えましたが、文字を消去したときにボタンが消えない等々うまくいきませんでした。
サンプルコードを書いてみます。
- (void)setupUserInterface
{
UIView *contentView;
UITextField *textField;
contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self setView:contentView];
[contentView release];
doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(respondsToDoneButtonTouchDown:)];
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
[contentView addSubview:textField];
[textField release];
[textField addTarget:self
action:@selector(respondsToEditingChanged:)
forControlEvents:UIControlEventEditingChanged];
}
- (void)respondsToEditingChanged:(id)sender
{
if ([sender isKindOfClass:[UITextField class]])
{
// 本当はsenderを用いずにメンバ変数とかにしておけばいいのだけれど、今回はあえて型変換をしてみる。
if ([[(UITextField *)sender text] length])
{
[[self navigationItem] setRightBarButtonItem:doneButton animated:YES];
}
else
{
[[self navigationItem] setRightBarButtonItem:nil animated:YES];
}
}
}
参考
Disable button if textField is empty – iPhone SDK Development
- Comments: 0
- Trackbacks: 0
iPhoneによる位置情報関係のまとめ
- 2010-01-31 (Sun)
- Cocoa
以下はiPhoneによる位置情報関係のまとめ | アクトインディ技術部隊報告書と同様のものです。
今回も前回のGeohashに引き続き、位置情報に関する内容です。
ググれば出てくる内容なので、新しいことはなんにもないのが申し訳ないところです。
位置情報に関するライブラリは
- CoreLocation
- MapKit
などがあります。
GPSで現在位置を取得するにはCoreLocationを使用し、地図を表示したり現在地の大まかな住所を取得するためにはMapKitを使用します。
GPSで現在位置を取得するには
CoreLocationではCLLocationManagerDelegateを実装し
- (void)setup
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager locationServicesEnabled])
{
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation);
}
このようにすればLogに現在地の情報が取得できます。
地図を表示するには
- (void)setup
{
CGRect screen = [[UIScreen mainScreen] bounds];
MKMapView *mapView = [[MKMapView alloc] initWithFrame:screen];
[[self view] addSubview:mapView];
[mapView release];
MKCoordinateRegion region;
region.center = [location coordinate];
region.span.latitudeDelta = 0.005;
region.span.longitudeDelta = 0.005;
[mapView setRegion:region animated:YES];
}
とすれば、locationで指定した座標に移動します。
現在地を取得するには
MKReverseGeocoderDelegateを実装し
- (void)setup
{
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:[location coordinate]];
[reverseGeocoder setDelegate:self];
[reverseGeocoder start];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
NSLog(@"%@", [placemark title]);
}
とすることで取得することができます。
すべてのAPIが非常にシンプルに作られているので、使用するのは非常に簡単です。
- Comments: 0
- Trackbacks: 0
Objective-C的???Geohash
- 2010-01-24 (Sun)
- Cocoa
以下はObjective-C的???Geohash | アクトインディ技術部隊報告書と同様のものです。
先日chibaさんがGeohashをdecodeするものをCLで書いていらしたので、それを微妙に参考にしながらCocoaのCoreLocationのCLLocationにgeohashをencode/decodeするメソッド追加してみました。
本当はビット演算がしたいのですが、文字列処理の方が自分には単純で簡単だったため、とりあえず今回は文字列処理にしてみました。
我ながらひどいコードだと思いますので、これからリファクタリングをしていきたいところです。
[CLLocation+Geohash.h]
#import <CoreLocation/CoreLocation.h> #import <Foundation/Foundation.h> @interface CLLocation(Geohash) @property (nonatomic, readonly) NSString *geohash; + (CLLocation *)locationFromGeohash:(NSString *)aGeohash; @end
[CLLocation+Geohash.m]
#import "CLLocation+Geohash.h"
#include <math.h>
NSString *int_to_binary(NSUInteger input)
{
if (input == 1 || input == 0)
{
return [NSString stringWithFormat:@"%d", input];
}
return [NSString stringWithFormat:@"%@%d", int_to_binary(input / 2), input % 2];
}
double parse_binary(NSString *binary, double max, double min)
{
double mid = 0.0;
for (NSUInteger i = 0; i < [binary length]; ++i)
{
if ([binary characterAtIndex:i] == '1')
{
min = mid;
}
else
{
max = mid;
}
mid = (max + min) / 2;
}
return mid;
}
NSUInteger binary_to_int(NSString *input)
{
NSUInteger result, length;
result = 0;
length = [input length];
for (NSUInteger i = 0; i < length; ++i)
{
if ([input characterAtIndex:i] == '1')
{
result += pow(2, length - i - 1);
}
}
return result;
}
NSString * generate_binary(double input, double max, double min, int cutoff)
{
NSMutableString *result;
double mid;
result = [NSMutableString string];
for (int i = 0; i < cutoff; ++i)
{
mid = (max + min) / 2;
if (input > mid)
{
[result appendString:@"1"];
min = mid;
}
else
{
[result appendString:@"0"];
max = mid;
}
}
return [NSString stringWithString:result];
}
@implementation CLLocation(Geohash)
- (NSString *)geohash
{
int cutoff = 15;
NSString *base32_characters = @"0123456789bcdefghjkmnpqrstuvwxyz";
NSString *bin_lat, *bin_lng;
NSMutableString *bin_packed, *result;
bin_lat = generate_binary([self coordinate].latitude, 90.0, -90.0, cutoff);
bin_lng = generate_binary([self coordinate].longitude, 180.0, -180.0, cutoff);
bin_packed = [NSMutableString string];
for (int i = 0; i < [bin_lat length]; ++i)
{
[bin_packed appendFormat:@"%c%c", [bin_lng characterAtIndex:i], [bin_lat characterAtIndex:i]];
}
result = [NSMutableString string];
// extract by 5-bit.
for (int i = 0; i < [bin_packed length] / 5; ++i)
{
NSUInteger index;
index = binary_to_int([bin_packed substringWithRange:NSMakeRange(i * 5, 5)]);
[result appendFormat:@"%c", [base32_characters characterAtIndex:index]];
}
return result;
}
+ (CLLocation *)locationFromGeohash:(NSString *)aGeohash
{
NSString *base32_characters = @"0123456789bcdefghjkmnpqrstuvwxyz";
NSMutableString *bin_packed, *bin_lat, *bin_lng;
bin_packed = [NSMutableString string];
for (NSUInteger i = 0; i < [aGeohash length]; ++i)
{
NSString *character;
character = [[NSMutableString stringWithFormat:@"%c", [aGeohash characterAtIndex:i]] lowercaseString];
for (NSUInteger j = 0; j < [base32_characters length]; ++j)
{
if ([character isEqualToString:[NSString stringWithFormat:@"%c", [base32_characters characterAtIndex:j]]])
{
NSMutableString *binary;
binary = [NSMutableString stringWithFormat:@"%@", int_to_binary(j)];
NSUInteger length = [binary length];
for (NSUInteger k = 0; k < 5 - length; ++k)
{
[binary insertString:@"0" atIndex:0];
}
[bin_packed appendString:binary];
break;
}
}
}
bin_lat = [NSMutableString string];
bin_lng = [NSMutableString string];
for (NSUInteger i = 0; i < [bin_packed length]; ++i)
{
if (i % 2)
{
// a latitude is composed of odd bits.
[bin_lat appendFormat:@"%c", [bin_packed characterAtIndex:i]];
}
else
{
// a longitude is composed of even bits.
[bin_lng appendFormat:@"%c", [bin_packed characterAtIndex:i]];
}
}
return [[CLLocation alloc] initWithLatitude:parse_binary(bin_lat, 90.0, -90.0)
longitude:parse_binary(bin_lng, 180.0, -180.0)];
}
@end
NSStringでかなり遅いcharacterAtIndexを使いまくっている。
参考
Geohash – Wikipedia, the free encyclopedia
GeoHashのdecodeのアルゴリズムの解説します & ScalaのGeoHashライブラリを作ってみました(仮) – ゆろよろ日記
- Comments: 0
- Trackbacks: 0
Delaunay Tessellation for iPhone
- 2010-01-13 (Wed)
- Cocoa
以下の文章はDelaunay Tessellation for iPhone | アクトインディ技術部隊報告書と同様のものです。
プログラムプレゼンテーションの第2回は、iPhoneを用いてDelaunay分割をするというものにしてみました。
本当は他のものをやるつもりだったのですが、前回のプログラムプレゼンテーションではkomagataさんがObjective-Cのキメラさ、カオスさをおもしろがっていたと勝手に仮定して、Objective-C++でさらなるカオスさを目指すことにしました。
そんな折りに以前ドロネー分割をするためのプログラムをつくって、アップロードしようかなといっていたのを思い出しました。
ということで、今回はそれを流用することにしました。
単純に移植するだけだから簡単だろと思ったところ、意外と手直しに時間がかかってしまいました。
ドロネー分割については計算幾何学で出てくるものですが、この余白はそれを書くのにはせますぎるということで、ググればきっとわかる!!!
ソースコードはこちらです。
ライブラリとしてBoost C++ Librariesを使用しています。
実行すると、毎回ランダムな10点を作成し、下図のようにドロネー分割された図が表示されます。
右側の図は外接円も表示させた場合です。
ドロネー分割では生成された三角形によって作られる外接円の中に、その三角形以外の点が含まれてはいけません。
ランダムな点を生成する部分にはboost::randomを用い、ドロネー分割を計算する部分はC++で書かれています。
Objective-CとC++のコードを混ぜる場合の注意点は、HMDTさんによくまとめられているのでこちらを参照していただくのがよいかと思います。
HMDT – Objective-C++
Objective-C++のソースを書かれるときは、.mとなっている拡張子を.mmにしてくださいね。
自分はこれをしないでObjective-C++のファイルだと認識されずはまりまくりました。
こんな風にC++のコードも簡単に使用することができるので、Objective-Cを使われている皆様もぜひ組み合わせてみてはいかがでしょうか???
P.S.
本当はNSOperationを使って、スレッドを新しく作ってバックグラウンドで処理をしたかったのですが、プログラムの構造上面倒なのでやめました。
参考
Objective-C プログラミング言語:C++ と Objective-C の併用
HMDT – Objective-C++
- Comments: 0
- Trackbacks: 0
複数行にわたったテキストを含むUILabelを適切な高さに調節する。
- 2010-01-09 (Sat)
- Cocoa
複数行にわたったテキストを表示するためのUILabelのインスタンスであるlabelがあり、そのlabelを適切なサイズ高さに変更したい時がありました。
// すでに以下の2行は他の部分で設定されている。 [label setLineBreakMode:UILineBreakModeWordWrap]; [label setNumberOfLines:0]; CGRect *frame; frame = [label frame]; frame.size = CGSizeMake(WIDTH, 0); [label setFrame:frame]; [label setText:@"long long long text..."]; [label sizeToFit];
ポイントはnumberOfLinesを0にすることと、sizeToFitメッセージを呼び出すこと。
numberOfLinesを0にしないと、sizeToFitを呼び出した時点で、指定した幅が無視されてしまう気がする。
毎回frameの幅を決めているのは、テキストが何度も書き換わる場所だから。
テキストが短いものになった場合、sizeToFitメッセージで短い幅が設定されてしまうと以降ずっとその短い幅になったままになってしまうため。
でもAppleのドキュメントには、以下のようにsizeToFitメッセージはnumberOfLinesを考慮するって書いてある。
自分のやり方がおかしいのか?
- 「UILabel Class Reference」より抜粋
-
When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.
- 引用元: UILabel Class Reference
- Comments: 0
- Trackbacks: 0
RSpecでMacro?を書く。
- 2010-01-08 (Fri)
- Ruby
RSpecを使ってSpecを書いていると何度も同じことを書くことが多いです。
たとえばパラメータに応じてページタイトルを変えたい場合とか、validationがきちんと行われているかとか。。。
毎回、 it “should have hoge” do … end と書いているのは大変です。
RSpecではいくつかの方法を使用して、もっと簡単に書くことができます。
クラスメソッドを使う
一つのspecファイルで何度も使うものであればクラスメソッドにします。
[$RAILS_ROOT/spec/views/articles/index.html.erb_spec.rb]
describe "/articles" do
def self.it_should_have_tag(selector, content)
it "should have '#{content}' at '#{selector}'" do
response.should(have_tag(selector, content))
end
end
end
と書いておけば、以下のような書き方が可能になります。
[$RAILS_ROOT/spec/views/articles/index.html.erb_spec.rb]
describe "/articles" do
def self.it_should_have_tag(selector, content)
it "should have '#{content}' at '#{selector}'" do
response.should(have_tag(selector, content))
end
end
it_should_have_tag('tr>td', 'MyString')
end
it_should_have_tag(’tr>td’, ‘MyString’) の部分だけで適切なspecに展開されます。
1行で簡潔に書けますので、理解する時間も短くてすみます。
自分はページタイトル、meta description、meta keywords、RSSのスペックに以下のコードを使用しています。
[$RAILS_ROOT/spec/views/articles/index.html.erb_spec.rb]
describe "/articles" do
def self.it_should_have_tag(selector, content)
it "should have '#{content}' at '#{selector}'" do
response.should(have_tag(selector, content))
end
end
def self.it_should_have_page_title(title)
it_should_have_tag('head>title', title)
end
def self.it_should_have_meta_description(meta_description)
it_should_have_tag('head>meta[name=description][content=?]', meta_description)
end
def self.it_should_have_meta_keywords(meta_keywords)
it_should_have_tag('head>meta[name=keywords][content=?]', meta_keywords)
end
def self.it_should_have_rss_link(link)
it_should_have_tag('head>link[rel=alternate][type=application/rss+xml][href=?]', link)
end
end
モジュールに追い出す
さらによく使う項目、上記のhtmlに関するspecなどはたくさんのところに書かれるであろうことが推測されます。
そういう場合には、モジュールに追い出してしまうということが可能だということを同僚のquekさんに教えていただきました。1
$RAILS_ROOT/spec/support フォルダにモジュールのファイルを書きます。
上記の例であれば
[$RAILS_ROOT/spec/support/tag_macros.rb]
module TagMacros
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def it_should_have_tag(selector, content)
it "should have '#{content}' at '#{selector}'" do
response.should(have_tag(selector, content))
end
end
def it_should_have_page_title(title)
it_should_have_tag('head>title', title)
end
def it_should_have_meta_description(meta_description)
it_should_have_tag('head>meta[name=description][content=?]', meta_description)
end
def it_should_have_meta_keywords(meta_keywords)
it_should_have_tag('head>meta[name=keywords][content=?]', meta_keywords)
end
def it_should_have_rss_link(link)
it_should_have_tag('head>link[rel=alternate][type=application/rss+xml][href=?]', link)
end
end
end
使用する際には include TagMacros を書いておきます。
[$RAILS_ROOT/spec/views/articles/index.html.erb_spec.rb]
describe "/articles" do
include TagMacros
it_should_have_tag('tr>td', 'MyString')
end
自分のような include 〜 を書くのもものぐさだという人は、 $RAILS_ROOT/spec/spec_helper.rb に config.include メソッドを使用すれば、自動的に読み込まれるようになります。
[$RAILS_ROOT/spec/spec_helper.rb]
Spec::Runner.configure do |config| # ... config.include(TagMacros, :type => [:views]) # ... end
上記のように :type を指定すれば view だけで読み込まれるというような指定も可能です。
参考
Railscasts – RSpec Matchers & Macros
Writing Macros in RSpec • Blog Archive • Ben Mabey
P.S.
参考にしたサイトではMacroと書いてありますが、どうもこれをMacroと読んでいいものなのかどうかかなり謎なので、タイトルはMacro?と?付きにしてみましたwww
- quekさんによると、下の「参考」のところに書いた Railscasts に同様のことが書いてあり、そのアドレスを過去の自分は社内のチケットに書いていたらしいのですが、モジュールに出すことは全く知りませんでしたwww [↩]
- Comments: 0
- Trackbacks: 0
Grand Central Dispatchを試してみる。
- 2009-09-02 (Wed)
- Programming
Snow LeopardではOSの基礎部分にかなりの改良を加えたと、Appleはいいます。
その基礎部分の改良の目玉機能の一つであるGrand Central Dispatchは、マルチコアでのプログラミングを簡単に行うためのフレームワークを提供してくれます。
マルチコアでのプログラミングはいろいろと難しいことが多く自分は興味を持ったので、プログラムプレゼンテーションのネタに少し試してみることにしました。
すでに最初の一歩を踏み出された方がいます。
Grand Central Dispatch at mootoh.log
Grand Central Dispatch をためす at mootoh.log
Grand Central Dispatchを試してみる – 理想未来はどうなった?
dispatch_asyncというのは非同期に与えたブロックを実行するもので、
対してdispatch_syncというものは同期的にブロックを実行するものようです。
この同期、非同期というものを自分はきちんと理解していないのですが、Concurrency Programming Guildeというドキュメントによると。。。
- dispatch_async
-
Submits a block for asynchronous execution on a dispatch queue and returns immediately.
- dispatch_sync
-
Submits a block object for execution on a dispatch queue and waits synchronously until that block completes.
- 引用元: Grand Central Dispatch (GCD) Reference
dispatch_syncはブロックが終了するまで待ってるよという風に見えます。
上記のサンプルを参考にしつつ、ためしにこんなコードを書いてみました。
#include <dispatch/dispatch.h>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
void c_hello(int count = 10)
{
void (^hello)(int) = ^(int x)
{
printf("Hello, C World! %d\n", x);
};
for (int i = 0; i < count; ++i)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
hello(i);
});
}
}
void cpp_hello(int count = 10)
{
void (^hello)(int) = ^(int x)
{
std::cout << "Hello, C++ World! " << x << std::endl;
};
for (int i = 0; i < count; ++i)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
hello(i);
});
}
}
int main (int argc, char * const argv[])
{
c_hello(1000);
cpp_hello(1000);
std::cout << "done!" << std::endl;
// sleep がないと処理が終了する前に return してしまう。
sleep(5);
return 0;
}
カリー化はブロックをブロックでくくればいいのでしょうか?
わざとCのprintfとC++のstd::coutの両方を書いてみました。
面白かったのが1000回ループをさせたときでした。
10回では起こりませんでしたが、回数が増えるにつれ、C++の方はこんな風にごちゃ混ぜになりました。
Hello, C++ World! Hello, C++ World! Hello, C++ World! 974975976977978
ちょっと考えてみると、<< 演算子で何個ものメソッドが入り組んでいる分、多数のスレッドが起動されるとこうなるのでしょう。
実際にプログラムプレゼンテーションで見せて、なにか簡単ないいサンプルがないと話してみたところ、g000001さんに疑似並行処理 どう書く?orgという課題をやってみてはどうか?というアドバイスをいただきました。
これがその解答になるかと思います。
#include <dispatch/dispatch.h>
#include <stdio.h>
#include <unistd.h>
int main (int argc, char * const argv[])
{
void (^alphabet)(int x) = ^(int x)
{
putchar('a' + x);
putchar(' ');
};
void (^number)(int x) = ^(int x)
{
printf("%d ", x);
};
for (int i = 0; i < 26; ++i)
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{ alphabet(i); });
dispatch_async(queue, ^{ number(i); });
}
printf("done!\n");
sleep(5);
return 0;
}
結果は次のようになって、アルファベットと数字が無事混ざって表示されました。
done! a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10 l 11 m 12 n 13 o 14 p 15 q 16 r 17 s 18 t 19 u 20 v 21 w 22 x 23 y 24 z 25
これからは共有リソースをどうやって扱うことができるのか調べたいところです。
P.S.
スレッド関連の用語を知らないので、適当に書いたところが多々あります。
うまく説明できないのが非常にもどかしいところです。
- Comments: 0
- Trackbacks: 0
ssl_requirement pluginでssl_required(:all)と書きたい!
- 2009-08-27 (Thu)
- Ruby
Ruby on RailsでSSLで通信する必要があるページには、DHH謹製らしい?ssl_requirement pluginを使用すると、以下のようにに簡単にhttpsにリダイレクトしてくれるようにできます。
class Hoge < ApplicationController
ssl_required(:index) # インデックスページだけSSLで通信する。
def index
# some crazy codes ...
end
def show
# some rotten codes ...
end
end
残念ながら、このssl_requiredはアクションに:allとか書いてコントローラ全部を対象にするというようなことができません。
allというアクションを定義する場合もあるからこうなっているのかもしれません。
コントローラ全体にSSL通信をさせるようにするための方法については、コントローラ全体をssl強制に – 車輪の再発明というサイトに、trueを返すssl_required?メソッドを定義する方法が書かれています。
現在書いているRailsアプリでも同様のことが行われているものの、いかんせんたびたび見る気がしてきて、クラスメソッドにしようかと思いました。
でも、すでにssl_requiredというメソッドがあるところに、さらなる同じようなメソッドを作るのは避けたいし、やっぱりssl_required(:all)と書きたいと思いました。
そこでalias_method_chainを使用して、ApplicationControllerをこんな風にしてみました。
class ApplicationController < ActionController::Base
include SslRequirement
# some codes...
class << self
def ssl_required_with_all(*actions)
if actions.include?(:all)
class_eval do
def ssl_required?
true
end
end
else
class_eval do
ssl_required_without_all(*actions)
end
end
end
alias_method_chain(:ssl_required, :all)
end
end
まともにclass_evalとかがわかっていないので、かなり怪しいです!!!
- Comments: 0
- Trackbacks: 0
Apple Push Notification Serviceを利用した、iPhone クライアントと、Rubyによるサーバの作成。
[とりあえず表示しましたが、これからコードをアップロードしたり改変していきます。]
先週から、システム部で何か動くものを作って発表するという企画?が始まりました。
プログラムプレゼンテーションを始めてみたら意外に良かった
第一弾は自分だったので、1週間しかなかったので、ちょこっとやればできそうで今までやってみたかったiPhone OS 3.0の目玉機能の一つ、Push Notification Serviceの実装をやってみることにしてみました。
iPhoneクライアントはほぼObjective-Cしかありませんのでそれを使い、サーバはRubyで実装しました。
以下が発表したスライドを少し改変したものになります。
コードは今回はbitbucket.orgにアップロードすることにしました!
コードはこちらになります。
http://bitbucket.org/milkcocoa/apns-test/
ただしサーバのファイルに関しては
- デバイスIDとかの部分はxでマスクしています。
- キーに関してはスライドで指定した方法で記述してください。
[ポイントを書く。]
- Comments: 0
- Trackbacks: 2
mislav-will_paginate pluginを使用したページをRSpecでテストする。
- 2009-07-19 (Sun)
- Ruby
Webアプリケーションでは、たくさんの項目を一覧表示する場合、1ページに全部見せるのではなくpaginate処理をして数ページに分ける場合があります。
たとえばGoogleの検索結果は通常、1ページに10件しか表示されません。
そういうページをRuby on Railsで作成する場合には、mislav-will_paginate pluginというプラグインを使用するということは割とよく知られていることです。
先日もそういうページを作成する必要があり、作成していましたが、RSpecでテストを作成しようとしたところやり方がわからず詰まりました。
そのため、その方法をここに記しておこうと思います。
ドキュメントを読もう!
実はGitHubのpluginのWikiページに最低限必要なことは書いてありました。
気がついていればもっと早く解決していたところでした。
「I’m getting “undefined method `total_pages’” error when rendering in the view!」という部分ですね。
ポイントはpaginateやtotal_pagesというメソッドをstub!で定義してあげることです。
実際のコード
- Controller: ArticlesController
- Model: Article
- title: string
- body: text
Controllerのテスト(の一部)
[$RAILS_ROOT/spec/controllers/articles_controller_spec.rb]
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe ArticlesController do
describe "handling GET /articles" do
before(:each) do
article = mock_model(Article)
article.stub!(:find).and_return([article])
@articles = [article]
@articles.stub!(:total_pages).and_return(1)
@articles.stub!(:paginate).and_return(@articles)
end
def do_get
get(:index)
end
it "should be successful" do
do_get
response.should be_success
end
it "should render index template" do
do_get
response.should render_template('index')
end
it "should find all articles" do
Article.should_receive(:paginate).with(:page => nil, :per_page => 100).and_return(@articles)
do_get
end
it "should assign the found articles for the view" do
do_get
assigns[:articles].should == @articles
end
end
end
ちなみに29行目の:page => nilになっているのは、たぶんassingsとかでparameterを指定していないからこうなるのだと思います。
まだshould_receiveをきちんと理解していないで、まともな説明が書けません。
Viewのテスト(の一部)
[$RAILS_ROOT/spec/views/articles/index.html.erb_spec.rb]
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe "/articles/index.html.erb" do
include ArticlesHelper
before(:each) do
article_98 = mock_model(Article)
article_98.stub!(:id).and_return(1)
article_98.stub!(:title).and_return('MyString')
article_98.stub!(:body).and_return('MyText')
article_99 = mock_model(Article)
article_99.stub!(:id).and_return(2)
article_99.stub!(:title).and_return('MyString')
article_99.stub!(:body).and_return('MyText')
@articles = [article_98, article_99]
@articles.stub!(:total_pages).and_return(1)
@articles.stub!(:paginate).and_return(@articles)
assigns[:articles] = @articles
end
it "should render list of articles" do
render "/articles/index.html.erb"
response.should have_tag("tr>td", "MyString", 2)
response.should have_tag("tr>td", "MyText", 2)
end
end
- Comments: 0
- Trackbacks: 0
Home > Programming



