You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We need to update Torch to support Phoenix 1.3 in an idiomatic way.
Generators
Our generator is too complicated and hard to use. It would be better if we went to a CLI approach.
Here's how you would build a new Torch area from scratch:
# First, generate the Schema using the Phoenix generator
$ mix phx.gen.schema MyApp.Post ...
$ mix torch.gen
What schema module do you want to use?> MyApp.Post
What do you want to use as the key?
Options: [:id, :uuid]
> id
Which fields do you want to be able to filter on?
Options: [:title, :author, :body, ...]
> title,author,body,published
What context module do you want to generate business logic into?> MyApp.Writing
What is the name of the `repo` module?> MyApp.Repo
What should the controller be named?> MyAppWeb.PostController
Does a user need to have specific permissions to edit `MyApp.Post`s?
yes/no
> yes
What is the name of the user module?> MyApp.User
Generating...
Done!
Context
This is the code that we should append to the bottom of the given context.
aliasMyApp.RepoaliasMyApp.PostaliasMyApp.User,warn: false@post_filters[%Filtrex.Type.Config{type: :text,keys: ~w(title author)}]@doc"""Paginates posts.## Example Writing.paginate_posts(%{ "post" => %{ "title_equals" => "Hello, World!" }, "page" => 2 }, %User{...})"""@specpaginate_posts(map,User.t)::{:ok,Scrivener.Page.t}|{:error,String.t}defpaginate_posts(params,_current_user)dowith{:ok,filter}<-Filtrex.parse(params["post"],@post_filters)dopage=Post|>Filtrex.query(filter)|>Repo.paginate(params){:ok,page}endend@doc"""Gets a post by ID.## Example Writing.get_post(123, %User{...})"""@specget_post(integer,User.t)::{:ok,post}|{:error,:not_found}defget_post(id,_current_user)dopost=Repo.get_by(Post,id)ifpost,do: {:ok,post},else: {:error,:not_found}end@doc"""Returns an `Ecto.Changeset` for a post.## Examples Writing.change_post(%Post{}, %User{}) # => {:ok, %Ecto.Changeset{data: %Post{}, ...}} Writing.change_post(%Post{}, %{"title" => "Hello, World!"}, %User{}) # => {:ok, %Ecto.Changeset{data: %Post{}, ...}}"""@specchange_post(Post.t,map,User.t)::{:ok,Ecto.Changeset.t}defchange_post(post,params\\%{},_current_user)do{:ok,Post.changeset(post,params)}end@doc"""Creates a post.## Examples Writing.create_post(%{"title" => "Hello, World!"}, %User{}) # => {:ok, %Post{title: "Hello, World!"}} Writing.create_post(%{"title" => ""}, %User{}) # => {:error, %Ecto.Changeset{...}}"""@speccreate_post(map,User.t)::{:ok,Post.t}|{:error,Ecto.Changeset.t}defcreate_post(params,_current_user)dowith{:ok,changeset}<-change_post(%Post{},params)doRepo.insert(changeset)endend@doc"""Updates a post.## Examples Writing.update_post(post, %{"title" => "Best Post Ever"}, %User{}) # => {:ok, %Post{title: "Best Post Ever"}} Writing.update_post(post, %{"title" => ""}, %User{}) # => {:error, %Ecto.Changeset{...}}"""@specupdate_post(Post.t,params,User.t)::{:ok,Post.t}|{:error,Ecto.Changeset.t}defupdate_post(post,params,_current_user)dowith{:ok,changeset}<-change_post(post,params)doRepo.update(changeset)endend@doc"""Deletes a post.## Examples Writing.delete_post(post, %User{}) # => {:ok, %Post{...}} Writing.delete_post(post, %User{}) # => {:error, %Ecto.Changeset{...}}"""@specdelete_post(Post.t)::{:ok,Post.t}|{:error,Ecto.Changeset.t}defdelete_post(post,_current_user)dopost|>change_post|>Repo.deleteend
defmoduleMyAppWeb.TorchFallbackControllerdoimportPlug.Conndefcall(conn,{:error,:not_found})doconn|>put_flash(:error,"Record not found!")|>redirect(to: conn.assigns.fallback.index_path)enddefcall(conn,{:error,<<"Unknown filter key",_rest::binary>>=error})doconn|>put_flash(:error,"Invalid search: #{inspect(error)}")|>redirect(to: conn.assigns.index_path)enddefcall(conn,{:error,%Ecto.Changeset{data: %{id: id}}=changeset})whenid==nildoconn|>validation_error(changeset)|>render("new.html")enddefcall(conn,{:error,%Ecto.Changeset{}=changeset})doconn|>validation_error(changeset)|>render("edit.html")enddefpvalidation_error(conn,changeset)doconn|>put_flash(:error,"Oops! There were errors on the form!")|>assign(:changeset,changeset)endend
We need to update Torch to support Phoenix 1.3 in an idiomatic way.
Generators
Our generator is too complicated and hard to use. It would be better if we went to a CLI approach.
Here's how you would build a new Torch area from scratch:
Context
This is the code that we should append to the bottom of the given context.
Controller
Fallback Controller
This helps us DRY up our controller code.
View
Templates
Pretty much unchanged from what we have now.
The text was updated successfully, but these errors were encountered: