Change Text Validation Rails 3 - ruby-on-rails

I would like to to do the following, any ideas
I have a Products model with two fields, both strings.
Name
Position
I would like on the index pages for the products to display the Name in Red if the Positions field is blank in the database.
Thanks in advance

Create helper method to check if object is blank?
def set_css_class(object, css_class)
" #{css_class}" if object.blank?
end
Call it in your View:
<div class="name <%= set_css_class(#poroduct.position, 'red') %>">
<%= #product.name %>
</div>

<span class="<%= product.position.nil? ? "red" : "blue" %>">
<%= product.name %>
</span>
Update:
Let's say you have code like you provided in comment, modify it to:
<td>
<%= link_to admin_printer_path(printer), :class => 'ico' do %>
<b<%= ' class="error"' unless printer.position? %>><%= printer.name %></b>
<% end %>
</td>
and in css file/section in header add (or modify according to your structure):
.error { color: red; }

Related

Changing the color of each item of a list in Rails

I have a model Runn that has the boolean field occupied. My goal is to list the ids of each Runn in a Ward, displaying the occupied Runns in Green and the empty Runns in red. Currently I have
<p>
<strong>List of Runns (ids)</strong>
<%= #ward.runns.ids %>
</p>
how do I access each individual runn to compare their occupied field?
You can iterate through them. I'm not sure how you want to tell whether it's 'occupied' or 'empty' - replace r.occupied? in the example below with your check.
<p>
<strong>List of Runns (ids)</strong>
<% #ward.runns.each do |r| %>
<span style="color: <%= r.occupied? ? 'green' : 'red' %>">
<%= r.id %>
</span>
<% end %>
</p>
You should give different class based on the boolean field as below:
<p>
<strong>List of Runns (ids)</strong>
<% #ward.runns.each do |r| %>
<span class="<%= r.occupied? ? 'runn-occupied' : 'runn-empty' %>">
<%= r.id %>
</span>
<% end %>
</p>
And in you css file, add different style for them (color, margin, size...):
.runn-occupied{
background-color: green;
}
.runn-empty{
background-color: red;
}

How can I reference the name of the previous array item in this Rails ERB mark-up loop?

I am a Rails noob. I am generating a menu in an ERB template, looping through the array "elements":
<% #elements.each do |element| -%>
<div class="category <%= element.category.color %>">
</div>
<% end %>
I want to add an additional class to each item (except the first, obviously), referencing the element.category.color of the PREVIOUS item, so that the final mark-up looks like:
<div class="category blue">I am the first, no extra class</div>
<div class="category green after-blue">I come after blue</div>
<div class="category yellow after-green">I come after green</div>
This is all basically a work-around to avoid using next-sibling CSS selectors, which are slow as hell in some of the browsers I need to support, and causing rendering problems when changing the background colours of the items.
Can I add the class I want directly in the ERB, or will I need to also use the controller to calculate it?
You can do this
<% #elements.each_with_index do |element, index| -%>
<div class=<%= "category #{element.category.color} " + ("after-#{#elements[index-1].category.color}" unless index == 0) %>>
</div>
<% end %>
Another way:
<% prev = '' %>
<% #elements.each do |element| -%>
<div class=<%= "category #{element.category.color}#{' after-' + prev if prev.present?}" %>>
<% prev = element.category.color %>
</div>
<% end %>

Rails add text to class in a content_tag_for :li

I have a content tag that is creating jquery sortable output.
Some items I don't want sortable, so I've added the following to the jquery:
cancel: ".ui-state-disabled"
So, now I need to put "ui-state-disabled" into the li class.
Currently the code creating the li is this:
<% wostatus.workorders.each do |workorder| %>
<%= content_tag_for(:li, workorder) do %>
<div class="<%= workorder.type.maximo_no %> <%= workorder.priority %> ">
<a href="<%= workorder_path(workorder) %>">
<strong><%= workorder.wonum %></strong>
<%= workorder.description %>
</a>
</div>
<% end %>
<% end %>
The results in HTML are:
<li class="workorder" id="workorder_36">
<div class=" ">
<a href="/workorders/36">
<strong>13-39870</strong>
Added some text again
</a>
</div>
</li>
In the browser, if I edit the li class to include "ui-state-disabled" it works the way I want.
Now, how can I insert the "ui-state-disabled" into the li status if the workorder.wostatus.id = 232 ??
Thanks for your help!!
Make a helper:
def disabled_workorder_li(workorder)
{:class => "ui-state-disabled"} if workorder.id == 232
end
Then in the view:
<%= content_tag_for(:li, workorder, disabled_workorder_li(workorder) %>
Here's what it would look like if you skipped the helper and tried to do it all in the view:
<%= content_tag_for(:li, workorder,
(workorder.id == 232) ? {:class => "ui_state_disabled"} : nil %>
That looks terrible. Putting it into a helper will also make it easier to test.

Help applying two different styles to Ruby on Rails pluralize method

How do I apply two different styles to the "vote" text and the number output from this RoR code:
<%= pluralize video.vote_sum, 'Vote' %>
Since you want to style the pluralized word, I would just suggest doing the following:
<%= video.vote_sum %>
<div class="style-word">
<% word = "Vote" %>
<% if video.vote_sum > 1 %>
<%= word.pluralize %>
<% else %>
<%= word %>
<% end %>
</div>
(Sorry about all the <% %>, I'm so used to HAML now, I don't do erb anymore).
Try this :
<div class="<%= pluralize video.vote_sum, 'number' %>">
<%= video.vote_sum %>
</div>
<div class="<%= pluralize video.vote_sum, 'vote' %>">
<%= pluralize video.vote_sum, 'Vote' %>
</div>
And in your CSS:
.vote{# some style code here!}
.votes{# some style code here!}
.number{# some style code here!}
.numbers{# some style code here!}
<%= pluralize video.vote_sum, '<span class="some_other_class">Vote</span>', '<span class="some_other_class">Votes</span>' %>
I just ran into this exact situation and chose to extend the pluralize method in application_helper.rb
def pluralize(count, singular, plural = nil)
count, counted = super.split(' ', 2)
[content_tag(:span, count, class: 'count'),content_tag(:span, counted, class: 'counted')].join(' ').html_safe
end
pluralize(2, "Thing") will now output something like <span class="count">1</span> <span class="counted">Things</span>

How can I change the tags/css class of some dynamic text based on output from a helper?

I have a repeated line which outputs something like:
Call John Jones in -3 days (status)
I have a helper called show_status(contact,email) which will output whether that particular email had been sent to that particular contact.
If it is "sent," then that entire line should show up as "strike out."
Similarly, if the number of days is -3 (<0), the line should be formatted in red.
Here's my hack, but there must be a cleaner way to put the logic into the controller?
I hard-code a value that wraps around the lines I want formatted, and assign the value based on a separate call to the same helper:
<% for call in #campaign.calls %>
<% if !show_call_status(#contact,call).blank? %>
<%= strike_start = '<s>'%>
<%= strike_end = '</s>' %>
<% end %>
<p>
<%= strike_start %>
<%= link_to call.title, call_path(call) %> on
<%= (#contact.date_entered + call.days).to_s(:long) %> in <%= interval_email(#contact,call) %>
days
<%= make_call(#contact,call) %>
<span class='status'><%= show_call_status(#contact,call) %></span>
<%= strike_end %>
</p>
<% end %>
I guess what I'd like to do is not have the if statement in the View. Not sure how to do this.
Basically, I would put a class on the p tag based on the status and have the CSS decide what needed to be done.
So the view:
<% for call in #campaign.calls %>
<p class="<%= call_status_class(#contact, call) %>">
<%= link_to call.title, call_path(call) %> on
<%= (#contact.date_entered + call.days).to_s(:long) %> in <%= interval_email(#contact,call) %>
days
<%= make_call(#contact,call) %>
<span class='status'><%= show_call_status(#contact,call) %></span>
</p>
<% end %>
And another helper:
def call_status_class(contact, call)
# do what you have to do to figure out status
if overdue
return 'overdue'
elsif called
return 'called'
else
return 'standard'
end
end
Then in CSS:
.standard {
...
}
.overdue {
color: red;
}
.called {
text-decoration: line-through;
}
Pick and choose. I can't really give you full fledged solution without seeing all the helper functions. Hope this helps.

Resources