Skip to main content

Ecto "inheritance" using macros

Oooh them's fighting words. Anyways, this is definitely not true inheritance by any means, but let me explain the problem and then the solution.

Ecto is the primary way you connect to the database in Phoenix. However, Ecto differs in many significant ways from ActiveRecord in Rails. You can set up a schema file which lists the relationships between your "models" or "entities", but these schemas do not allow you to directly access the database. They are merely descriptions of how the data is expressed.

To access the database, you need to use a data context which uses Ecto.

So, from my article comparing Active Record to Ecto (http://www.skuunk.com/2020/01/comparing-rails-active-record-pattern.html)


defmodule Schema.Article do
  use Ecto.Schema
  import Ecto.Changeset

  alias Schema.Author
  alias Schema.Comment

  @permitted_fields [:title, :content]
  @required_fields [:title, :content]

  schema "article" do
    belongs_to(:author, Author)
    has_many(:comments, Comment)
    field(:title, :string)
    field(:content, :string)

    timestamps()
  end

  def changeset(article, params) do
    article
    |> cast(params, @permitted_fields)
    |> validate_required(@required_fields)
  end
end

defmodule Context.Articles do
  import Ecto.Query, warn: false
  alias Context.Repo

  alias Schema.Article

  def find(id) do
    Repo.get(Article, id)
  end

  ...
  #implement other data access methods as required
  ...
end
This is all well and good and it does work, however you will soon discover that there are some very common methods you will need over and over again. Basically the CRUD operations (Create, Read, Update and Delete) but you might also have some other common variations (for example you can find by id or find by other attributes).

Wouldn't it be nice if like in Rails, you could just "inherit" these methods on all your contexts so you would not have to write a custom find method each time. Well, using macros you can!

In Elixir, macros are the metaprogramming framework which allows you to basically use code to write code. In fact the if function in Elixir is basically a macro.

On top of macros, you need to know how to use code from other files. Read up on the use macro for more details. https://elixir-lang.org/getting-started/alias-require-and-import.html#use

Anyways, let's maybe start with the code and then explain it later...


defmodule Services.Data.Common do
  defmacro __using__(values) do
    schema = Keyword.get(values, :schema)

    quote bind_quoted: [schema: schema] do
      require Ecto.Query
      alias Ecto.Query
      alias Services.Repos.Repo
      @schema schema

      def find(id) when is_atom(preloads) or is_list(preloads) do
        @schema
        |> Repo.get(id)
      end

      # ... more common database functions
    end
  end
end

This is a common data services module. Any functions you create in here will filter through to any other modules that use it. In fact this is how we import the Ecto.Schema functionality into our Schema module.

To use this, we will call it from the Context module


defmodule Context.Articles do
  import Ecto.Query, warn: false
  alias Context.Repo

  alias Schema.Article

  use Services.Data.Common, schema: Articles

end

As you can see, we were able to remove the find function from Articles as it is using Common. We are passing the schema into the macro so that we can also use this macro with Author or Comment (or any other schema).

Okay, so this is really not inheritance, it's composition, but it has the same net effect. When you compile this, essentially we are adding all these functions to the data contexts.

The one major difference from inheritance is you cannot then override find from inside your own context (you will have to give it a different name I am afraid).

So a little more explanation might be needed. Importing a module with the use macro not only imports the functions, but also calls the __using__ function. Calling the quote function inserts all this code into the virtual version of your module. bind_quoted allows you to make sure that the variables you pass in are available at runtime (and are not precompiled). In this case, we want to make sure the schema can be passed in at runtime so it is in fact variable.

And that is how a little metaprogramming can go a long way.

Comments

Popular posts from this blog

Freezing Gems

What is a gem and why would you want to freeze it?

In Ruby, there are times when you want to access pieces of functionality that other people of written (3rd party libraries) and you normally have 2 options. You can install a plug in or install a gem. Normally the method you use is determined by which ever is made available by the author.

Gems are installed on the host machine and are pretty handy when you want to run things in the command line or else across lots of projects, but their downside is that if you use a gem in a Rails project there is no automatic publishing mechanism when you deploy your site. You will need to log onto the remote host machine and install the gem manually.

Plugins are specific to Rails and are similar to gems in that they are also 3rd party libraries. However they are associated with your Rails project as opposed to your machine so they will get posted to the server on a regular deploy.

Freezing a gem is the process of transforming a gem into a plug in. Essen…

Unit/Functional Testing RubyAMF

One of my current projects is using RubyAMF to communicate with Flash (http://rubyforge.org/projects/rubyamf/). On the whole this is really nice because it allows you to transfer Ruby objects directly to ActionScript ones (as opposed to translating the object into XML, sending the XML and then recreating the object in ActionScript).
However, Rails does not provide a built in transport mechanism for AMF, so we cannot run functional testing directly on the data call (as we could for an XML or HTML transport layer). This is a show stopper for a lot of people (Rails w/o Unit testing = a big mess of trouble when something goes wrong).
We can though serve both the HTML and the AMF formats depending on the request format. This means that we can test the object instantiation logic and make sure there are no errors in the controllers (though we cannot check the actual format of the data being served). In the controller, instead of rendering AMF alone, do the following respond_to do |format|

Comparing Rails' Active Record Pattern with Phoenix/Elixir/Ecto

Rails has a very well established Active Record pattern for dealing with the database. You have an Active Record model which maps to the database table, the schema of the model comes directly from the database schema and you place your model specific methods on the Active Record model. This file is also where you set your model relationships (e.g. has_many, has_one, belongs_to). Your instance of the model has all the methods built in.

In Ecto/Phoenix it's a little different. First of all, the database schema doesn't automatically map to the "model". In fact we don't really have models (as Elixir is a functional paradigm). What happens in one file in Rails, happens in essentially two (or more). You have a schema file (where you have to list out all the attributes and relationships). Using the schema file, your "instance" is essentially a data structure (with no methods on it). If you want to transform the data on your struct, you would use a context modu…