Code Reduction

I am using ERB templates in my code. It seems too big, can’t get my head
around on how to reduce it. I am sure this can be made smaller. My
problem is that if there ain’t any flash messages than the contained
HTML shouldn’t be rendered at all.

<% if flash[:notice] %>



<%= flash[:notice] %>


<% end %>

<% if flash[:error] %>



<%= flash[:error] %>


<% end %>

thanks.

Hey,

If you call your CSS class for a `notice’ flash NoticeMessage isntead of
just Message, you can make it simple like this:

<% flash.each do |k, v| %>
<% if v %>



<%= v %>


<% end %>
<% end %>

Cheers,
Arlen.

Hiya,

On Fri, Feb 22, 2008 at 2:07 AM, Jigar G. [email protected]
wrote:

The “if v” can be removed, cause if the key exists than there is no
problem in rendering its value.

Good point, I’m asleep!

Thanks a lot, that was a really quick reply.

No problems.

I just love ruby, cause so much can be expressed in so less.

You’re absolutely right. :slight_smile: I recently read a good article to this
effect:
“Do not learn Ruby”
Do not learn Ruby « Programblings. It’s a fun
read.

Cheers,
Arlen.

Cool, thanks.

The “if v” can be removed, cause if the key exists than there is no
problem in rendering its value.

Thanks a lot, that was a really quick reply.

I just love ruby, cause so much can be expressed in so less.

Arlen C. wrote:

Hey,

If you call your CSS class for a `notice’ flash NoticeMessage isntead of
just Message, you can make it simple like this:

<% flash.each do |k, v| %>
<% if v %>



<%= v %>


<% end %>
<% end %>

Cheers,
Arlen.

from: Do not learn Ruby « Programblings

"Ruby will get under your skin. You will miss its features and quirks
when you’re not using it. You might even find other languages
insufferable, once you get comfortable with Ruby.

After you’ve started using Ruby, there’s a significant chance you’ll
start loathing whatever code base you currently have to work on.
Especially if it’s a statically compiled language. A code base you used
to think was ok, except for its few quirks."

thats exactly what I am experiencing now :slight_smile:

thanks for pointing it out.

Arlen C. wrote:

Hiya,

On Fri, Feb 22, 2008 at 2:07 AM, Jigar G. [email protected]
wrote:

The “if v” can be removed, cause if the key exists than there is no
problem in rendering its value.

Good point, I’m asleep!

Thanks a lot, that was a really quick reply.

No problems.

I just love ruby, cause so much can be expressed in so less.

You’re absolutely right. :slight_smile: I recently read a good article to this
effect:
“Do not learn Ruby”
Do not learn Ruby « Programblings. It’s a fun
read.

Cheers,
Arlen.

On Feb 21, 2008, at 10:14 AM, Arlen C. wrote:

Thanks a lot, that was a really quick reply.
fun read.

Cheers,
Arlen.

Or put one of these in your application_helper (not both, or combine
them yourself) (Apologies for the inevitable email wrapping :wink:

module ApplicationHelper

COLORS = Hash.new(‘#ff0000’).merge!({ :notice => ‘#00ff00’,
:warning => ‘#ff9900’,
:error => ‘#ff0000’,
}) unless const_defined? ‘COLORS’

Shows the flash message(s) with a highlight effect

def flash_div(*keys)
keys.collect { |key|
content_tag(:div, flash[key],
:id => “#{key}”,
:class => “flash”) if flash[key]
}.join +
keys.collect { |key|
javascript_tag(visual_effect(:highlight, “#{key}”,
:startcolor => “‘#{COLORS[key]}’”,
:endcolor => “‘#ffffff’”)) if
flash[key]
}.join
end

Shows the flash message(s), but they go away when clicked. An

options hash will be applied to a surrounding div tag.
def flash_div(*keys)
return ‘’ if keys.blank?
options = { :id => ‘flash’ }
options.update(keys.pop) if keys.last.is_a? Hash
content_tag(:div,
keys.collect { |key|
content_tag(:div,
flash[key], :id => “#{key}-
flash”, :class => “#{key} section”,
:onClick => “new Effect.Fade(this);”)
if flash[key]
}.join,
options)
end

end

And then in your views (or layout):

   <%= flash_div :notice, :warning, :error -%>

Then toss in some CSS to taste and serve warm.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

thanks this is cool too.

I don’t know why, but I don’t like generating HTML pragmatically.

Maybe the fear came from outputting HTML directly from Servlets as java
strings. Ahaa those were the days :slight_smile:

Rob B. wrote:

Or put one of these in your application_helper (not both, or combine
them yourself) (Apologies for the inevitable email wrapping :wink: