elixir

Define a constant module in elixir

≡放荡痞女 提交于 2019-12-11 13:40:56
问题 I would like to create some changeable boundary module. Ideally the result would look like any other module but the behaviour could be set at compile time or in the configuration files. I think I am looking for something like define in erlang Say I have a SystemClock module and a DummyClock tuple module. Ideally the Clock module would be one or other of the two modules above chosen in the config file. In config/test.ex define(Clock, {DummyClock, 12345678}) Later Clock.now # => 12345678 In

Reverse concat in Elixir

◇◆丶佛笑我妖孽 提交于 2019-12-11 12:56:49
问题 Is there a standard library function in Elixir (or Erlang) to concatenate the reverse of a list in front of some other list? Basically I'm looking for an equivalent of reverse_::: in Scala. The rationale is that it's handy when implementing a tail-recursive algorithm on a list. During recursion, you hold on to some of the elements for later by adding them onto the front of an accumulator list. In the end you can reverse-concat them onto the remainder of the assembled list in one go (which

Can the Ecto schema field name different from column name?

时光毁灭记忆、已成空白 提交于 2019-12-11 12:55:35
问题 I've got a legacy database that I'm trying to pull into Ecto. In it there's an orders table which has an order_status_id column. order_status_id maps to a set of constants in the legacy system. I'd like to have the MyApp.Order struct contain an order_status field, which has a custom type that converts the integer IDs to meaningful atoms. I've got the custom type working, but I can't figure out how to map a field named order_status to a column named order_status_id . The legacy system is still

can not generate phoenix app

三世轮回 提交于 2019-12-11 10:11:50
问题 i want try Phoenix framework, but can not take it I install elixir brew install elixir , after it, i install Phoenix mix archive.install /path/to/archive and try mix phoenix.new new_app and get the error: ** (UndefinedFunctionError) undefined function:crypto.strong_rand_bytes/1 (module :crypto is not available) :crypto.strong_rand_bytes(64) lib/phoenix_new.ex:459: Mix.Tasks.Phoenix.New.random_string/1 lib/phoenix_new.ex:187: Mix.Tasks.Phoenix.New.run/4 (mix) lib/mix/cli.ex:55: Mix.CLI.run

Insert null into ecto belongs_to field

末鹿安然 提交于 2019-12-11 09:45:39
问题 I have an ecto model defmodule App.Profile do use App.Web, :model alias App.Repo schema "profiles" do belongs_to :user, App.User field :name, :string field :last_name, :string field :second_surname, :string timestamps end But sometime I want to save to database and put the user to nil, Need I to add some flag to the user field? I have this code in my controller changeset = Profile.changeset(%Profile{user_id: nil}, profile_params) But when try to save I got this error :erlang.byte_size({:user

Websockets on elixir code

天涯浪子 提交于 2019-12-11 08:49:46
问题 I'm currently creating a chat using elixir. but anytime i try running the app, the websocket gives an error on sever console The client's requested channel transport version "2.0.0" does not match server's version requirements of "~> 1.0" And browser console: WebSocket connection to 'ws://localhost:4000/socket/websocket token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9

Elixir / Erlang Dialyzer : Why behaviour callback's param type should be subtype instead of supertype?

£可爱£侵袭症+ 提交于 2019-12-11 08:48:48
问题 I have a behaviour X and a callback function with parameter type: %{a: any} Module Y implements behaviour X and the callback function in implementing module Y has parameter type: %{a: any, b: any} Dialyzer doesn't like that and complains: (#{'a':=_, 'b':=_, _=>_}) is not a supertype of #{'a':=_} This implies dialyzer attempts to determine if callback parameter's type in implementing module Y is a supertype of param type in behaviour X. In other words, it asks: Is behaviour X's callback param

Pattern match on value of Map's key in elixir

∥☆過路亽.° 提交于 2019-12-11 08:11:31
问题 I want to pattern match on the value of a key in a map def handle_in("new_message", payload, socket) do case payload.message do "hello" -> broadcast! socket, "new_message", payload end {:noreply, socket} end [error] GenServer #PID<0.378.0> terminating ** (KeyError) key :message not found in: %{"message" => "hello", "name" => "x"} (chatroom) web/channels/lobby_channel.ex:9: Chatroom.LobbyChannel.handle_in/3 (phoenix) lib/phoenix/channel/server.ex:226: anonymous fn/4 in Phoenix.Channel.Server

How to write a Ecto query that does a group_by MONTH on a datetime field

落爺英雄遲暮 提交于 2019-12-11 07:39:18
问题 I am doing a ecto query and am trying to group by q.created_date . This query successfully does the GROUP BY but it does it by the second. I am trying to group by month instead. MYQUERY |> group_by([q], [q.created_date, q.id]) Is there something like: MYQUERY |> group_by([q], [month(q.created_date), q.id]) 回答1: You can use fragment with date_part('month', field) to extract the month in PostgreSQL: fragment("date_part('month', ?)", p.inserted_at)) iex(1)> from(Post) |> select([p], p.inserted

Remember me functionality in Phoenix using Guardian

浪尽此生 提交于 2019-12-11 07:20:05
问题 I'm developing a login system for a web application using Guardian to handle authentication. In my Guardian config i have ttl: {30, :days} User's token is stored in cookies by calling: defp login(conn, user) do conn |> Guardian.Plug.sign_in(user) end Like this, token is valid for 30 days and stays there even if browser is closed (expected behaviour for a cookie). User, however, should be able to choose if being remembered or not during login. If not, token must be deleted from cookies upon