Skip to content

niccolox/phoenix_generator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

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, and postgrex to your dependencies (see above)
  • make sure you have postgres running. If you don't configure jumpstart with a postgres-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

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 --timestamps flag when running the scaffold generator, create a file called lib/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

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_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

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
  • Keep up with changes
  • Tests

About

Generate boilerplate for Phoenix applications

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Elixir 88.6%
  • HTML 11.4%