Is there any way to test HAML output in Rails Console? - ruby-on-rails

I want to do something like this in console:
= raw CSV.generate_line(array)
And see how the output looks like.
The reason being the CSV output on Heroku is adding in extra newlines in every CSV file genrated and I can't replicate this on development. It's not very good practice to try random attempts to fix the problem by deployment =)
If anyone has hints to what might be causing the newline characters, be grateful too.

You can do it like this
engine = Haml::Engine.new("%p Haml code!")
engine.render #=> "<p>Haml code!</p>\n"
Of course you need to have haml gem required in your app but i assume you have this.

You can also have
include ActionView::Helpers
in your console.
and have raw, link_to and other helpers handy.

Related

Rails: Can't access a module in my lib directory

I'd like to create a general purpose string manipulation class that can be used across Models, Views, and Controllers in my Rails application.
Right now, I'm attempting to put a Module in my lib directory and I'm just trying to access the function in rails console to test it. I've tried a lot of the techniques from similar questions, but I can't get it to work.
In my lib/filenames.rb file:
module Filenames
def sanitize_filename(filename)
# Replace any non-letter or non-number character with a space
filename.gsub!(/[^A-Za-z0-9]+/, ' ')
#remove spaces from beginning and end
filename.strip!
#replaces spaces with hyphens
filename.gsub!(/\ +/, '-')
end
module_function :sanitize_filename
end
When I try to call sanitize_filename("some string"), I get a no method error. When I try to call Filenames.sanitize_filename("some string"), I get an uninitilized constant error. And when I try to include '/lib/filenames' I get a load error.
Is this the most conventional way to create a method that I can access anywhere? Should I create a class instead?
How can I get it working? :)
Thanks!
For a really great answer, look at Yehuda Katz' answer referenced in the comment to your question (and really, do look at that).
The short answer in this case is that you probably are not loading your file. See the link that RyanWilcox gave you. You can check this by putting a syntax error in your file - if the syntax error is not raised when starting your app (server or console), you know the file is not being loaded.
If you think you are loading it, please post the code you are using to load it. Again, see the link RyanWilcox gave you for details. It includes this code, which goes into one of your environment config files:
# Autoload lib/ folder including all subdirectories
config.autoload_paths += Dir["#{config.root}/lib/**/"]
But really, read Yehuda's answer.

Where is the the right place to put the "magic comment" ( utf-8 ) in RSpec in an rails project?

I'm using rspec for my rails project and it seems a hack must be used when using the German Umlaute (ä ö ü) in the spec-file:
# coding: utf-8
where is the best place to put this in? Is it common practice to put this in the spec_helper.rb or somewhere else?
Thanx in advance :)
Into every .rb file that has UTF-8 characters, right to the first line.
the best way seems (after trying many options) is to use the
gem magic_encoding
because it is the easiest way to let the gem find any relevant files for your application to change :)

Trouble on translating views after renaming statements from 'translate' to 'I18n.t' and from 'localize' to 'I18n.l'

I am using Ruby on Rails 3.2.2 and I have a strange problem. In my view files I use the following code:
# app/views/articles/show.html.erb
I18n.t('.page_title')
When I render the above view code in the browser I get the translation missing: en.page_title message. However, as you can see from the "missing" message, the translation doesn't refer to the articles.show.page_title YML statement... but it should do that! It seems that the “Lazy” Lookup doesn't work as expected.
Why that happens? Have you some idea about the issue and how to solve it?
Note: I just made a code refactoring renaming statements from translate to I18n.t and from localize to I18n.l...
Try simply: t('.page_title') Then you should see another statement. But in fact, I18n searches in several places. You can add the translation and see for yourself:
en:
page-title:
Or:
en:
articles:
show:
page-title:

generate nofollow links in RDiscount output

My rails app is using RDiscount to generate HTML from user-supplied markdown text, and I noticed that the anchor tags don't have rel="nofollow". This is a big issue for me as my app is open to the public. Is there a way to enable nofollow links, or are there better solutions?
Thanks!
I think this is possible only with Kramdown, which is a ruby Markdown parser with extended syntax. You would do that then as shown in the link:
[link](test.html){:rel='nofollow'}
In the mean time, I am using this hack, by re-parsing the RDiscount output and add a rel="nofollow" to each anchor:
def markdown(input)
html = RDiscount.new(input).to_html
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.css("a").each do |link|
link['rel'] = 'nofollow'
end
doc.to_html
end
Though I think this should really be handled by the markdown parser.
I needed to do something similar, add target="_new" to all links. Solved it using Kramdown and a custom Kramdown::Converter::Html class.
Define a Kramdown::Converter::Html subclass (kramdown/converter/my_html.rb in some autoload path)
class Kramdown::Converter::MyHtml < Kramdown::Converter::Html
def convert_a(el, indent)
el.attr['target'] = '_new'
super
end
end
I also have a view helper in app/helpers/application_helper.rb
def markdown(str)
Kramdown::Converter::MyHtml.convert(Kramdown::Document.new(str).root)[0].html_safe
end
Ideally it should be possible to just use Kramdown::Document.new(str).to_my_html.html_safe but I can't get it to work in rails development mode as Kramdown uses const_defined? to see if a converter is available and that does not trigger the autoloader. Please comment if you know how to fix this.
There is an open feature request on RDiscount to support modifying links in this fashion.
It is scheduled for one of the upcoming RDiscount 2.1.5.x releases.

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?

Resources