[Programming Phoenix] How do sessions work in Programming Phoenix?

book-programming-phoenix
phoenix

#1

Hello there,

I`m currently reading Programming Phoenix and struggle to understand some parts of the code.
2 Examples:

[web/models/user.ex]
def changeset(model, params \\ :empty) do
  model
  |> cast(params, ~w(name username), []) # WTF is ~w? Never explained
  |> validate_length(:username, @username_validation_length)
end

Why the ~w/1 function and this strange name? What does name/1 do?
When I look up the docs for cast/3 , I can just replace ~w/1 with [:name, :username] but I wan’t to know what’s the difference (besides eye cancer).

Another problem is the following code:

[web/controllers/auth.ex]
def call(conn, repo) do
  user_id = get_session(conn, :user_id)
  user = user_id && repo.get(Rumbl.User, user_id) # && should return boolean
  assign(conn, :current_user, user)
end

The docs are not really helpful in describing get_session/2:

get_session(conn, key)
Returns session value for the given key

I can assume that get_session/2 returns an int (but not because of the docs…).
I dont know what’s going on on the other lines of code, he is using this session id to get a user from the repo, but I thought the session id would be some kind of random value…
After that, I dont know whats going on with user (an int) and the returned struct…He is comparing them somehow…how is this working?
Thanks for taking time and reading this, I hope you can help me.


Session doesn't persist across web requests
#2

~w is a sigil, it calls the macro named sigil_w at compile-time, which takes what is between the (/) pairs and parses it out via space separation into atoms in a list. It is explained at: https://elixir-lang.org/getting-started/sigils.html :slight_smile:

Basically ~w(name username) === [:name, :username]

No, I don’t get the point either, I just use atoms straight. I personally think it is a perfectly good wasted sigil name (we only have a limited amount after all!). ^.^;


#3

Thats not a function, thats a “sigil”, in this case a word list, meaning it is equivalent to ["name", "username"] and very convienient.

And also its not /1, sigils are /2 :wink:

which is equivalent to ~w[name username]a (the a is an option to the w sigil and makes it return a list of atoms instead of strings, Another option is c which makes it return a list of char-lists.

The docs tell you, it can return any and totally depends on what you put there first.

After put_session(conn, :foo, "bar") you will get "bar" as a result of get_session(conn, :foo) on subsequent requests.


But to be honest, I’m sure that all of this is explained in the book before using it or very near to that snippet referencing it.


#4

It returns whatever you put in to the session with put_session/3 in the same docs. :slight_smile:

He is just getting back whatever he put in. The ‘session’ is just storage for anything (keep it small) for a session. :slight_smile:

Not comparing, Matching. :slight_smile:

Rumbl.User is an atom, the module name specifically. get_session(conn, :user_id) is just getting out whatever was put in the session earlier via put_session/3, then matching it to user_id (so user_id is bound to the result of that). The repo.get(Rumbl.User, user_id) is looking up the Rumbl.User user with the primary key of whatever user_id is bound to at this time. The user_id && repo... means only run the second expression after the && if the expression to the left of it is not nil or false (I wish it tested [] too as that is really nil in my opinion… but it does not). All that is bound to user then, which is then assigned on the conn for the current connection via assign. :slight_smile:


#5

NO!!! Its not! Without options given ~w creates a list of strings, not atoms. But yes, in the case of ectos cast/* they are treated identically, but they aren’t.

I don’t use it often as well, but I’ve seen it often used by folks comming from ruby where the equivalent %w() is more than common.


#6

Er yes, that. ^.^

I just don’t really see what it saves, a single character per atom/word in exchange for one extra overall does not seem like a huge savings to make it worth using up one of the valuable/limited sigil names (I wish they were not restricted to a single character, don’t know why they are…).


#7

As I said, its often used by the ruby folks, and please remember where José comes from :slight_smile:

Also I think it might come in handy when you have a lot of words which contain quotes or are IPs/hosts:

~w[foo"bar bar'baz ::1 example.com]a

If you have a lot of them and try it “traditionally” you’ll have some overhead I’d guess. But in simple cases as casts arguments I prefer explicit list of atoms as well.