Giter VIP home page Giter VIP logo

short_circuit's Introduction

short_circuit

Gem Version Build Status Code Climate

Short Circuit adds simple presenters for Rails views.

Usage

Install the gem in your Gemfile:

gem 'short_circuit'

Include short_circuit in your model:

# app/models/user.rb
 
class User < ActiveRecord::Base
  include ShortCircuit::Presentable

  attr_accessible :first_name, :last_name, :job_title, :member_since
end

Create a presenter:

# app/presenters/user_presenter.rb
 
class UserPresenter < ShortCircuit::Presenter
  def first_name
    @user.first_name.titleize
  end
 
  def full_name
    "#{first_name} #{last_name}"
  end
 
  def job_title
    @user.job_title || 'not listed'
  end
 
  def member_since
    @user.member_since.to_formatted_s(:long) 
  end
 
  def error_response(method, *args, &block)
    link_to 'N/A', root_path
  end
end

No additional controller code is necessary. Retrieve the model as you normally would:

# app/controllers/users_controller.rb
 
class UsersController < ApplicationController
  def show
    @user = User.find(params['id'])
  end
end

In the view, you don't need to instantiate any presenter/decorator objects, just use the model:

# Normal method vs presenter method:
@user.first_name # john
@user.present :first_name # John
 
 
# Missing presenter method is delegated to model:
@user.last_name # smith
@user.present :last_name # smith
 
 
# Method defined on presenter object:
@user.full_name # NoMethodError
@user.present :full_name # John smith
 
 
# Formatted data returned by presenter method:
@user.member_since # 2013-02-28 20:46:32 UTC
@user.present :member_since # February 28, 2013 20:46
 
 
# Presenter method prevents nil error by returning default data:
@user.job_title.upcase # undefined method 'upcase' for nil:NilClass
@user.present(:job_title).upcase # NOT LISTED
 
 
# Presenter method silences nil error and returns alternative content:
@user.not_a_real_method # NoMethodError
@user.present :not_a_real_method # <a href="/">N/A</a>
 
 
# Present! method does not silence errors:
@user.present! :not_a_real_method # NoMethodError

Design goals

  • Minimize project size and scope
  • Preserve direct access to model attributes
  • Create a separate access point for presenter methods
  • Silence accessor errors by default
  • Delegate missing presenter methods to the model object

short_circuit's People

Contributors

cj avatar

Watchers

 avatar James Cloos avatar

Forkers

acdcorp

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.