Skip to content

jefvlamings/view_model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

viewmodel

A ViewModel layer for Ruby On Rails

Installation

Add viewmodel to your Rails application

bundle add viewmodel

Usage

Create a ViewModel object in a corresponding view_models folder

app/
├─ view_models/
│   └─ products/
│       └─ show_view_model.rb
├─ views/
│   └─ products/
│       └─ show.html.erb

The contents of the view model look something like this:

class Products::ShowViewModel < ViewModel
  def internal_reference
    [@product.id, @product.name].join("-")
  end
end

The view would then look like this;

<div id="product">
  <h1><%= @product.name %></h1>
  <h2><%= @internal_reference %></h2>
</div>

Core concepts

  • All instance variables and local variables (local_assigns) are passed to the view model.
  • All attributes and public methods are exposed to the view
  • You can override local variables in the view model with public methods
  • You can add new local variables in the view model with public methods

About

A ViewModel layer for Ruby On Rails

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published