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

New Features in Rails 4.2

murajun1978
February 21, 2015

New Features in Rails 4.2

murajun1978

February 21, 2015
Tweet

More Decks by murajun1978

Other Decks in Programming

Transcript

  1. New Features
    in Rails 4.2
    ୈ65ճ Rubyؔ੢ ษڧձ

    View full-size slide

  2. about me
    ΉΒ͡ΎΜ
    murajun1978
    Ruby <3 <3 <3

    View full-size slide

  3. Shinosaka.rb #13
    http://shinosakarb.doorkeeper.jp/events/19518
    2 / 28

    View full-size slide

  4. ࿩͢͜ͱ
    • Active Job
    • Global ID
    • Adequate Record
    • Web Console
    • Foreign key support
    • Default Host for rails server

    View full-size slide

  5. Active Jobͱ͸ʁ
    queueing backends
    Active Job API
    Job
    adapter

    View full-size slide

  6. Job࡞੒
    $ rails generate job job_name

    View full-size slide

  7. ಛఆͷΩϡʔ΁ͷJob࡞੒
    $ rails generate job job_name —queue queue_name

    View full-size slide

  8. JobΛΩϡʔʹొ࿥

    View full-size slide

  9. JobͷεέδϡʔϦϯά

    View full-size slide

  10. ΞμϓλΛมߋ

    View full-size slide

  11. ར༻Ͱ͖ΔΞμϓλ
    http://api.rubyonrails.org/classes/ActiveJob/
    QueueAdapters.html

    View full-size slide

  12. ίʔϧόοΫ
    • before_enqueue
    • around_enqueue
    • after_enqueue
    • before_perform
    • around_perform
    • after_perform

    View full-size slide

  13. Ωϡʔʹొ࿥͞ΕΔ࣌ʹ࣮ߦ
    • before_enqueue
    • around_enqueue
    • after_enqueue

    View full-size slide

  14. Job͕࣮ߦ͞ΕΔ࣌ʹ࣮ߦ
    • before_perform
    • around_perform
    • after_perform

    View full-size slide

  15. ඇಉظͰϝʔϧૹ৴

    View full-size slide

  16. URIʹΑΔϞσϧࢀর
    https://github.com/rails/globalid

    View full-size slide

  17. Global IDͷੜ੒
    pry(main)> gid = post.to_gid
    => #>
    URI͕ੜ੒͞Εͨ
    ActiveRecordΦϒδΣΫτ

    View full-size slide

  18. Global ID͔Β
    Modelͷ৘ใΛऔಘͯ͠ΈΔ
    gid.app #=> "blog"
    ɾΞϓϦέʔγϣϯ໊
    ɾϞσϧ໊
    gid.model_name #=> "Post"
    ɾϞσϧΫϥε
    gid.model_class #=> Post(id: integer, title: string,
    published_at: datetime …)
    ɾID
    gid.model_id #=> "1"

    View full-size slide

  19. Global ID͔ΒΦϒδΣΫτऔಘ
    globalid/lib/global_id/locator.rb
    GlobalID::Locator.locate gid
    => #

    View full-size slide

  20. Adequate Record

    View full-size slide

  21. http://tenderlovemaking.com/2014/02/19/adequaterecord-pro-
    like-activerecord.html

    View full-size slide

  22. ActiveRecord
    2x faster ƅшƅ)ʂ

    View full-size slide

  23. Rails 4.1
    SELECT "posts".* FROM "posts" WHERE "posts"."id" = 1 LIMIT 1
    SELECT "posts".* FROM "posts" WHERE "posts"."id" = 2 LIMIT 1
    ৚݅ͷ஋͕มΘ͚ͬͨͩͳͷʹɺΫΤϦΩϟογϡ͞Εͯͳ͍

    View full-size slide

  24. Rails 4.2
    SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 1]]
    SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 2]]
    Ωϟογϡ͞ΕͨΫΤϦʹ஋Λ౉͍ͯ͠Δ

    View full-size slide

  25. αϙʔτ͞Ε͍ͯΔ΋ͷ
    • find(id)
    • find_by(name: name)
    • find_by_name(name)

    View full-size slide

  26. Ωϟογϡ͕ద༻͞Εͳ͍৔߹
    • ϞσϧʹσϑΥϧτείʔϓ͕ઃఆ͞Ε͍ͯΔ
    • ϞσϧͰ୯Ұςʔϒϧܧঝ (STI) ͕࢖༻͞Ε͍ͯΔ
    • PolymorphicΛ࢖ͬͯΔ
    • findͰ idͷ഑ྻΛ౉͢৔߹
    • find(1,2) or find([1,2])

    View full-size slide

  27. ΤϥʔϖʔδͰσόοά

    View full-size slide

  28. ಛఆͷϖʔδͰσόοά

    View full-size slide

  29. ࢦఆIP͔ΒͷΈར༻͢Δ

    View full-size slide

  30. Foreign Key Support

    View full-size slide

  31. ϚΠάϨʔγϣϯͰ֎෦Ωʔ
    ͷ௥Ճɺ࡟আΛαϙʔτ

    View full-size slide

  32. αϙʔτ͞ΕΔΞμϓλ
    • mysql
    • mysql2
    • postgresql

    View full-size slide

  33. Default Host for rails
    server

    View full-size slide

  34. σϑΥϧτͷϗετ͕0.0.0.0͔Β
    localhost(127.0.0.1)ʹมߋ
    ֎෦PC͔ΒΞΫηε͢Δ৔߹
    rails server -b 0.0.0.0

    View full-size slide

  35. ·ͱΊ
    • Active Job͸όοΫΤϯυͷΩϡʔγεςϜʹ͔͔ΘΒ
    ͣɺಉ͡هड़Ͱॻ͚Δ͕ྑ͍
    • ܁Γฦ͢όονॲཧͳͲ͸ɺsidetip΍sidekiq-schedulerΛ
    ࢖͏ͱྑ͍͔΋
    • Global ID͸Active JobҎ֎Ͱ΋࢖͑ͦ͏
    • Ruby on Rails 4.2 ϦϦʔεϊʔτಡ΋͏
    • RailsͷContributorsʹײँ :)

    View full-size slide

  36. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide