Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Forked from joemsak/example_class.rb
Created May 24, 2011 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/989066 to your computer and use it in GitHub Desktop.
Save tenderlove/989066 to your computer and use it in GitHub Desktop.
instance has?
class Example < ActiveRecord::Base
#...
# would this be cool to have, or am I daft?
def has? *args
args.any? { |a| send(a.to_sym) }
end
# irc chan said to drop the to_sym but I like having the failsafe
# also thinking about maybe has_any? & has_all? for clarification
#...
end
<% if @instance.has? :city, :state, :zipcode -%>
<p class="address">
<%= @instance.city %><% if @instance.has? :city -%>, <% end -%><%= @instance.state %> <%= @instance.zipcode %>
</p>
<% end -%>
@citrus
Copy link

citrus commented May 24, 2011

I like it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment