html_safe on whole html code block in rails - ruby-on-rails

i can't seem to find a way to escape whole html block, if it is even possible.
Here is what i am trying to do:
%table
%tr
%th{rowspan: "2"}= t("public.home.graphic_title_payment").html_safe
%th.datefield{rowspan: "2"}= t("public.home.graphic_title_dates").html_safe
%th{colspan: "3"}= t("public.home.graphic_title_pay_group").html_safe
%th{rowspan: "2"}= t("public.home.graphic_title_unpaid").html_safe
Altho i dont like purring .html_safe at the end of every string. Is there a way i could maybe put some kind of indicator that all %tr needs to be html_safe??
Thanks :)

Rails escapes ERB output by default. So the best thing would probably be to create a new helper method to run the I18n translation and mark it as safe. So perhaps:
# app/helpers/application_helper.rb
def st(translation)
t(translation).html_safe
end
Then:
%th{rowspan: "2"}= st("public.home.graphic_title_payment")

Related

Is there a helper for creating handlebars blocks in haml?

Because handlebars tags are evaluated after haml is compiled to html, and handlebars counts as plane text in haml you cannot indent logic
{{#if misc}}
%b Misc Products
Total:
{{misc_total}}
{{#each misc}}
{{price_charged}}
{{notes}}
{{/each}}
{{/if}}
Which is hard to read.
I made a rails helper to make this better looking*.
def handlebars_helper(helper, &block)
raise ArgumentError, "Missing block" unless block_given?
open = ActiveSupport::SafeBuffer.new("{{##{helper}}}") # helper opening
open.safe_concat capture(&block)
open.safe_concat("{{/#{helper.split.first}}}") # helper closing
concat(open)
end
which lets you write haml that looks like this
- handlebars_helper 'if misc' do
%b Misc Products
Total:
{{misc_total}}
- handlebars_helper 'each misc' do
{{price_charged}}
{{notes}}
Has this been done before, or is there a better way for me to do it?
*this helper looks a bit messy, I'm not that comfortable with blocks how would I clean this up?
If you want to use HAML to generate handlebars code, you can use hamlbars, which will provide you a few helpers to help facilitate indentation. Unfortunately you'll still be stuck with awkward indentation surrounding if/else.
I'd recommend Emblem.js as an alternative... the syntax is closer to Slim than HAML, but there's gonna be no ugly helper code polluting your templates just to preserve indentation. Disclaimer: I wrote emblem.

In Rails, how to write a helper function "who_am_i" so that a view or partial can use it to report which file it is?

When designing for layout and styling with CSS, it might be useful to add a line in the view or partial:
<%= who_am_i %>
or if using haml:
= who_am_i
so that it will print on the webpage
this is view/products/_footer.html.haml
How can this helper be written? (or is there one already in Rails?)
(my first try was to use __FILE__ and do some manipulation with string, and it works well if everything is done inside the view or partial file, but when it moves to helper, then __FILE__ becomes helpers/application_helper.rb so it won't work. But I'd like to find out possibly better ways to do it)
Your helper method should be:
def who_am_i
#template.template
end
Rails 3
def who_am_i
#_virtual_path
end
Note:
I tested the solution in Rails 3.0.5 and it works. I am not certain that it will work in all scenarios. Use with caution.

Is there any gem can dump the data from and to yml files?

I'm find such a gem a whole day, but not find a good one. I want to write one, but I'm not able to do it.
The data in my database may be English text, which will be dump to a yml file with plain text. And some are non-English text, which will be binary type.
And both of them may have such code:
<% xxx %>
When I use rake db:fixtures:load to load them into database, error may occur: method xxx not found.
I wan't to find a good gem can handle this problem. Thank for any help
UPDATE
I have gave up finding such a gem. At first, I think it's a easy task, but now, after some researchings, I have to say, it's much harder than I expected.
The reason you are having problems is because the Fixture loader will pass your fixture through erb before loading the data. This means that if you have <% xxx %> in your yaml file then Rails will see that as erb and try to run a method called xxx.
There does not seem to be an easy way to turn off erb processing of fixtures. I have tried replacing the fixtures with CSV fixtures and this still does ERB pre-processing.
Without a simple answer I have to ask the question Why do you have these strings in your file?
Do you want them to be expanded by erb?
Err...I'm not sure if you actually need a gem for this? Rails natively can turn any model into YAML.
Let's say you have a model called "Objects". You could hit a route that looks like:
/objects.yaml
and you would get a giant text file of all your Objects in YAML form.
Of course, you would want to have something like:
respond_to do |format|
format.yaml {render :yaml => #objects}
end
in your restful controller.
If you'd rather not hit a route to do this, you can always do
#yaml = []
#objects.each do |object|
#yaml.push object.to_yaml
end
anywhere in ruby, which will give you an array of yaml objects, that you can then write to a file at your leisure.
I imagine that if rails itself is generating the yaml, then it would be able to then later load it as a fixture?

Possible to embed markdown within erb?

If you use haml as rails view template, you can write portion of your page using markdown by using the ":markdown" filter.
Is is possible to do the same using erb?
It's pretty easy to write a method that does it, assuming you're using something like Rails which has #capture, #concat, and #markdown helpers. Here's an example, using Maruku:
def markdown_filter(&block)
concat(markdown(capture(&block)))
end
Then you can use this as so:
<% markdown_filter do %>
# Title
This is a *paragraph*.
This is **another paragraph**.
<% end %>
There are a few things to note here. First, it's important that all the text in the block have no indentation; you could get around this by figuring out the common indentation of the lines and removing it, but I didn't do that in the example helper above. Second, it uses Rails' #markdown helper, which could easily be implemented in other frameworks as so (replacing Maruku with your Markdown processor of choice):
def markdown(text)
Maruku.new(text).to_html
end
Rails 3 has removed the #markdown helper, so just add the above code in the appropriate helper, substituting the Markdown processor of your choice.
ERB does not have filtering like this built-in. You'll need to directly use a gem that handles it, like RDiscount or the venerable BlueCloth.

Is there an alternative to rail's auto_link helper method?

I'm using the auto_link method in Rails to turn urls in plain text to clickable links. It works pretty good most of the time.
but
google.com doesn't create a link
Is there something else out there that provides better coverage of urls? Or is there anyway I can easily modify the Rails code to include .coms and .net without a http or www start string?
auto_link uses AUTO_LINK_RE which you can override.
you can place a file in config/initializers
module ActionView::Helpers::TextHelper
silence_warnings do # you might need this to get rid of the warning
AUTO_LINK_RE = # your own definition here
end
end
It should work, but I never tried myself.

Resources