Location via proxy:
[ UP ]
[Report a bug]
[Manage cookies]
No cookies
No scripts
No ads
No referrer
Show this form
$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kaminari
Search
Akira Matsuda
July 04, 2014
Programming
20
10k
Kaminari
ページャNight <[1]> の発表 "ふつうのページャーをつくろう"の発表スライド
http://www.zusaar.com/event/5477013
Akira Matsuda
July 04, 2014
Tweet
Share
More Decks by Akira Matsuda
See All by Akira Matsuda
初心者のためのRubyKaigi入門/RubyKaigi Introduction
a_matsuda
13
4.7k
Hanamiを支える技術 2023 / Asakusa.rb Hanami 2023
a_matsuda
2
540
Rails 7.1をn倍速くした話
a_matsuda
11
12k
RubyKaigi 2022 After Talk
a_matsuda
0
410
Coming Soon…💎 / coming soon
a_matsuda
12
7.8k
Rails Performance Issues and Solutions
a_matsuda
9
35k
Extending Rails for Real World App Development
a_matsuda
9
7.4k
A RubyKaigi Talk
a_matsuda
8
2.3k
Ginza Rails 1
a_matsuda
6
1.2k
Other Decks in Programming
See All in Programming
Develop iOS apps with Neovim / vimconf_2024
uhooi
1
110
Contemporary Test Cases
maaretp
0
140
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
3
340
Jakarta EE meets AI
ivargrimstad
0
950
Jakarta EE meets AI
ivargrimstad
0
1k
Jakarta EE meets AI
ivargrimstad
0
520
みんなでプロポーザルを書いてみた
yuriko1211
0
290
Reckoner における Datadog Browser Test の活用事例 / Datadog Browser Test at Reckoner
nomadblacky
0
160
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
270
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2.2k
Java 23の概要とJava Web Frameworkの現状 / Java 23 and Java web framework
kishida
2
310
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
3
1.3k
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
390
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
A Philosophy of Restraint
colly
203
16k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Music & Morning Musume
bryan
46
6.2k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Transcript
None
begin
amatsuda/kaminari
͜ΕԿʁ Ruby on RailsͰ ϖʔδωʔγϣϯ͢Δͭ
Ͱ͖Δ͜ͱ ☇DBΫΤϦͷϖʔδ൪߸ͱ 1ϖʔδ͋ͨΓ݅ʹΑΔ ݅Ճ ☇ϖʔδωʔγϣϯϦϯΫͷ ඳը
ྺ࢙ ☇1st commit: 2011/02/05 ☇1st release: 2011/02/05
Railsͷϖʔδϟʔͷมભ ☇Rails 1.x: ඪ४ఴ࣌ ☇ActionController::Pagination ☇Rails 2.x: ྑϓϥάΠϯ࣌ ☇classic_pagination ☇will_paginate
☇paginating_ nd
AC::Pagination ☇Railsඪ४ఴ (ͦΜͳ࣌ ͋Γ·ͨ͠Ͷ) ☇͍ํ͕ͯ͘͠ͳ͔ͳ͔ ֮͑ΒΕͳ͍ ☇͍͜ͱͰఆධ
AC::Pagination
"Automatic pagination for every action in a controller" class PersonController
< ApplicationController model :person paginate :people, :order => 'last_name, rst_name', :per_page => 20 # ... end
AC::Pagination ☇φκͷએݴతDSL
"Pagination for a single action" def list @person_pages, @people =
paginate :people, :order => 'last_name, rst_name' end
AC::Pagination ☇ϦϯΫඳը༻ͷΦϒδΣΫτ ͱݕࡧ݁Ռͷྻͷ2ͭͷΦϒ δΣΫτΛฦ͢API
AC::Paginationͷઃܭࢥ ☇ϖʔδωʔγϣϯ ίϯτϩʔϥʔͷͰ͋Δ
will_paginateͷొ “I WILL PAGINATE” http://errtheblog.com/posts/47- i-will-paginate
will_paginate ☇PJ & defunkt
PHPͷΠέͯΔ͔ͭΒύΫͬͨ ☇"Rails pagination code is really bad" ☇"stealing some PHP
code" ☇"I nd Digg-style Pagination" ☇"you take 120 lines of PHP and slim it down to around 40 in Ruby"
will_paginateͷઃܭࢥ ϖʔδωʔγϣϯ ίϯτϩʔϥʔ͚ͩͷ Ͱͳ͍ͷͰʁ
࠷ॳظͷREADMEΑΓ Quick quiz: Where does pagination logic belong? a) in
the model; b) in the controller; c) in views; d) all of the above.
ARϞσϧʹpaginateϝιου @posts = Post.paginate :page => params[:page], :per_page => 50
ϏϡʔͰ͜ͷݕࡧ݁ՌΦϒδΣΫτ ͕ϦϯΫͷඳըʹΘΕΔ <%= will_paginate @posts %>
will_paginateͷ͍উख ݕࡧϞσϧͰɺσʔλͷड͚ ͠1ݸͷΦϒδΣΫτͰɺ ϦϯΫͷඳըϏϡʔͷ ϔϧύʔϝιουͰɺͱΛ ͚ͨ͜ͱʹΑΓɺγϯϓϧͳ APIΛ࣮ݱ
will_paginateͷઃܭࢥ ϖʔδωʔγϣϯ M, V, CͷશͯʹؔΘΔ
will_paginateͷ͍উख (2) φϏήʔγϣϯϦϯΫRuby Ͱॻ͔Εͨϔϧύʔϝιουͷ தͰจࣈྻΛཱͯͯΔͷͰɺ ඇৗʹΧελϚΠζ͕͠ʹ͘ ͍ɺͱ͍͏͔Ͱ͖ͳ͍
Railsͷϖʔδϟʔͷมભ ☇Rails 1.x: ඪ४ఴ࣌ ☇Rails 2.x: ྑϓϥάΠϯ࣌ ☇Rails 3.0: Railsͷେվ
☇Կ͔ಈ͔ͳ͘ͳͬͨ…
Rails 3.0࣌ ☇ Ralis 2.3 -> 3.0ͷϝδϟʔόʔδϣϯ ΞοϓͰRailsͷ෦ߏ͕େ͖͘มԽ ☇ Rails
2࣌ͷϓϥάΠϯ͕͍Ζ͍Ζࢮ ☇ ۀͰ։ൃ͍ͯͨ͠Rails 3.0ͷΞϓϦͰ ಈ࡞͢ΔϖʔδωʔγϣϯϥΠϒϥϦ͕ ͳ͔ͬͨ ☇ …ͷͰ͔ͨ͠ͳࣗ͘Ͱ࡞ͬͨ
ͳͥRails 2ͰͬͯͨطଘϥΠϒϥϦ ͷύονʹ͠ͳ͔ͬͨͷ͔ʁ ☇will_p͕Ϋιίʔυ͗ͯ͢͜Ε ͍ͫΔͷϚδແཧͬͯࢥͬͨ ☇Rails 3ͷ৽ػೳͨͪΛͬͨ ৽͍͠ΠσΟΦϜΛࢥ͍͍ͭͯ ͠·ͬͨ
Rails 3࣌ͷΠσΟΦϜ ☇RelationΛ͏·͘͏ ☇Engineʹด͡ࠐΊΔ
࠷ॳظ࣮ ☇https://github.com/amatsuda/ kaminari/tree/v0.1.0
Ϟσϧଆͷ࣮
kaminari/active_record.rb (ൈਮ) kls.class_eval do scope :page, lambda {|num| offset(PER_PAGE *
([num.to_i, 1].max - 1)).limit(PER_PAGE) } do def per(num) offset(offset_value / limit_value * num).limit(num) end def num_pages (except(:offset, :limit).count.to_f / limit_value).ceil end def current_page (offset_value / limit_value) + 1 ennnnnnnd
Ϟσϧଆͷػೳ ☇ ARͷϞσϧͦΕͧΕʹରͯ͠page ͱ͍͏໊લͷscopeΛఆٛ ☇ pageͰlimitͱoffsetͷΛࢉग़ͯ͠ ΫΤϦΛΈཱͯΔ ☇ pageperͱ͔current_pageͱ͔ͷϝ ιου͕ੜ͑ͨ݁ՌΦϒδΣΫτΛฦ͢
͜ΜͳAPI
ϖʔδ൪߸ʹΑΔߜΓࠐΈ User.page(7)
1ϖʔδ͋ͨΓͷ݅ͷࢦఆ User.page(7).per(50)
ϖʔδωʔγϣϯϦϯΫͷඳը
ඞཁͳใ ☇૯݅ ☇1ϖʔδ͋ͨΓͷ݅ ☇ݱࡏͷϖʔδ൪߸
ಋग़Ͱ͖Δใ ☇૯ϖʔδ ☇࠷ॳͷϖʔδ ☇࠷ޙͷϖʔδ ☇લͷϖʔδ ☇࣍ͷϖʔδ
Ϗϡʔଆͷ࣮ ☇Rails EnginesͷΈΛར༻
Rails Engines ☇Rails 3Ͱಋೖ͞Εͨ৽ػೳ ☇Ϟσϧͱ͔ίϯτϩʔϥʔͱ͔ Ϗϡʔͱ͔con gͱ͔ΛؚΉ͜ͱ ͕Ͱ͖ΔϛχRailsΞϓϦΈ͍ͨ ͳͷΛΞϓϦʹόϯυϧ ͢ΔΈ
Ϗϡʔଆͷ࣮ % ls app/views/kaminari _current_page.html.erb _ rst_page_link.html.erb _last_page_link.html.erb _next_link.html.erb _next_span.html.erb
_page_link.html.erb _prev_link.html.erb _prev_span.html.erb _truncated_span.html.erb
Ϗϡʔଆͷ࣮ ☇ ଟͷpartialςϯϓϨʔτʹখ͚ ͞ΕͨERB (Ruby͕ຒΊࠐΊΔHTML ςϯϓϨʔτ) ☇ ϦϯΫΛΧελϚΠζ͍ͨ͠߹ partialΛॻ͖͑Εྑ͍ ☇
ैདྷͷϥΠϒϥϦͰෆຬͩͬͨϏϡʔͷ ΧελϚΠζੑͷѱ͞Λվྑͯ͠Έͨ
ϦϯΫ ☇ ࣍ͷϖʔδɺલͷϖʔδɺ࠷ॳͷϖʔ δɺ࠷ޙͷϖʔδɺ͋ͱϖʔδ൪߸ͷ ࣈ ☇ ભҠઌͷϖʔδ൪߸ΛΫΤϦύϥϝʔ λʔʹՃͭͭ͠GETͰݱࡏϖʔδʹ ભҠ ☇
AjaxରԠ
Dave Thomasʹ๙ΊΒΕͨʂ
the admin side of our store our store == pragprog.com?
(!!!)
ͦͷଞͷಛ ☇ArrayͳͲΛԚછ͠ͳ͍ ☇ແઃఆͰ͑Δ ☇Agnostic
Agnosticͱ ☇Rails 3+ͷಛ ☇Framework agnosticɹ ☇͍ΖΜͳϥΠϒϥϦͱ͔ʹ ରԠͯ͠ΔΑ͍ͬͯ͏͜ͱ ☇KaminariʹӨڹ
͍ΖΜͳϥΠϒϥϦʹରԠ ☇DataMapperͱ͔Mongoͱ͔ Sinatraͱ͔ͬͯΔਓ͔Β ͍Ζ͍ΖΔΓ͕དྷΔ ☇ΦϨAR͔ͬͯ͠ͳ͍Μ ͚ͩͲ…
Agnostic ☇Α͘Θ͔Μͳ͍͚Ͳɺಈ͍ͯͦ͏ ͩͬͨΒདྷͨͷશͯऔΓࠐΜͰ Έͨ ☇༷ͷංେԽ ☇=> ϝϯςͭΒ͍… ☇=> CIʹ͍͔͔ͬ࣌ؒ͢͝Δ
ݱࡏ࣮ߦ͞Ε͍ͯΔCI ☇Ruby 1.8.7, 1.9.3, 2.0.0, 2.1, ruby- head, jruby, rbx-2
☇ActiveRecord 30, 31, 32, 40, 41, edge, DataMapper 12, MongoMapper, Mongoid 24, 30, 31, 40 ☇Rails֤όʔδϣϯ, Sinatra 13, 14
ݱࡏ࣮ߦ͞Ε͍ͯΔCI …ͷϚτϦοΫε
Sinatraͱ͔શͬͯ͘ͳ͍͔ ΒͲ͏Ͱ͍͍Μ͚ͩͲ…
Sinatraͱ͔શͬͯ͘ͳ͍͔ΒͲ͏Ͱ ͍͍Μ͚ͩͲ… Sinatraαϙʔτͯͯ͠ Α͔ͬͨ͜ͱ
None
None
͜Ε͔Β ☇Sinatraͱ͔DataMapperͱ͔ mongoͳΜͱ͔ͱ͔ͷΞμϓ λʔΛผϥΠϒϥϦʹ ☇όʔδϣϯ൪߸Λ1.0ʹ͢Δ ☇Coming soon!
end