component based frameworks

Phoenix Models and Migrations Compared to Rails Framework

The structure of the web application on both platforms is quite similar. And it’s not surprising: one of the most significant members of the Phoenix community is a former member of the core team Rail, and most of the people involved in the development of the framework are rubies. Some differences still exist.

Implementing the Clean Architecture approach for building an application. The most significant changes came along with the latest version of Phoenix at the moment. Separating parts of the Phoenix from parts of the application, limiting business logic to contexts, using repositories to work with the database – all this is not typical for Rail’s world.

Umbrella apps are the idea of ​​not using microservices prematurely and splitting the bulk business logic into parts within one large application.

The views in Phoenix are called templates and look more like decorators, but theoretically, this shouldn’t cause any particular difficulties, although it changes the idea of ​​interaction, making it more like the now popular Hanami.

Another of the features of Phoenix is ​​the channels. Every time you connect to the channel, a new process is created, which in the future, when an error occurs, won’t affect other connections in any way. In addition, Phoenix offers a front-end library with excellent abstractions for connections with an unstructured web socket.

Also, there is a huge number of small differences, ranging from built-in tools for working with the front-end, ending with different ideas and philosophy than Rail.

Phoenix Framework draws heavily upon important foundations in the opinionated web frameworks that came before it, such as Ruby on Rails. Though it is not quite as opinionated as rails, its still very opinionated and any rails developer will feel very much «at home» there.

Migrations:

  1. Rails.

class CreateBooks < ActiveRecord::Migration[5.0] def change
create_table :books do |t|
t.string :title
t.text :description
t.decimal :price
t.string :image_url

t.timestamps
end
end
end

  1. Phoenix.

defmodule Storex.Repo.Migrations.CreateStoreBooks do
use Ecto.Migration

def change do
create table(:store_books) do
add :title, :string
add :description, :text
add :price, :decimal
add :image_url, :string

timestamps()
end
end
end

Templates:

  1. Rails.

<section class=”book-list”>
<ul class=”grid”>
<% @books.each do |book| do %>
<li class=”grid-item”>
<div class=”book book-small”>
<h2 class=”book__title”><%= book.title %></h2>
<span class=”book__price”><%= book.price %></span>
</div>
</li>
<% end %>
</ul>
</section>

  1. Phoenix.

<section class=”book-list”>
<ul class=”grid”>
<%= for book <- @books do %>
<li class=”grid-item”>
<div class=”book book-small”>
<h2 class=”book__title”><%= book.title %></h2>
<span class=”book__price”><%= book.price %></span>
</div>
</li>
<% end %>
</ul>
</section>

Igor Grigorenko

Add comment