diff --git a/larus/config/config.exs b/larus/config/config.exs index 95fd427..6fe5ded 100644 --- a/larus/config/config.exs +++ b/larus/config/config.exs @@ -1,32 +1,32 @@ # This file is responsible for configuring your application # and its dependencies with the aid of the Mix.Config module. # # This configuration file is loaded before any dependency and # is restricted to this project. use Mix.Config # Configures the endpoint config :larus, Larus.Endpoint, url: [host: "localhost"], root: Path.dirname(__DIR__), - secret_key_base: "P4ngDTuHdlm7r4xqCkz1XwmyQoVJ8tJBDixcKVMEE6aVAwUsy59vZw/b/U7UgYpz", + secret_key_base: "P4ngDTuHdlm7r4xqCkz1XwmyQoVJ8tJBDixcKVMEE6aVAwUsy59vZw/b/", render_errors: [accepts: ~w(html json)], pubsub: [name: Larus.PubSub, adapter: Phoenix.PubSub.PG2] config :larus, Larus.Gettext, locales: ~w(en de nl) # Configures Elixir's Logger config :logger, :console, format: "$time $metadata[$level] $message\n", metadata: [:request_id] # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{Mix.env}.exs" # Configure phoenix generators config :phoenix, :generators, migration: true, binary_id: false diff --git a/larus/lib/larus.ex b/larus/lib/larus.ex index 4e0d879..cbd1a4d 100644 --- a/larus/lib/larus.ex +++ b/larus/lib/larus.ex @@ -1,30 +1,33 @@ defmodule Larus do + @moduledoc """ + Application + """ use Application # See http://elixir-lang.org/docs/stable/elixir/Application.html # for more information on OTP Applications def start(_type, _args) do import Supervisor.Spec, warn: false children = [ # Start the endpoint when the application starts supervisor(Larus.Endpoint, []), # Start the Ecto repository supervisor(Larus.Repo, []), # Here you could define other workers and supervisors as children # worker(Larus.Worker, [arg1, arg2, arg3]), ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: Larus.Supervisor] Supervisor.start_link(children, opts) end # Tell Phoenix to update the endpoint configuration # whenever the application is updated. def config_change(changed, _new, removed) do Larus.Endpoint.config_change(changed, removed) :ok end end diff --git a/larus/lib/larus/endpoint.ex b/larus/lib/larus/endpoint.ex index ac3015b..775862d 100644 --- a/larus/lib/larus/endpoint.ex +++ b/larus/lib/larus/endpoint.ex @@ -1,39 +1,43 @@ defmodule Larus.Endpoint do + @moduledoc """ + Larus.Endpoint + """ + use Phoenix.Endpoint, otp_app: :larus socket "/socket", Larus.UserSocket # Serve at "/" the static files from "priv/static" directory. # # You should set gzip to true if you are running phoenix.digest # when deploying your static files in production. plug Plug.Static, at: "/", from: :larus, gzip: false, only: ~w(css fonts images js favicon.ico robots.txt) # Code reloading can be explicitly enabled under the # :code_reloader configuration of your endpoint. if code_reloading? do socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket plug Phoenix.LiveReloader plug Phoenix.CodeReloader end plug Plug.RequestId plug Plug.Logger plug Plug.Parsers, parsers: [:urlencoded, :multipart, :json], pass: ["*/*"], json_decoder: Poison plug Plug.MethodOverride plug Plug.Head plug Plug.Session, store: :cookie, key: "_larus_key", signing_salt: "RoqWQScU" plug Larus.Router end diff --git a/larus/lib/larus/repo.ex b/larus/lib/larus/repo.ex index d236b0d..3cd40e0 100644 --- a/larus/lib/larus/repo.ex +++ b/larus/lib/larus/repo.ex @@ -1,3 +1,6 @@ defmodule Larus.Repo do + @moduledoc """ + Larus.Repo + """ use Ecto.Repo, otp_app: :larus end diff --git a/larus/mix.exs b/larus/mix.exs index 6b39675..36af8dd 100644 --- a/larus/mix.exs +++ b/larus/mix.exs @@ -1,52 +1,53 @@ defmodule Larus.Mixfile do use Mix.Project def project do [app: :larus, version: "0.0.1", elixir: "~> 1.0", elixirc_paths: elixirc_paths(Mix.env), compilers: [:phoenix, :gettext] ++ Mix.compilers, build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, aliases: aliases, deps: deps] end # Configuration for the OTP application. # # Type `mix help compile.app` for more information. def application do [mod: {Larus, []}, applications: [:phoenix, :phoenix_html, :cowboy, :logger, :gettext, :phoenix_ecto, :postgrex]] end # Specifies which paths to compile per environment. defp elixirc_paths(:test), do: ["lib", "web", "test/support"] defp elixirc_paths(_), do: ["lib", "web"] # Specifies your project dependencies. # # Type `mix help deps` for examples and options. defp deps do [{:phoenix, "~> 1.1.4"}, {:postgrex, ">= 0.0.0"}, {:phoenix_ecto, "~> 2.0"}, {:phoenix_html, "~> 2.4"}, {:phoenix_live_reload, "~> 1.0", only: :dev}, {:gettext, "~> 0.9"}, + {:dogma, "~> 0.1", only: :dev}, {:cowboy, "~> 1.0"}] end # Aliases are shortcut or tasks specific to the current project. # For example, to create, migrate and run the seeds file at once: # # $ mix ecto.setup # # See the documentation for `Mix` for more info on aliases. defp aliases do ["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], "ecto.reset": ["ecto.drop", "ecto.setup"]] end end diff --git a/larus/test/support/model_case.ex b/larus/test/support/model_case.ex index 0e16e03..ca1ef14 100644 --- a/larus/test/support/model_case.ex +++ b/larus/test/support/model_case.ex @@ -1,61 +1,61 @@ defmodule Larus.ModelCase do @moduledoc """ This module defines the test case to be used by model tests. You may define functions here to be used as helpers in your model tests. See `errors_on/2`'s definition as reference. Finally, if the test case interacts with the database, it cannot be async. For this reason, every test runs inside a transaction which is reset at the beginning of the test unless the test case is marked as async. """ use ExUnit.CaseTemplate using do quote do alias Larus.Repo import Ecto import Ecto.Changeset import Ecto.Query, only: [from: 1, from: 2] import Larus.ModelCase end end setup tags do unless tags[:async] do Ecto.Adapters.SQL.restart_test_transaction(Larus.Repo, []) end :ok end @doc """ Helper for returning list of errors in model when passed certain data. ## Examples Given a User model that lists `:name` as a required field and validates `:password` to be safe, it would return: - iex> errors_on(%User{}, %{password: "password"}) + iex> errors_on(%User{}, %{password: "pass"}) [password: "is unsafe", name: "is blank"] You could then write your assertion like: - assert {:password, "is unsafe"} in errors_on(%User{}, %{password: "password"}) + assert {:password, "is unsafe"} in errors_on(%User{}, %{password: "pass"}) You can also create the changeset manually and retrieve the errors field directly: iex> changeset = User.changeset(%User{}, password: "password") iex> {:password, "is unsafe"} in changeset.errors true """ def errors_on(model, data) do model.__struct__.changeset(model, data).errors end end diff --git a/larus/test/test_helper.exs b/larus/test/test_helper.exs index 25e7539..c7ce2c1 100644 --- a/larus/test/test_helper.exs +++ b/larus/test/test_helper.exs @@ -1,6 +1,5 @@ ExUnit.start Mix.Task.run "ecto.create", ~w(-r Larus.Repo --quiet) Mix.Task.run "ecto.migrate", ~w(-r Larus.Repo --quiet) Ecto.Adapters.SQL.begin_test_transaction(Larus.Repo) - diff --git a/larus/test/views/layout_view_test.exs b/larus/test/views/layout_view_test.exs index a86dee0..41d5fbb 100644 --- a/larus/test/views/layout_view_test.exs +++ b/larus/test/views/layout_view_test.exs @@ -1,3 +1,3 @@ defmodule Larus.LayoutViewTest do - use Larus.ConnCase, async: true -end \ No newline at end of file + use Larus.ConnCase, async: true +end diff --git a/larus/web/channels/user_socket.ex b/larus/web/channels/user_socket.ex index c95a530..d4d7589 100644 --- a/larus/web/channels/user_socket.ex +++ b/larus/web/channels/user_socket.ex @@ -1,37 +1,41 @@ defmodule Larus.UserSocket do + @moduledoc """ + Larus.UserSocket + """ use Phoenix.Socket - ## Channels + # ## Channels # channel "rooms:*", Larus.RoomChannel - ## Transports + # ## Transports transport :websocket, Phoenix.Transports.WebSocket # transport :longpoll, Phoenix.Transports.LongPoll # Socket params are passed from the client and can # be used to verify and authenticate a user. After # verification, you can put default assigns into # the socket that will be set for all channels, ie # # {:ok, assign(socket, :user_id, verified_user_id)} # # To deny connection, return `:error`. # # See `Phoenix.Token` documentation for examples in # performing token verification on connect. def connect(_params, socket) do {:ok, socket} end - # Socket id's are topics that allow you to identify all sockets for a given user: + # Socket id's are topics that allow you to identify all sockets for a given + # user: # - # def id(socket), do: "users_socket:#{socket.assigns.user_id}" + # def id(socket), do: "users_socket:#{socket.assigns.user_id}" # # Would allow you to broadcast a "disconnect" event and terminate # all active sockets and channels for a given user: # - # Larus.Endpoint.broadcast("users_socket:#{user.id}", "disconnect", %{}) + # Larus.Endpoint.broadcast("users_socket:#{user.id}", "disconnect", %{}) # # Returning `nil` makes this socket anonymous. def id(_socket), do: nil end diff --git a/larus/web/controllers/page_controller.ex b/larus/web/controllers/page_controller.ex index 0604934..ae3a46b 100644 --- a/larus/web/controllers/page_controller.ex +++ b/larus/web/controllers/page_controller.ex @@ -1,7 +1,11 @@ defmodule Larus.PageController do - use Larus.Web, :controller + @moduledoc """ + Larus.PageController + """ - def index(conn, _params) do - render conn, "index.html" - end + use Larus.Web, :controller + + def index(conn, _params) do + render conn, "index.html" + end end diff --git a/larus/web/gettext.ex b/larus/web/gettext.ex index b06b41c..0b3e07a 100644 --- a/larus/web/gettext.ex +++ b/larus/web/gettext.ex @@ -1,36 +1,37 @@ defmodule Larus.Gettext do - @moduledoc """ - A module providing Internationalization with a gettext-based API. + @moduledoc """ + A module providing Internationalization with a gettext-based API. - By using [Gettext](http://hexdocs.pm/gettext), - your module gains a set of macros for translations, for example: + By using [Gettext](http://hexdocs.pm/gettext), + your module gains a set of macros for translations, for example: - import Larus.Gettext + import Larus.Gettext - # Simple translation - gettext "Here is the string to translate" + # Simple translation + gettext "Here is the string to translate" - # Plural translation - ngettext "Here is the string to translate", - "Here are the strings to translate", - 3 + # Plural translation + ngettext "Here is the string to translate", + "Here are the strings to translate", + 3 - # Domain-based translation - dgettext "errors", "Here is the error message to translate" + # Domain-based translation + dgettext "errors", "Here is the error message to translate" - See the [Gettext Docs](http://hexdocs.pm/gettext) for detailed usage. - """ - use Gettext, otp_app: :larus + See the [Gettext Docs](http://hexdocs.pm/gettext) for detailed usage. + """ - def supported_locales do - known = Gettext.known_locales(Larus.Gettext) - allowed = config[:locales] + use Gettext, otp_app: :larus - Set.intersection(Enum.into(known, HashSet.new), Enum.into(allowed, HashSet.new)) - |> Set.to_list - end + def supported_locales do + known = Gettext.known_locales(Larus.Gettext) + allowed = config[:locales] - defp config do - Application.get_env(:larus, __MODULE__) - end + Set.intersection(Enum.into(known, HashSet.new), Enum.into(allowed, HashSet.new)) + |> Set.to_list + end + + defp config do + Application.get_env(:larus, __MODULE__) + end end diff --git a/larus/web/router.ex b/larus/web/router.ex index 91973dc..b92c428 100644 --- a/larus/web/router.ex +++ b/larus/web/router.ex @@ -1,27 +1,31 @@ defmodule Larus.Router do + @moduledoc """ + Larus.Router + """ + use Larus.Web, :router pipeline :browser do plug :accepts, ["html"] plug :fetch_session plug :fetch_flash plug :protect_from_forgery plug :put_secure_browser_headers plug Larus.Plug.Locale, "en" end pipeline :api do plug :accepts, ["json"] end scope "/", Larus do pipe_through :browser # Use the default browser stack get "/", PageController, :index end # Other scopes may use custom stacks. # scope "/api", Larus do # pipe_through :api # end end diff --git a/larus/web/views/error_helpers.ex b/larus/web/views/error_helpers.ex index d2462c1..bcc80bf 100644 --- a/larus/web/views/error_helpers.ex +++ b/larus/web/views/error_helpers.ex @@ -1,35 +1,37 @@ defmodule Larus.ErrorHelpers do @moduledoc """ Conveniences for translating and building error messages. """ use Phoenix.HTML @doc """ Generates tag for inlined form input errors. """ def error_tag(form, field) do - if error = form.errors[field] do - content_tag :span, translate_error(error), class: "help-block" + error = form.errors[field] + if error do + content_tag :span, translate_error(error), class: "help-block" end end @doc """ Translates an error message using gettext. """ def translate_error({msg, opts}) do # Because error messages were defined within Ecto, we must # call the Gettext module passing our Gettext backend. We # also use the "errors" domain as translations are placed # in the errors.po file. On your own code and templates, # this could be written simply as: # # dngettext "errors", "1 file", "%{count} files", count # + Gettext.dngettext(Larus.Gettext, "errors", msg, msg, opts[:count], opts) end def translate_error(msg) do Gettext.dgettext(Larus.Gettext, "errors", msg) end end diff --git a/larus/web/views/error_view.ex b/larus/web/views/error_view.ex index 6c69b99..cab7da9 100644 --- a/larus/web/views/error_view.ex +++ b/larus/web/views/error_view.ex @@ -1,17 +1,21 @@ defmodule Larus.ErrorView do + @moduledoc """ + Views on errors + """ + use Larus.Web, :view def render("404.html", _assigns) do "Page not found" end def render("500.html", _assigns) do "Server internal error" end # In case no render clause matches or no # template is found, let's render it as 500 def template_not_found(_template, assigns) do render "500.html", assigns end end diff --git a/larus/web/views/layout_view.ex b/larus/web/views/layout_view.ex index 61b6403..a197375 100644 --- a/larus/web/views/layout_view.ex +++ b/larus/web/views/layout_view.ex @@ -1,15 +1,19 @@ defmodule Larus.LayoutView do - use Larus.Web, :view + @moduledoc """ + A layout view. + """ - def author do - "Kolab Systems AG" - end + use Larus.Web, :view - def description do - gettext "Larus flying high." - end + def author do + "Kolab Systems AG" + end - def title do - gettext "[YOUR TITLE HERE]" - end + def description do + gettext "Larus flying high." + end + + def title do + gettext "[YOUR TITLE HERE]" + end end diff --git a/larus/web/views/page_view.ex b/larus/web/views/page_view.ex index 137315a..765596c 100644 --- a/larus/web/views/page_view.ex +++ b/larus/web/views/page_view.ex @@ -1,3 +1,7 @@ defmodule Larus.PageView do + @moduledoc """ + The Larus page view + """ + use Larus.Web, :view end diff --git a/larus/web/web.ex b/larus/web/web.ex index eab5f39..0f73fe8 100644 --- a/larus/web/web.ex +++ b/larus/web/web.ex @@ -1,81 +1,82 @@ defmodule Larus.Web do @moduledoc """ A module that keeps using definitions for controllers, views and so on. This can be used in your application as: use Larus.Web, :controller use Larus.Web, :view The definitions below will be executed for every view, controller, etc, so keep them short and clean, focused on imports, uses and aliases. Do NOT define functions inside the quoted expressions below. """ def model do quote do use Ecto.Schema import Ecto import Ecto.Changeset import Ecto.Query, only: [from: 1, from: 2] end end def controller do quote do use Phoenix.Controller alias Larus.Repo import Ecto import Ecto.Query, only: [from: 1, from: 2] import Larus.Router.Helpers import Larus.Gettext end end def view do quote do use Phoenix.View, root: "web/templates" # Import convenience functions from controllers - import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1] + import Phoenix.Controller, only: + [get_csrf_token: 0, get_flash: 2, view_module: 1] # Use all HTML functionality (forms, tags, etc) use Phoenix.HTML import Larus.Router.Helpers import Larus.ErrorHelpers import Larus.Gettext end end def router do quote do use Phoenix.Router end end def channel do quote do use Phoenix.Channel alias Larus.Repo import Ecto import Ecto.Query, only: [from: 1, from: 2] import Larus.Gettext end end @doc """ When used, dispatch to the appropriate controller/view/etc. """ defmacro __using__(which) when is_atom(which) do apply(__MODULE__, which, []) end end