nelsonic / rumbl

Phoenix App built following Programming Phoenix 1.4 book
3 stars 0 forks source link

Re-Read Programming Phoenix Book for v.1.4 #1

Open nelsonic opened 5 years ago

nelsonic commented 5 years ago

With the recent release of the Programming Phoenix 1.4 Book (2018-10-19) see: https://github.com/dwyl/learn-phoenix-framework#book I feel that it would be a good use of my time to re-read it and follow all the examples to ensure that my knowledge is up-to-date for my upcoming Phoenix-based web app projects.

If I sit down and focus, I estimate that it will probably take me 4 days (two uninterrupted weekends...) to read the 269 pages and write the example code.

Better get cracking!

nelsonic commented 5 years ago

At end of page 34 this is the state of the app: image

2 chapters to cover "Hello World" ... ๐Ÿ™„ I feel this could be done with fewer words and more clarity. ๐Ÿค”

nelsonic commented 5 years ago

By the end of page 38 this is what you should see: image

nelsonic commented 5 years ago

At the end of P. 48 you should see this: http://localhost:4000/users/ image

http://localhost:4000/users/2 image

nelsonic commented 5 years ago

image

nelsonic commented 5 years ago

image

nelsonic commented 5 years ago
mix phx.routes
Compiling 5 files (.ex)
page_path  GET   /           RumblWeb.PageController :index
user_path  GET   /users      RumblWeb.UserController :index
user_path  GET   /users/new  RumblWeb.UserController :new
user_path  GET   /users/:id  RumblWeb.UserController :show
user_path  POST  /users      RumblWeb.UserController :create
nelsonic commented 5 years ago

page 64: http://localhost:4000/users/new image

image

nelsonic commented 5 years ago

http://localhost:4000/users image

nelsonic commented 5 years ago

p.69 http://localhost:4000/users/new image Form error working. ๐Ÿ‘

nelsonic commented 5 years ago

Confirm cannot be blank: image

nelsonic commented 5 years ago

Seeing the following warning: image

Compiling 2 files (.ex)
warning: invalid association `credential` in schema Rumbl.Accounts.User: associated schema Credential does not exist
  lib/rumbl/accounts/user.ex:1: Rumbl.Accounts.User (module)

warning: function Credential.changeset/2 is undefined (module Credential is not available)
  lib/rumbl/accounts/user.ex:23
nelsonic commented 5 years ago

Derp ... needed to add the line:

alias Rumbl.Accounts.Credential

To the top of the lib/rumbl/accounts/user.ex file.

No more warnings: image

nelsonic commented 5 years ago

This should work:

$ iex -S mix
iex> alias Rumbl.Accounts.User
iex> alias RumblWeb.Router.Helpers, as: Routes
iex> changeset = User.registration_changeset(%User{}, %{ username: "max", name: "Max", credential: %{ password: "asecret", email: "max@example.com"}})

image

nelsonic commented 5 years ago
iex> for u <- Repo.preload(Repo.all(User), :credential) do 
Repo.update!(User.registration_changeset(u, %{ credential: %{email: "#{u.username}@example.com",
password: "temppass"}})) end

I โค๏ธ Elixir!

nelsonic commented 5 years ago

http://localhost:4000/users/new image

Password length error message working.

Works when length meets min required (6): image

HTML is good as Google Chrome asks to save the password. โœ… image

nelsonic commented 5 years ago

Minor detour to re-read the docs for Plug https://hexdocs.pm/plug/Plug.Conn.html ๐Ÿ‘€

nelsonic commented 5 years ago

on p.88 I'm seeing the following warning: image

nelsonic commented 5 years ago

Access denied: (as expected when user is not logged in) image means the auth Plug is working. ๐Ÿ‘

nelsonic commented 5 years ago

Welcome Back! ๐ŸŽ‰ image

nelsonic commented 5 years ago

Login works: image

As does logout.

#progresso ๐Ÿ‡ง๐Ÿ‡ท

nelsonic commented 5 years ago

p.103

mix phx.gen.html Multimedia Video videos user_id:references:users \
url:string title:string description:text

Add the resource to your browser scope in lib/rumbl_web/router.ex:

resources "/videos", VideoController
$ mix ecto.migrate
nelsonic commented 5 years ago

Now the progress gets faster ... image image

nelsonic commented 5 years ago

image

This is expected on p.117 โœ…

nelsonic commented 5 years ago

By p.118 Editing videos works (as expected) image

nelsonic commented 5 years ago

p.121 mix phx.gen.schema Multimedia.Category categories name:string

nelsonic commented 5 years ago

p.123 mix ecto.gen.migration add_category_id_to_video edit the resulting migration and add the alter command:

defmodule Rumbl.Repo.Migrations.AddCategoryIdToVideo do
  use Ecto.Migration

  def change do
    alter table(:videos) do
      add :category_id, references(:categories) 
    end
  end
end

followed by:

mix ecto.migrate
nelsonic commented 5 years ago

Select Category working: p.128 image image

nelsonic commented 5 years ago

image image

Still does not display category on the :show view. #comingsoon ๐Ÿ‘

nelsonic commented 5 years ago

The Query API (p.131)

Ecto supports a wide range of query options see: https://hexdocs.pm/ecto/Ecto.Query.API.html

In short, you can use many of the same comparison, inclusion, search, and aggregate operations for a typical query that youโ€™d use in Elixir. You can see documentation and examples for many of them in the Ecto.Query.API documen- tation.2 Those are the basic features youโ€™re going to use as you build queries. Youโ€™ll use them from two APIs: keywords syntax and pipe syntax. Letโ€™s see what each API looks like.

nelsonic commented 5 years ago

Pipe Syntax queries: p.133 image

nelsonic commented 5 years ago

The latest version of the Programming Phoenix 1.4 book is considered "content complete": image

So now is a great time to re-read it.

nelsonic commented 5 years ago

Reading up on how Ecto Sandbox works: https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html it's so much better than any other DB adapter I've used. ๐Ÿ˜ฎ

nelsonic commented 5 years ago

"If your code is worth writing, itโ€™s worth testing."

p.167

nelsonic commented 5 years ago

Each time I sit down to try and read (and follow/type the examples) there are at least 10 GitHub notifications competing for my attention ... I really need a "Reading Tracker" https://github.com/nelsonic/time-mvp-phoenix/issues/3 or more broadly a general purpose "Learning Tracker" collaborative-learning-community.md ...

My plan is to use Phoenix for building the tools. So finishing reading the book is the "egg" ๐Ÿฅšthat will hatch into the chicken ... ๐Ÿฃ|> ๐Ÿ”|> ๐Ÿš€

Must stay focussed! ๐Ÿ‘จโ€๐Ÿ’ป 20 pages today! ๐ŸŽฏ

nelsonic commented 5 years ago

http://localhost:4000/manage/videos image

nelsonic commented 5 years ago

Beta 9 released July 24 ... anyone else feel the irony of a typo in the release note? ๐Ÿ™„ image

inexing indeed ...

nelsonic commented 5 years ago

http://localhost:4000/watch/6-totes-amaze-video image

nelsonic commented 5 years ago

p.204: "_Each channel module has three ways to receive events. Youโ€™ll learn more about these callback functions in detail soon. For now, know that handle_in receives direct channel events, handle_out intercepts broadcast events, and handle_info receives OTP messages_".

nelsonic commented 5 years ago

p.232 Introspecting Applications with Observer

$ iex -S mix
iex> :observer.start()

image image image

This is a dream for DevOps and debugging. ๐Ÿ˜

nelsonic commented 5 years ago

When you see the following error:

Module not found: Error: Can't resolve 'phoenix' in 'code/rumbl_umbrella/apps/rumbl_web/assets/js'
Module not found: Error: Can't resolve 'phoenix_html' in 'code/rumbl_umbrella/apps/rumbl_web/assets/js'

Try: https://elixirforum.com/t/phoenix-phoenix-html-npm-error-for-umbrella-project/17575/2 i.e. manually create the symlinks with the following steps:

cd apps/rumbl_web/assets/
rm -f phoenix phoenix
rm -f phoenix phoenix_html
ln -s ../../../../deps/phoenix .
ln -s ../../../../deps/phoenix_html .  
npm install 

Now the JS will compile. โœ…

.then if you see this error (repeatedly):

[error] GenServer #PID<0.461.0> terminating
** (ArgumentError) argument error
    (stdlib) :ets.lookup(RumblWeb.Presence, :pool_size)
    (phoenix_pubsub) lib/phoenix/tracker.ex:279: Phoenix.Tracker.pool_size/1
    (phoenix_pubsub) lib/phoenix/tracker.ex:215: Phoenix.Tracker.list/2
    (phoenix) lib/phoenix/presence.ex:355: Phoenix.Presence.list/2
    (rumbl_web) lib/rumbl_web/channels/video_channel.ex:23: RumblWeb.VideoChannel.handle_info/2
    (phoenix) lib/phoenix/channel/server.ex:305: Phoenix.Channel.Server.handle_info/2
    (stdlib) gen_server.erl:637: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:711: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: :after_join
State: %Phoenix.Socket{assigns: %{user_id: 2, video_id: 6}, channel: RumblWeb.VideoChannel, channel_pid: #PID<0.461.0>, endpoint: RumblWeb.Endpoint, handler: RumblWeb.UserSocket, id: "users_socket:2", join_ref: "157", joined: true, private: %{log_handle_in: :debug, log_join: :info}, pubsub_server: RumblWeb.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: "videos:6", transport: :websocket, transport_pid: #PID<0.457.0>}

Go hunting through the Errata in: https://pragprog.com/titles/phoenix14/errata ๐Ÿ™„ I had to make a couple of changes which were a good test of my Umbrella Knowledge.

First in the file apps/rumbl_web/lib/rumbl_web/application.ex, add RumblWeb.Presence to the list of supervised child processes, e.g:

    children = [
      RumblWeb.Endpoint,
      RumblWeb.Presence
    ]

Then in the apps/rumbl_web/lib/rumbl_web/channels/presence.ex file, change:

  use Phoenix.Presence,
    otp_app: :rumbl,
    pubsub_server: Rumbl.PubSub

to:

  use Phoenix.Presence,
    otp_app: :rumbl_web,
    pubsub_server: RumblWeb.PubSub

Now it works. โœ…

nelsonic commented 5 years ago

When you see the following error:

Module not found: Error: Can't resolve 'phoenix' in 'code/rumbl_umbrella/apps/rumbl_web/assets/js'
Module not found: Error: Can't resolve 'phoenix_html' in 'code/rumbl_umbrella/apps/rumbl_web/assets/js'

Try: https://elixirforum.com/t/phoenix-phoenix-html-npm-error-for-umbrella-project/17575/2 i.e. manually create the symlinks with:

.then if you see this error (repeatedly):

[error] GenServer #PID<0.461.0> terminating
** (ArgumentError) argument error
    (stdlib) :ets.lookup(RumblWeb.Presence, :pool_size)
    (phoenix_pubsub) lib/phoenix/tracker.ex:279: Phoenix.Tracker.pool_size/1
    (phoenix_pubsub) lib/phoenix/tracker.ex:215: Phoenix.Tracker.list/2
    (phoenix) lib/phoenix/presence.ex:355: Phoenix.Presence.list/2
    (rumbl_web) lib/rumbl_web/channels/video_channel.ex:23: RumblWeb.VideoChannel.handle_info/2
    (phoenix) lib/phoenix/channel/server.ex:305: Phoenix.Channel.Server.handle_info/2
    (stdlib) gen_server.erl:637: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:711: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: :after_join
State: %Phoenix.Socket{assigns: %{user_id: 2, video_id: 6}, channel: RumblWeb.VideoChannel, channel_pid: #PID<0.461.0>, endpoint: RumblWeb.Endpoint, handler: RumblWeb.UserSocket, id: "users_socket:2", join_ref: "157", joined: true, private: %{log_handle_in: :debug, log_join: :info}, pubsub_server: RumblWeb.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: "videos:6", transport: :websocket, transport_pid: #PID<0.457.0>}

Go hunting through the Errata in: https://pragprog.com/titles/phoenix14/errata ๐Ÿ™„ I had to make a couple of changes which were a good test of my Umbrella Knowledge.

First in the file apps/rumbl_web/lib/rumbl_web/application.ex, add RumblWeb.Presence to the list of supervised child processes, e.g:

    children = [
      RumblWeb.Endpoint,
      RumblWeb.Presence
    ]

Then in the apps/rumbl_web/lib/rumbl_web/channels/presence.ex file, change:

  use Phoenix.Presence,
    otp_app: :rumbl,
    pubsub_server: Rumbl.PubSub

to:

  use Phoenix.Presence,
    otp_app: :rumbl_web,
    pubsub_server: RumblWeb.PubSub

Now it works. โœ…

nelsonic commented 5 years ago

On p.265 getting the following error:

** (ArgumentError) argument error
    (stdlib) :ets.insert(InfoSys.Cache_cache, {"one plus one?", "two"})
    (info_sys) lib/info_sys/cache.ex:7: InfoSys.Cache.put/2

I tried following https://elixirschool.com/en/lessons/specifics/ets/#inserting-data and get the same error:

image

After a lot of retracing of steps, I figured out that I failed to follow the instruction on p.261 regarding putting InfoSys.Cache in the children (supervision tree) in application.ex ๐Ÿ™„

nelsonic commented 5 years ago
** (ArgumentError) could not fetch application environment :wolfram for application :rumbl because configuration :wolfram was not set

After reading the errata for the book https://pragprog.com/titles/phoenix14/errata Found that @samcdavid had spotted and reported the issue: image

opened: apps/info_sys/lib/info_sys/wolfram.ex and changed the last line:

  defp id, do: Application.fetch_env!(:rumbl, :wolfram)[:app_id]

To:

defp id, do: Application.fetch_env!(:info_sys, :wolfram)[:app_id]
nelsonic commented 5 years ago

At the end of Chapter 12 (OTP) we have http://localhost:4000/watch/7 image

Wolfram alpha is inserting useful info in the annotations thread. ๐ŸŽ‰

nelsonic commented 5 years ago

Had to submit another errata: https://pragprog.com/titles/phoenix14/errata #85673 Sadly it is preventing me from finishing Chapter 13. ๐Ÿ˜ž Really hope they answer/fix it. Meanwhile I'm going to finish reading chapter 14 "What's Next?" which is unrelated to Rumbl.

nelsonic commented 5 years ago

Finished reading the Programming Phoenix 1.4 Book. Felt that the last chapter was a bit of a cop-out. It briefly covers LiveView (but only basic demos no full app or useful extension to "Rumbl") and mentions Telemetry but says that the API will change. Touches upon Phoenix Channels 2.0 which is still in development ... I would much rather they pointed to where to go next than finish with "Good Luck". ๐Ÿ™„

Overall I would recommend the book. ๐Ÿ‘ But I feel they (or someone else) can still do much better. ๐Ÿ’ญ