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.6k
Hanamiを支える技術 2023 / Asakusa.rb Hanami 2023
a_matsuda
2
520
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.3k
A RubyKaigi Talk
a_matsuda
8
2.3k
Ginza Rails 1
a_matsuda
6
1.2k
Other Decks in Programming
See All in Programming
推し活としてのrails new/oshikatsu_ha_iizo
sakahukamaki
3
1.6k
ECSのサービス間通信 4つの方法を比較する 〜Canary,Blue/Greenも添えて〜
tkikuc
11
2.3k
僕がつくった48個のWebサービス達
yusukebe
18
17k
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
110
Synchronizationを支える技術
s_shimotori
1
150
Identifying User Idenity
moro
6
7.8k
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
240
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
220
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
330
Server Driven Compose With Firebase
skydoves
0
390
Vaporモードを大規模サービスに最速導入して学びを共有する
kazukishimamoto
4
4.3k
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
7
2.8k
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
115
6.9k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Become a Pro
speakerdeck
PRO
24
5k
Navigating Team Friction
lara
183
14k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
For a Future-Friendly Web
brad_frost
175
9.4k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
GraphQLの誤解/rethinking-graphql
sonatard
66
9.9k
Docker and Python
trallard
40
3.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
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