DEPRECATED: The functionality of these generators is now mostly built into Phoneix and the rest have not been updated in quite some time!
#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"}]
end
You should also update your applications list to include postgrex and ecto:
def application do
[applications: [:postgrex, :ecto]]
end
mix do deps.get, compile
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.
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
, andpostgrex
to your dependencies (see above) - make sure you have postgres running. If you don't configure
jumpstart
with apostgres-url
then make sure user:user
has 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
- 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
datetime
s being inserted directly into html. If you want this functionality, say by using the--timestamps
flag when running thescaffold
generator, create a file calledlib/ecto_datetime_html_safe.ex
with 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
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_generator
to 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
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
- Keep up with changes
- Tests