Giter Site home page Giter Site logo

Comments (4)

danschultzer avatar danschultzer commented on May 2, 2024 1

Looks good to me, and it'll be easy to refactor!

A note is that if the :userinfo association is preloaded on the user, it'll update instead of replacing it with changeset (the docs describe how it works).

Also, instead of checking id, you can just check if the struct is persisted: Ecto.get_meta(user, :state) != :loaded.

from pow.

danschultzer avatar danschultzer commented on May 2, 2024

You can do this modifying the changeset method adding the user info as a nested changeset like so:

def changeset(user_or_changeset, attrs) do
  user
  |> pow_changeset(attrs)
  |> pow_extension_changeset(attrs)
  |> cast_assoc(:user_info, required: true)
end

This will call the changeset method in your UserInfo schema module.

I think this is the most appropriate way. I'll probably leave ControllerCallbacks out of readme as it's mostly for well-versed developers. The issue you experience is because it'll only pull the extension callbacks. You'll have to create a different controller callback module. But it'll be a lengthy explanation, and I think the nested association would work here.

from pow.

danschultzer avatar danschultzer commented on May 2, 2024

Oh, the hex docs on cast_assoc/3: https://hexdocs.pm/ecto/Ecto.Changeset.html#cast_assoc/3

from pow.

jung-hunsoo avatar jung-hunsoo commented on May 2, 2024

Oh great. Thanks. You're right that Changeset is the best place. It's ironic that I migrated from Coh.. to Pow because redirect doesn't work. (#372 there) If I utilized cast_assoc/3 there I still reside there.

Anyway I finally managed to make it work as expected. Not a pretty codes though:

  def changeset(user, attrs) do
    attrs = refine_attrs(user, attrs)

    user
    |> pow_changeset(attrs)
    |> pow_extension_changeset(attrs)
    |> Ecto.Changeset.cast(attrs, [:nickname, :locale, :role])
    |> Ecto.Changeset.validate_required([:nickname])
    |> Ecto.Changeset.unique_constraint(:nickname)
    |> Ecto.Changeset.cast_assoc(:userinfo, required: true)
  end

  @doc false
  defp refine_attrs(user, attrs) do
    if !Map.get(user, :id) && map_size(attrs) > 0 do
      case Map.keys(attrs) |> hd |> is_atom do
        true  -> Map.put(attrs, :userinfo, %{})
        false -> Map.put(attrs, "userinfo", %{})
      end
    else
      attrs
    end
  end

:userinfo is added when a new user is created, so that even from default Pow operations can be worked too.

I already was aware of cast_assoc/3, but didn't know this can work. UUID was my walkaround for this situation, and other libs too. Like note in Arc.

I've learned one good thing haha. Thanks. Now my webapp is working smoothly.

from pow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.