Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kaminari

 Kaminari

ページャNight の発表 "ふつうのページャーをつくろう"の発表スライド http://www.zusaar.com/event/5477013

76a777ff80f30bd3b390e275cce625bc?s=128

Akira Matsuda

July 04, 2014
Tweet

Transcript

  1. None
  2. begin

  3. amatsuda/kaminari

  4. ͜Ε͸Կʁ Ruby on RailsͰ ϖʔδωʔγϣϯ͢Δ΍ͭ

  5. Ͱ͖Δ͜ͱ ☇DBΫΤϦ΁ͷϖʔδ൪߸ͱ 1ϖʔδ͋ͨΓ݅਺ʹΑΔ ৚݅௥Ճ ☇ϖʔδωʔγϣϯϦϯΫͷ ඳը

  6. ྺ࢙ ☇1st commit: 2011/02/05 ☇1st release: 2011/02/05

  7. Railsͷϖʔδϟʔͷมભ ☇Rails 1.x: ඪ४ఴ෇࣌୅ ☇ActionController::Pagination ☇Rails 2.x: ໺ྑϓϥάΠϯ࣌୅ ☇classic_pagination ☇will_paginate

    ☇paginating_ nd
  8. AC::Pagination ☇Railsඪ४ఴ෇ (ͦΜͳ࣌୅΋ ͋Γ·ͨ͠Ͷ) ☇࢖͍ํ͕೉ͯ͘͠ͳ͔ͳ͔ ֮͑ΒΕͳ͍ ☇஗͍͜ͱͰఆධ

  9. AC::Pagination

  10. "Automatic pagination for every action in a controller" class PersonController

    < ApplicationController model :person paginate :people, :order => 'last_name, rst_name', :per_page => 20 # ... end
  11. AC::Pagination ☇φκͷએݴతDSL

  12. "Pagination for a single action" def list @person_pages, @people =

    paginate :people, :order => 'last_name, rst_name' end
  13. AC::Pagination ☇ϦϯΫඳը༻ͷΦϒδΣΫτ ͱݕࡧ݁Ռͷ഑ྻͷ2ͭͷΦϒ δΣΫτΛฦ͢API

  14. AC::Paginationͷઃܭࢥ૝ ☇ϖʔδωʔγϣϯ͸ ίϯτϩʔϥʔͷ੹຿Ͱ͋Δ

  15. will_paginateͷొ৔ “I WILL PAGINATE” http://errtheblog.com/posts/47- i-will-paginate

  16. will_paginate ☇PJ & defunkt

  17. 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"
  18. will_paginateͷઃܭࢥ૝ ϖʔδωʔγϣϯ͸ ίϯτϩʔϥʔ͚ͩͷ੹຿ Ͱ͸ͳ͍ͷͰ͸ʁ

  19. ࠷ॳظͷREADMEΑΓ Quick quiz: Where does pagination logic belong? a) in

    the model; b) in the controller; c) in views; d) all of the above.
  20. ARϞσϧʹpaginateϝιου @posts = Post.paginate :page => params[:page], :per_page => 50

  21. ϏϡʔͰ͸͜ͷݕࡧ݁ՌΦϒδΣΫτ ͕ϦϯΫͷඳըʹ࢖ΘΕΔ <%= will_paginate @posts %>

  22. will_paginateͷ࢖͍উख ݕࡧ͸ϞσϧͰɺσʔλͷड͚ ౉͠͸1ݸͷΦϒδΣΫτͰɺ ϦϯΫͷඳը͸Ϗϡʔͷ ϔϧύʔϝιουͰɺͱ੹຿Λ ෼͚ͨ͜ͱʹΑΓɺγϯϓϧͳ APIΛ࣮ݱ

  23. will_paginateͷઃܭࢥ૝ ϖʔδωʔγϣϯ͸ M, V, CͷશͯʹؔΘΔ

  24. will_paginateͷ࢖͍উख (2) φϏήʔγϣϯϦϯΫ͸Ruby Ͱॻ͔Εͨϔϧύʔϝιουͷ தͰจࣈྻΛ૊ཱͯͯΔͷͰɺ ඇৗʹΧελϚΠζ͕͠ʹ͘ ͍ɺͱ͍͏͔Ͱ͖ͳ͍

  25. Railsͷϖʔδϟʔͷมભ ☇Rails 1.x: ඪ४ఴ෇࣌୅ ☇Rails 2.x: ໺ྑϓϥάΠϯ࣌୅ ☇Rails 3.0: Railsͷେվ଄

    ☇Կ΋͔΋ಈ͔ͳ͘ͳͬͨ…
  26. Rails 3.0࣌୅ ☇ Ralis 2.3 -> 3.0ͷϝδϟʔόʔδϣϯ ΞοϓͰRailsͷ಺෦ߏ଄͕େ͖͘มԽ ☇ Rails

    2࣌୅ͷϓϥάΠϯ͕͍Ζ͍Ζࢮ๢ ☇ ۀ຿Ͱ։ൃ͍ͯͨ͠Rails 3.0ͷΞϓϦͰ ಈ࡞͢ΔϖʔδωʔγϣϯϥΠϒϥϦ͕ ͳ͔ͬͨ ☇ …ͷͰ͔ͨ͠ͳࣗ͘෼Ͱ࡞ͬͨ
  27. ͳͥRails 2Ͱ࢖ͬͯͨطଘϥΠϒϥϦ ΁ͷύονʹ͠ͳ͔ͬͨͷ͔ʁ ☇๭will_p͕Ϋιίʔυ͗ͯ͢͜Ε ͍ͫΔͷϚδແཧͬͯࢥͬͨ ☇Rails 3ͷ৽ػೳͨͪΛ࢖ͬͨ ৽͍͠ΠσΟΦϜΛࢥ͍͍ͭͯ ͠·ͬͨ

  28. Rails 3࣌୅ͷΠσΟΦϜ ☇RelationΛ͏·͘࢖͏ ☇Engineʹด͡ࠐΊΔ

  29. ࠷ॳظ࣮૷ ☇https://github.com/amatsuda/ kaminari/tree/v0.1.0

  30. Ϟσϧଆͷ࣮૷

  31. 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
  32. Ϟσϧଆͷػೳ ☇ ARͷϞσϧͦΕͧΕʹରͯ͠page ͱ͍͏໊લͷscopeΛఆٛ ☇ pageͰ͸limitͱoffsetͷ஋Λࢉग़ͯ͠ ΫΤϦΛ૊ΈཱͯΔ ☇ page͸perͱ͔current_pageͱ͔ͷϝ ιου͕ੜ͑ͨ݁ՌΦϒδΣΫτΛฦ͢

  33. ͜ΜͳAPI

  34. ϖʔδ൪߸ʹΑΔߜΓࠐΈ User.page(7)

  35. 1ϖʔδ͋ͨΓͷ݅਺ͷࢦఆ User.page(7).per(50)

  36. ϖʔδωʔγϣϯϦϯΫͷඳը

  37. ඞཁͳ৘ใ ☇૯݅਺ ☇1ϖʔδ͋ͨΓͷ݅਺ ☇ݱࡏͷϖʔδ൪߸

  38. ಋग़Ͱ͖Δ৘ใ ☇૯ϖʔδ਺ ☇࠷ॳͷϖʔδ ☇࠷ޙͷϖʔδ ☇લͷϖʔδ ☇࣍ͷϖʔδ

  39. Ϗϡʔଆͷ࣮૷ ☇Rails Enginesͷ࢓૊ΈΛར༻

  40. Rails Engines ☇Rails 3Ͱಋೖ͞Εͨ৽ػೳ ☇Ϟσϧͱ͔ίϯτϩʔϥʔͱ͔ Ϗϡʔͱ͔con gͱ͔ΛؚΉ͜ͱ ͕Ͱ͖ΔϛχRailsΞϓϦΈ͍ͨ ͳ΋ͷΛ਌ΞϓϦʹόϯυϧ ͢Δ࢓૊Έ

  41. Ϗϡʔଆͷ࣮૷ % 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
  42. Ϗϡʔଆͷ࣮૷ ☇ ଟ਺ͷpartialςϯϓϨʔτʹখ෼͚ ͞ΕͨERB (Ruby͕ຒΊࠐΊΔHTML ςϯϓϨʔτ) ☇ ϦϯΫΛΧελϚΠζ͍ͨ͠৔߹͸ partialΛॻ͖׵͑Ε͹ྑ͍ ☇

    ैདྷͷϥΠϒϥϦͰෆຬͩͬͨϏϡʔͷ ΧελϚΠζੑͷѱ͞Λվྑͯ͠Έͨ
  43. ϦϯΫ ☇ ࣍ͷϖʔδɺલͷϖʔδɺ࠷ॳͷϖʔ δɺ࠷ޙͷϖʔδɺ͋ͱϖʔδ൪߸ͷ ਺ࣈ ☇ ભҠઌͷϖʔδ൪߸ΛΫΤϦύϥϝʔ λʔʹ෇Ճͭͭ͠GETͰݱࡏϖʔδʹ ભҠ ☇

    AjaxରԠ
  44. Dave Thomasʹ๙ΊΒΕͨʂ

  45. the admin side of our store our store == pragprog.com?

    (!!!)
  46. ͦͷଞͷಛ௃ ☇ArrayͳͲΛԚછ͠ͳ͍ ☇ແઃఆͰ࢖͑Δ ☇Agnostic

  47. Agnosticͱ͸ ☇Rails 3+ͷಛ௃ ☇Framework agnosticɹ ☇͍ΖΜͳϥΠϒϥϦͱ͔ʹ ରԠͯ͠ΔΑ͍ͬͯ͏͜ͱ ☇Kaminariʹ΋Өڹ

  48. ͍ΖΜͳϥΠϒϥϦʹରԠ ☇DataMapperͱ͔Mongoͱ͔ Sinatraͱ͔࢖ͬͯΔਓ͔Β ͍Ζ͍Ζ΀ΔΓ͕དྷΔ ☇ΦϨAR͔͠࢖ͬͯͳ͍Μ ͚ͩͲ…

  49. Agnostic ☇Α͘Θ͔Μͳ͍͚Ͳɺಈ͍ͯͦ͏ ͩͬͨΒདྷͨ΋ͷ͸શͯऔΓࠐΜͰ Έͨ ☇࢓༷ͷංେԽ ☇=> ϝϯςͭΒ͍… ☇=> CIʹ͍͔͔ͬ࣌ؒ͢͝Δ

  50. ݱࡏ࣮ߦ͞Ε͍ͯΔ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
  51. ݱࡏ࣮ߦ͞Ε͍ͯΔCI …ͷϚτϦοΫε

  52. Sinatraͱ͔શ͘࢖ͬͯͳ͍͔ ΒͲ͏Ͱ΋͍͍Μ͚ͩͲ…

  53. Sinatraͱ͔શ͘࢖ͬͯͳ͍͔ΒͲ͏Ͱ ΋͍͍Μ͚ͩͲ… Sinatraαϙʔτͯͯ͠ Α͔ͬͨ͜ͱ

  54. None
  55. None
  56. ͜Ε͔Β ☇Sinatraͱ͔DataMapperͱ͔ mongoͳΜͱ͔ͱ͔ͷΞμϓ λʔΛผϥΠϒϥϦʹ෼཭ ☇όʔδϣϯ൪߸Λ1.0ʹ͢Δ ☇Coming soon!

  57. end