Rails Tutorialを Phoenixで移植してみた

6eebdd870162058a863f657f72f67eb2?s=47 hagiyat
August 25, 2015

Rails Tutorialを Phoenixで移植してみた

  *      *
  *     +  うそです
     n ∧_∧ n
 + (ヨ(* ´∀`)E)
      Y     Y    *

6eebdd870162058a863f657f72f67eb2?s=128

hagiyat

August 25, 2015
Tweet

Transcript

  1. Rails TutorialΛ PhoenixͰҠ২ͯ͠Έͨ @hagiyat 2015/08/25 #shibuya.ex#1

  2. ͸͡Ί·ͯ͠ɻഡ୩Ͱ͢ɻ github/twitter: hagiyat 2014/05͔Βגࣜձࣾnanapiʹॴଐ ձࣾͷTechBlog΋ͪΐͬͱॻ͍ͯ·͢ http://blog.nanapi.co.jp/tech/author/hagiyat/ ͦΕ·Ͱ͸ϑϦʔϥϯεΛ໊৐Γͳ͕Β৭ʑͳձࣾͰSIerͱͯ͠ಇ͍ͯ ͍ͨΓ͠·ͨ͠ 2014/12ͷݴޠೖΕ͔͑ࣄมͰॳΊͯRuby/Ruby on

    Railsʹ͞ΘΔ Ҏ߱΄΅RubyͷΈॻ͍ͯա͍ͯ͝͠·͢
  3. ͳͥElixirʹڵຯΛ࣋ͬͨͷ͔ʁ RubyΛॻ͍͍ͯͯҰ൪ָ͍͠ͷ͸ϝιουνΣΠϯ࡞Γ RubyΛॻ͖࢝Ίͯɺ͙͢ʹ@merborne͞Μͱ@yuroyoro͞ΜͷϒϩάͰ܆ ಃΛड͚ͨ ϝιουνΣΠϯʹ͔ܽͤͳ͍ͷ͸ߴ֊ؔ਺ ߴ֊ؔ਺ͱ͍͑͹ؔ਺ܕݴޠ ؔ਺ܕݴޠࣗମʹ΋ڵຯ͕Θ͍͚ͨͲɺϝδϟʔͲ͜Ζͷݴޠ͸͙͢ʹ਺ֶͷ ࿩͕࢝·Δͷ͕ਏ͔ͬͨ ख޶ͷ෇͖͖͍ͬͯͳ͍ؔ਺ܕݴޠΛٻΊΔ Elixirʂʁ

  4. ·ͣ͸Կ͔ॻ͍ͯΈΑ͏ ਎ۙͳ؀ڥΛ࠶ݱͯ͠ΈΔͱ͍͏ͷ͕ਅͬઌʹࢥ͍͍ͭͨ WAF͸ɺ޾͍͙͢ʹPhoenixͱ͍͏΋ͷ͕ݟ͔ͭͬͨ ͦΕ͕Railsʹ͍ۙͱ͍͏ᷚ΋ฉ͖͚ͭͨ

  5. ΍ͬͯΈΑ͏ʂʂʢ҆௚ʣ

  6. ·ͣ͸؀ڥͮ͘Γ ηοτΞοϓʢ1ষʣ͸ͬ͘͞ͱࡁΜͩ ౰࣌࠷৽ͩͬͨPhoenix0.13.0ΛΠϯετʔϧ ࠓ͔Β͸͡ΊΔΜͩͬͨΒઈରanyenv - erlenv/exenv͕ Φεεϝ http://qiita.com/nownabe/items/ 67a27ddd23652676ffee

  7. ͬͦ͘͞Phoenix.new EctoͷηοτΞοϓ ඪ४ͷσʔλετΞ͸PostgreSQL PostgreSQLૉਓ͗ͯ͢ɺ઀ଓ͢Δ·ͰͪΐͬͱϋϚΔ ↑useraddతͳͱ͜Ζ localhost:4000Λݟͯؾ෼ΛߴΊΔ ৽઱͞͸ϞνϕʔγϣϯʹͳΔ

  8. ֤Gemͷ୅ସΛ୳͢ Rails Phoenix sqlite3 EctoͰαϙʔτ sass-rails brunch-config.jsͰઃఆ͢Ε͹OK uglifier brunch-config.jsͰઃఆ͢Ε͹OK coffee-rails

    brunch-config.jsͰઃఆ͢Ε͹OKͷ͸ͣɻެࣜυΩϡϝϯ τʹTypescriptʹ͍ͭͯͷݴٴ͕͋Γ·͢ jquery-rails brunch-config.jsͰઃఆ͢Ε͹OK turbolinks ͳ͍ʂ jbuilder ViewͰରԠ sdoc ex_doc http://d.hatena.ne.jp/k-1/20130505/p1 rails_12factor ެࣜυΩϡϝϯτʹهࡌ͋Γ http://www.phoenixframework.org/docs/heroku
  9. ൪֎ฤɿϥΠϒϥϦ୳͠ͷ༑ http://getawesomeness.com/ ຊ౰ʹศར

  10. Scaffoldతͳ΍ͭ mix phoenix.gen.html Ͱ࣮ߦͰ͖Δ http://www.phoenixframework.org/docs/mix-tasks ࢖༻ײ͸΄΅ಉ͡ͱݴͬͯྑ͍ͱࢥ͍·͢ jsonϨεϙϯε༻ͷઐ༻λεΫ phoenix.gen.json ͕ଘࡏ ͢Δͷ͕ಛ௃త

  11. ActiveRecordతͳ΍ͭ Ectoͱ͍͏ඪ४ORM(?) PostgreSQL/MySQL/MSSQL/SQLite3͕࢖͑Δ MongoDB΋࢖͑ΔΑ͏ʹͳΔ໛༷ association΋ActiveRecordͱಉ͡ײ֮Ͱ࢖͑Δ model͝ͱ࡞Δ(phoenix.gen.model)ͷ΋ɺmigrate୯ମ΋ (ecto.gen.migration) mixλεΫͰ࣮ߦͰ͖Δ ೉͍͜͠ͱ͸ࢼ͍ͯ͠ͳ͍ͷͰɺ࢖༻ײ͸·ͩอཹ

  12. rails server/rails console RubyͰ͋Ε͹ଈPryؔ࿈ͷGemΛ͋Ε͜ΕಥͬࠐΉ͚Ͳɺiex͸ ૉͰ΋ઓ͑Δ iex -S mix phoenix.server ͸ɺrails

    server݉rails consoleͰɺ tmuxʹ΍͍͞͠ ิ׬͕ຊ౰ʹര଎ byebug΋ඪ४ͰೖͬͯΔ(ϒϨΠΫϙΠϯτషΔͷ͸؆୯ɻ΋͠ ͔ͯ͠εςοϓ࣮ߦͰ͖ͳ͍ʁ) show-doc͸Ͱ͖Δ͚Ͳshow-source͸Ͱ͖ͳ͍ʁʁ
  13. 2ষऴྃɺ3ষ΁ ໰୊ൃੜ

  14. Rails TutorialΛҠ২ͨ͠ͱݴ͕ͬͨɺΞϨ͸ӕͩ ࣮૷͍ͯ͘͠ʹͭΕͯɺ࣮ફʹೖͬͨΒViewपΓͰۤ͠Ή͜ ͱ͕༧૝Ͱ͖ͯ͠·ͬͨ Slim͕ͳ͍ʂ Turbolinks͕ͳ͍ʂ Cells͕ͳ͍ʂ Kaminari͕ͳ͍ʂ

  15. ΋ͬͱࠜຊతʹ RailsTutorialʹ͸ग़ͯ͜ͳ͍͚ͲɺElixir/Phoenixతʹ͸௒ ॏཁͳ͜ͱ͕͍͔ͭ͘ ฒྻॲཧؔ࿈ Channelؔ࿈ ͜Ε΍Βͳ͍ͰͲ͏͢Μͷײ

  16. Phoenix͸RailsͷҠ২(్த)ʹ͗͢ͳ͍ʁ ଟ෼໨ࢦ͍ͯ͠Δͱ͜Ζ͕ͪΐͬͱҧ͏ Channelͷଘࡏ 0.13.1Ҏ߱ɺWebSocketͱ͔PubSubͱ͔ Transport.LongPollͱ͔ɺίϯςϯπੜ੒ɾ഑৴ػೳ͔Β ཭Εͨͱ͜ΖͷڧԽ͕໨ཱͬͨ͜ͱ΋͋Γɺ༨ܭͦ͏ײ͡ Δ

  17. Elixirʹ͍ͭͯͷ·ͱΊ - πʔϧฤ iex͕࠷ߴ͗͢Δ REPLͳ͠Ͱ͸Կ΋Ͱ͖ͳ͍੎ʹͱͬͯ͸ࢸߴͷπʔϧ mix͸૬౰࢖͍΍͍͢ ૣͯ͘؆୯ mix.exsͷهड़΋ཧղ͠΍͍͢ ຊ֨తʹ࢖͍ग़ͨ͠Βɺzshͷิ׬͸ࣗ࡞ͯ͠Ͱ΋ͳΜͱ͔͠ ͍ͨ

  18. Elixirʹ͍ͭͯͷ·ͱΊ - ݴޠฤ |> ͸Ϡό͍ RubyͰϝιουνΣΠϯ͕ݏ͍ͩͬͨਓʹ΋Φεεϝ͍ͨ͠ “hoge,fuga,piyo,baz” |> String.split(“,”) ͱ͍͏۩߹ʹɺॲ

    ཧͷίϯςΩετ͕pipeͷ௚ޙʹ͋Δ(͜ͷྫͩͱString)ͬ ͯɺಡΈ΍ͯ͘͢Θ͔Γ΍͘͢ͳ͍Ͱ͔͢ʁ ฒྻॲཧ͕ຊ౰ʹΧδϡΞϧʹͰ͖Δ spawn_linkͱ͔؆୯͗͢ͰΘ͔Γ΍͗͢͢
  19. Phoenixʹ͍ͭͯͷ·ͱΊ ίϯςϯπΛ഑৴͢ΔλΠϓͷαʔϏε։ൃ͸࣌ظঘૣ(͔΋ʁ) ࠓ·Ͱ࢖͖ͬͯͨViewपΓͷศརͳಓ۩͕࢖͑ͳ͍ͷ͕ਏ͍ ؅ཧը໘ͱ͔͸ಛʹɺखؒΛ͔͚ͣʹαΫαΫը໘࡞ͬͨΓ͍ͨ͠ͷͰɺγϣʔτΧοτͷ੔ උ͸େࣄ͔ͳʔ RestfulͳAPI։ൃʁ ػೳతͳෆ଍͸ɺී௨ʹ΍͍ͬͯΔ෼Ͱ͸ಛʹײ͡ͳ͍ ڧ͍ͯݴ͑͹KVSΛEcto͔Β࢖͍͍ͨέʔε͸࣮֬ʹ͋ΔΑͳ͊ɾɾͱ͍͏ͱ͜Ζ Scala/Akkaൺֱͩͱ࣮ߦ଎౓͸ҰาৡΔ͚Ͳɺ࣮૷͕೉͘͠ͳͯ͘Ϗϧυ͕ૣ͍ Railsൺֱͩͱಉ࣌઀ଓ਺΍࣮ߦ଎౓ʹ༏Ґੑ͕͋Δ

    ্هཧ༝ʹΑΓɺདྷΔ΂͖Http2࣌୅ͷόοΫΤϯυͱͯ͠ຊ౰ʹͪΐ͏Ͳ͍͍ؾ͕͢Δ
  20. ๨Εͯ͸͍͚ͳ͍Ϣχοτςετ - extest ී௨ʹී௨ͷςετεΠʔτ͚ͩͲɺasyncઃఆ͕σϑΥ ϧτͰೖͬͯΔ RailsͰ͸͋Ε͜Εۤ࿑ͯ͠ฒྻςετͷ࢓૊ΈΛߟ͑ͨΓ ͯͨ͠ͷͰɺΧδϡΞϧʹͰ͖͗ͯ͢ٽ͚Δ ͨͩɺ࣌ʑFactoryGirl΍Faker΍TimecopͳͲ͕࿀͘͠ͳ Δ͔΋͠Εͳ͍ ࣗಈ࣮ߦ͸Guardͷ··Ͱ΋͍͍͔ͳʔ

  21. ऴΘΓʹ Elixir/PhoenixɺϓϩμΫτ΁ͷ౤ೖ։࢝͠·ͨ͠ʂʂ ͦͷ͏ͪৄ͘͠࿩ͤΔ͔΋ɾɾ ͋ͱ͜ͷϓϩδΣΫτ௒Ԡԉͯ͠·͢ https://github.com/awetzel/neovim-elixir neovim͸Elixirઐ༻ͷIDEʹͳΔ͔΋ʂʁ

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