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.7k
Hanamiを支える技術 2023 / Asakusa.rb Hanami 2023
a_matsuda
2
530
Rails 7.1をn倍速くした話
a_matsuda
11
12k
RubyKaigi 2022 After Talk
a_matsuda
0
400
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.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
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.2k
みんなでプロポーザルを書いてみた
yuriko1211
0
280
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
200
Jakarta EE meets AI
ivargrimstad
0
230
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
Macとオーディオ再生 2024/11/02
yusukeito
0
370
Click-free releases & the making of a CLI app
oheyadam
2
120
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Six Lessons from altMBA
skipperchong
27
3.5k
How to Ace a Technical Interview
jacobian
276
23k
A better future with KSS
kneath
238
17k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
It's Worth the Effort
3n
183
27k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Designing Experiences People Love
moore
138
23k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
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