0.01
No commit activity in last 3 years
No release in over 3 years
Display your Rails models easily
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

SimpleDisplay

Showcase your Rails models easily.

SimpleDisplay aims to provide a flexible way to display your model attribute and methods, so you can move from this:

<dl>
  <% if @book.title.present? %>
    <dt>Title</dt>
    <dd><%= @book.title %></dd>
  <% end %>

  <% if @book.price.present? %>
    <dt>Price</dt>
    <dd><%= number_with_currency @book.price %></dd>
  <% end %>
</dl>

To this:

<%= display_for @book do |d| %>
  <dl>
    <%= d.display :title %>
    <%= d.currency :price %>
  </dl>
<% end %>

This gem was inspired by this post by Ben West in the Quick Left blog. Thank you! <3

Installation

Add this line to your application's Gemfile:

gem 'simple_display', github: 'mrcasals/simple_display'

And then execute:

$ bundle

Or install it with:

$ gem install simple_display --pre

Usage

Simply install simple_display and start using it:

<%= display_for @book do |d| %>
  <dl>
    <%= d.display :title %>
    <%= d.currency :price %>
  </dl>
<% end %>

Custom displayers

You can use custom displayers. Create an app/displayers folder in your Rails app and add your displayers there. For instance, if you want to create an displayer that parses a text as markdown and outputs it as HTML using the Kramdown gem, you could do this:

# app/displayers/markdown_displayer.rb
class MarkdownDisplayer < SimpleDisplay::Displayers::Base
  def display_value(field_value, &block)
    Kramdown::Document.new(field_value).to_html.html_safe
  end
end

Then you can use it in your Rails views:

<%= display_for @book do |d| %>
  <dl>
    <%= d.display :title %>
    <%= d.currency :price %>
    <%= d.markdown :description %>
  </dl>
<% end %>

TODO

  • Add tests & docs (sorry!)
  • Add more displayers

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request