#Phoenix Generator A collection of boilerplate generators for the Phoenix Web Framework.
https://hex.pm/packages/phoenix_generator
https://github.com/etufe/phoenix_generator
##Setup
- Start a Phoenix project: http://www.phoenixframework.org/v0.8.0/docs
- Add Phoenix Generator and optionally Ecto and Postgrex to your project's dependencies
defp deps do
[{:postgrex, ">= 0.0.0"},
{:ecto, github: "elixir-lang/ecto"},
{:phoenix_generator, github: "etufe/phoenix_generator"}]
endYou should also update your applications list to include postgrex and ecto:
def application do
[applications: [:postgrex, :ecto]]
endmix do deps.get, compile
Generators
run a generator: mix phoenix.gen.some_generator
get help and options: mix help phoenix.gen.some_generator
- jumpstart requires ecto: Sets up a repo and database config.
- scaffold requires ecto: Generates a Controller/Model/View/Template scaffold.
- controller : Generates a controller and optionally sets up a view, actions and templates.
- view: generates a view class
- ectomodel requires ecto: Generates a model optionally with fields and a migration.
- template: Generates an empty template for the given action.
- channel: Generates an empty Channel and optionally the route.
- instachat: Generates a chatroom as a channel demo.
Putting it all together
Let's use the generators to create a simple notes app.
- Create a phoenix application http://www.phoenixframework.org/v0.8.0/docs
-
Add
phoenix_generator,ecto, andpostgrexto your dependencies (see above) -
make sure you have postgres running. If you don't configure
jumpstartwith apostgres-urlthen make sure user:userhas adequate permissions in postgres -
run
mix phoenix.gen.jumpstart -
run
mix ecto.create -
run
mix phoenix.gen.scaffold note title:string body:string -
run
mix ecto.migrate -
run
mix phoenix.server - navigate to http://localhost:4000/notes
Caveats
- At the moment this project is only meant to work with the most recent versions of Ecto and Phoenix on github.
-
Ecto doesn't currently support
datetimes being inserted directly into html. If you want this functionality, say by using the--timestampsflag when running thescaffoldgenerator, create a file calledlib/ecto_datetime_html_safe.exwith the contens:defimpl Phoenix.HTML.Safe, for: Ecto.DateTime do def to_iodata(%Ecto.DateTime{day: day, hour: hour, min: min, month: month, sec: sec, year: year}) do "#{year}/#{month}/#{day} #{hour}:#{min}:#{sec}" end end
Trying out Channels / Instachat
The instachat generator generates a functioning chat with multiple rooms and basic nickname management. It uses an ets table to store the nicknames.
- Create a phoenix application.
-
Add
phoenix_generatorto your dependencies. -
run
mix phoenix.gen.instachat -
run
mix phoenix.server - navigate to http://localhost:4000/instachat
- Look around web/channels/instachat.exs and web/templates/instachat/index.html.eex
Contributing
Feel free to make pull requests or create github issues. Ecto and Phoenix are both moving targets at the moment and I aim to keep these generators in sync with whatever is in master of those two projects.
##Todo
- Replace all resources_path references with the helper
-
There seems to be a bug with
Map.merge(@model.__struct__, params["resource"]) - Flash messages
- Handle Ecto date/times better
- Keep up with changes
- Tests
- Chat takes a minute to start working, find out why.