PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Code Reduction
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Code Reduction

Réponse
 
LinkBack Outils de la discussion
Vieux 21/02/2008, 15h56   #1
Jigar Gosar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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] %>
<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.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 21/02/2008, 16h02   #2
Arlen Cuss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Code Reduction

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

  Réponse avec citation
Vieux 21/02/2008, 16h07   #3
Jigar Gosar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Code Reduction

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


--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 21/02/2008, 16h14   #4
Arlen Cuss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Code Reduction

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

Hiya,

On Fri, Feb 22, 2008 at 2:07 AM, Jigar Gosar <jigar.gosar@gmail.com> 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. I recently read a good article to this effect:
"Do not learn Ruby"
http://webmat.wordpress.com/2008/02/...ot-learn-ruby/. It's a fun read.

Cheers,
Arlen.

  Réponse avec citation
Vieux 21/02/2008, 16h20   #5
Jigar Gosar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Code Reduction

from: http://webmat.wordpress.com/2008/02/...ot-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.

Arlen Cuss wrote:
> Hiya,
>
> On Fri, Feb 22, 2008 at 2:07 AM, Jigar Gosar <jigar.gosar@gmail.com>
> 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. I recently read a good article to this
> effect:
> "Do not learn Ruby"
> http://webmat.wordpress.com/2008/02/...ot-learn-ruby/. It's a fun
> read.
>
> Cheers,
> Arlen.


--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 21/02/2008, 16h28   #6
Rob Biedenharn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Code Reduction


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

> Hiya,
>
> On Fri, Feb 22, 2008 at 2:07 AM, Jigar Gosar <jigar.gosar@gmail.com>
> 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. I recently read a good article to this
> effect:
> "Do not learn Ruby"
> http://webmat.wordpress.com/2008/02/...ot-learn-ruby/. It's a
> fun read.
>
> Cheers,
> Arlen.



Or put one of these in your application_er (not both, or combine
them yourself) (Apologies for the inevitable email wrapping ;-)

module Applicationer

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",
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
Rob@AgileConsultingLLC.com


  Réponse avec citation
Vieux 21/02/2008, 16h38   #7
Jigar Gosar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Code Reduction

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

Rob Biedenharn wrote:
>
> Or put one of these in your application_er (not both, or combine
> them yourself) (Apologies for the inevitable email wrapping ;-)
>

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h18.


Édité par : vBulletin® version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12783 seconds with 15 queries