Giter Club home page Giter Club logo

no_value_helper's Introduction

NoValueHelper

The Problem

This Ruby on Rails plugin tries to solve a common pattern when showing
values from the database. If you want to show a nice message like ‘no value’
when an optional attribute has been left empty, you usually need to do the
same thing over and over again:

<%= @user.name.blank? ? ‘no value’ : @user.name %>

It gets even worse when it’s about an optional relation, with some extra methods:

<%= user.daddy ? link_to(user.daddy.name, @user.daddy) : ‘no daddy’ %>

The Solution

So this plugin tries to shorten this:

<%= show(@user.name) %>

Or the second example:

<%= show(:link_to, @user.daddy) { @user.daddy.name } %>

Don’t worry, NoMethodErrors will be caught for you. That is why we use a block
in this case.

For the exact usage, read the specs.

Configuration

To translate the message, you can simply add the “no_value” key (no scope) to
your translation files.

nl-NL: no_value: geen waarde

By default the message is encapsulated by an em-tag with the class ‘no_value’.
To change this, set the class variable @@no_value_text with a lambda. This is
done so I18n.translate will work. Make an initializer
(in config/initializers/no_value_helper.rb), containing this:

module NoValueHelper @@no_value_text = lambda { “something more to your liking” } end

You can also change how this plugin checks for empty values. By default this is
done with the method blank?.
This means that empty strings are also treated as ‘no value’. To change this,
set the class variable @@no_Value_check_method to a lambda that does what you
want. Your initializer will look something like this:

module NoValueHelper @@no_value_check_method = lambda { |value| value.nil? } end

Copyright © 2008 Iain Hecker, released under the MIT license

no_value_helper's People

Stargazers

Angus H. avatar Joris Trooster avatar  avatar iain avatar Marcos Piccinini avatar Colin Schlueter avatar

Watchers

James Cloos avatar  avatar

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.