Cut off text exceeding a certain length - abap

I have a list with texts with lengths ranging from 1 character to several thousands. I want to cut off all texts exceeding 255 characters. How can I do that?
Do I have to check the length of each String and then cut it with (255) or is there a more elegant expression?
Edit: like this
<% IF STRLEN( wa_comm-text ) > 255. %>
<%= wa_comm-text(255) %> ...
<% ELSE. %>
<%= wa_comm-text %>
<% ENDIF. %>
this is BSP
Thanks in advance

The other option is:
<%
data: ls_text(255) type c.
ls_text = wa_comm-text.
%>
<%= ls_text %>
Because you obviously cannot use substrings on strings, and if they are shorter, you will get a runtime error.

I created for this a 'string solutions' class called zss, with a static method that will cut off a given string and the given length.
Then you can just do something like this:
<%= zss=>left( s = wa_comm-text cutoff = 255 ). %>
or even a more specific method
<%= zss=>left255( wa_comm-text ). %>

Just as an option:
<%= CONV char255( wa_comm-text ) %>
inline conversion and trimming to target type is done here.

Related

Rails form - search engine

I try to create simple search engine but I meet some problmes. I have several search_field in my form and if either is empty should returns all objects. Otherwise when it has any content it should be selected by that content. Below is my sample form:
<%= form_for :product, url: products_path, method: :get do |form| %>
<%= form.search_field :brand %>
<%= form.search_field :model %>
<%= form.search_field :price_from %>
<%= form.search_field :price_to %>
<%= form.submit 'Submit' %>
<% end %>
my model method:
def self.search(search)
where(brand: search[:brand]).where(model: search[:model]).where("price >= ?", search[:price_from]).where("price <= ?", search[:price_to])
end
But the above piece of code is wrong because if I leave some field empty it is treated directly as empty string instead of ignore this field and final result is not correct.
Summary this form should work similarly to filter on online store
You'd could do something like this
def self.search(search)
results = all
results = results.where(brand: search[:brand]) if search[:brand]
results = results.where(model: search[:model]) if search[:model]
results = results.where("price >= ?", search[:price_from]) if search[:price_from]
results = results.where("price <= ?", search[:price_to]) if search[:price_to]
return results
end
Good luck.

Rails group_by and in_groups_of error

I have a list of organisations, grouped and displayed by their name, in alphabetical order. I want to display these across 4 columns for each letter, i.e.:
A
A... A... A... A...
A... A... A... A...
...
Z
Z... Z...
I have used the following code:
<% #organisations.keys.sort.each do |starting_letter| %>
<div class="page-chunk default">
<h6><%= starting_letter %></h6>
<% #organisations[starting_letter].each do |organisations| %>
<% organisations.in_groups_of(4).each do |column| %>
<div class="one_quarter">
<% column.each do |organisation| %>
<%= link_to organisation.name, organisation_path(organisation) %><br />
<% end %>
</div>
<% end %>
<% end %>
</div>
<% end %>
And in the controller:
#organisations = Organisation.all.group_by{ |org| org.name[0] }
But get undefined methodin_groups_of' for #for my troubles. If I change the code to#organisations[starting_letter].in_groups_of(4).each do |organisations|then I get aNilClass` error.
What have I done wrong and how should I fix it?
Try organisations.in_groups_of(4, false)
Without the false, it will fill in any empty spots in the last group with nils, which means it will try to call name on nil.

howto globally substitute nil values with a specific character (e.g. "-") in rails views

I guess it's a simple question, but how can I replace nil values in generell in my views.
I want to avoid having something like
<% unless value == nil %>
<%= value %> Ohm
<% else %>
<p>-</p>
<% end %>
Where is the best place to handle this?
I generally put little formatters like this in a helper:
module ResistorsHelper
def format_resistance(resistance)
resistance.nil? ? content_tag(:p, '-') : "#{resistance} Ohm"
end
end

Rails mailer template calculation

I am new to rails and am writing a daily report email template.
I am Outputting unique visitors, and calculating the difference between the 2 and displaying that as well with a + or - sign depending on if its positive or negative.
Is there a better way to do this? Should I not be doing math inside the view?
Unique Visitors: <%= number_with_delimiter(#stats["unique_visitors"]) %>
<% uniquediff = #stats["unique_visitors"] - #stats["unique_visitors_yesterday"] %>
(<% if uniquediff > 0 then %> + <% else %> - <% end %> <%= uniquediff %>)<br />
Try:
("+" if uniquediff>=0)+uniquediff.to_s
.to_s turns uniquediff to a string, and the ("+" if uniquediff>=0) bit evaluates to "+" if uniquediff is greater than or equal to zero, and nothing otherwise.. and you will already have a "-" if it is negative.
=]
How about this:
<% unique_diff = #stats['unique_visitors'] - #stats['unique_visitors_yesterday'] %>
<%= "Unique Visitors: #{number_with_delimiter(#stats['unique_visitors'])} #{'+' if unique_diff > 0}#{unique_diff}" %><br/>
It's recommended to do logical stuff in HELPER(and it's what a helper should do).
# In helper, eg. application_helper.rb
def unique_diff(stats)
unique_diff = stats['unique_visitors'] - stats['unique_visitors_yesterday']
(unique_diff > 0) ? "+#{unique_diff}" : "#{unique_diff}"
end
# In view
Unique Visitors Diff: <%= unique_diff #stat %>

Rails 3 custom validation: Highlighting offending fields

I'm writing my first custom rails validation, and would like to tag the offending class with an html "error" class if they return false - I can't quite figure out how to do it. Relevant validation code below - any help appreciated.
(If it makes a difference, I'm using jQuery)
validates_each :shop do |record, attr, value|
shopvar = record.shops.map{ |s| s.email.downcase.strip }
if shopvar.count != shopvar.uniq.count
record.errors.add(attr, 'has the same email address entered more than once')
#record.errors[attr] << "You have entered this shop in the form twice"
end
end
So in your form you'd have something like this for an input field
<%= form.text_field :title %>
Since errors is a hash you could use the "include?" method like so...
errors.include?(:title)
This tells you that there's something wrong with this field. Now all you need to do is style it.
Whack on a ternary operator asi...
<% css_class = errors.include?(:title) ? "highlight_error_class" : "no_problem_class" %>
<%= form.text_field :title, :class => css_class %>
Done.