Code Reduction

J

Jigar Gosar

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] %>
<div class="MessageWrapper">
<span class="Message">
<%= flash[:notice] %>
</span>
</div>
<% end %>

<% if flash[:error] %>
<div class="MessageWrapper">
<span class="ErrorMessage">
<%= flash[:error] %>
</span>
</div>
<% end %>



thanks.
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

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 %>
<div class="MessageWrapper">
<span class="<%= k.to_s.capitalize %>Message">
<%= v %>
</span>
</div>
<% end %>
<% end %>

Cheers,
Arlen.
 
J

Jigar Gosar

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.
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

Hiya,

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. :) I recently read a good article to this effect:
"Do not learn Ruby"
http://webmat.wordpress.com/2008/02/20/do-not-learn-ruby/. It's a fun read.

Cheers,
Arlen.
 
J

Jigar Gosar

from: http://webmat.wordpress.com/2008/02/20/do-not-learn-ruby/

"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 :)

thanks for pointing it out.
 
R

Rob Biedenharn

Hiya,



Good point, I'm asleep!

Thanks a lot, that was a really quick reply.

No problems.



You're absolutely right. :) I recently read a good article to this
effect:
"Do not learn Ruby"
http://webmat.wordpress.com/2008/02/20/do-not-learn-ruby/. It's a
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 ;-)

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",
:eek:nClick => "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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Jigar Gosar

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,284
Messages
2,571,411
Members
48,104
Latest member
Hellmary01

Latest Threads

Top