Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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.5k
Hanamiを支える技術 2023 / Asakusa.rb Hanami 2023
a_matsuda
2
500
Rails 7.1をn倍速くした話
a_matsuda
11
12k
RubyKaigi 2022 After Talk
a_matsuda
0
390
Coming Soon…💎 / coming soon
a_matsuda
12
7.7k
Rails Performance Issues and Solutions
a_matsuda
9
35k
Extending Rails for Real World App Development
a_matsuda
9
7.3k
A RubyKaigi Talk
a_matsuda
8
2.3k
Ginza Rails 1
a_matsuda
6
1.1k
Other Decks in Programming
See All in Programming
UnJSで簡単に始めるCLIツール開発 / cli-tool-development-with-unjs
aoseyuu
2
300
ポケモンで考えるコミュニケーション / Communication Lessons from Pokémon
mackey0225
4
170
Kotlin Multiplatform at Stable and Beyond (Kotlin Vienna, October 2024)
zsmb
2
360
"Swarming" をコンセプトに掲げるアジャイルチームのベストプラクティス
boykush
2
250
sqlcを利用してsqlに型付けを
kamiyam
0
240
Quarto Clean Theme
nicetak
0
220
MLOps in Mercari Group’s Trust and Safety ML Team
cjhj
1
120
CSC509 Lecture 04
javiergs
PRO
0
140
Vue :: Better Testing 2024
up1
1
400
色んなオートローダーを覗き見る #phpcon_okinawa
o0h
PRO
5
400
pytest プラグインを開発して DRY に自動テストを書こう
inuatsu
2
260
Integrating AI in Your Enterprise Java Applications
ivargrimstad
0
140
Featured
See All Featured
The Mythical Team-Month
searls
218
43k
KATA
mclloyd
27
13k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2k
Unsuck your backbone
ammeep
668
57k
Raft: Consensus for Rubyists
vanstee
136
6.6k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
27
1.9k
Art, The Web, and Tiny UX
lynnandtonic
296
20k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
1
290
Creatively Recalculating Your Daily Design Routine
revolveconf
217
12k
Fireside Chat
paigeccino
32
3k
Web Components: a chance to create the future
zenorocha
310
42k
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