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
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
Amazon Qを使ってIaCを触ろう!
maruto
0
400
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
CSC509 Lecture 12
javiergs
PRO
0
160
役立つログに取り組もう
irof
28
9.6k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
Arm移行タイムアタック
qnighy
0
320
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Featured
See All Featured
Statistics for Hackers
jakevdp
796
220k
Teambox: Starting and Learning
jrom
133
8.8k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Why Our Code Smells
bkeepers
PRO
334
57k
Building Applications with DynamoDB
mza
90
6.1k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
How STYLIGHT went responsive
nonsquared
95
5.2k
Happy Clients
brianwarren
98
6.7k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
89
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