Rails i18n using java .properties files - ruby-on-rails-3

Is it possible to use java .properties files in a Rails project instead of YAML files? And if so, how do you set this up?

generally the i18n integration works with YAML files or plain ruby hashes. So you could simply parse the .properties files (I think https://github.com/flergl/java-properties-for-ruby would still do the job, works even still with 1.9.3 at first glance) and convert them to YAML or dynamically parse them in something like:
# config/locales/en.rb:
# Gemfile: gem 'java_properties'
# or require 'rubygems'; require 'java_properties';
props = JavaProperties::Properties.new("de.properties")
translations_hash = props.keys.inject({}) { |hash, key| hash[key] = props[key]; hash }
{ :en => translations_hash }
Of course you might still need to replace placeholder syntax "{0}" to a i18n compatible "#{0}".
Cheers,
Fred
ps.: BTW check out our service PhraseApp.com we are working on easing the i18n pain!

Related

Rails with Jader gets 'failed to require "fs"' error

I am trying to use client side Jade templates that are precompiled by Ruby on Rails and available through JST.
I added the jader gem
gem "jader", "~> 0.0.8"
In configuration/initializers I created an initializer called jader.rb that contains:
Jader.configure do |config|
config.views_path = Rails.root.join('app','assets','javascripts','views')
config.mixins_path = Rails.root.join('app','assets','javascripts','mixins')
end
In app/assets/javascripts/views I added an index.js.jst.jade file
Hello World!
Lastly in my javascript file I have:
$('#app-content').append(JST["views/index"]());
When I run Rails and browse to the page triggering the code I am getting the following error:
failed to require "fs"
(in /my-project/app/assets/javascripts/views/index.js.jst.jade)
I understand that the problem is that jade is a node.js project and the require function is having a problem. How do I fix the require error?
If you are looking at using Jade templates only on the client side (with Rails asset pipeline), I recommend https://github.com/inossidabile/jade
It's likely there is an error in your index.jade template.
From the jade rails-fix pull request here, ExecJS used by rails does not support require("fs") and will throw 'failed to require "fs" even if it's a template error. Hopefully this should point you in the right direction.
To debug, you can replace the content of index.jade template with with a 1-liner jade placeholder to see if it renders well. Alternatively, try it out rendering the index.jade template with a nodejs server if possible to be sure the template renders without any whinning.

Rails 3.2 Asset Pipeline + html5shiv.JS in vendors/assets/javascript

After reading this post (recommended reading) about not using HTML5Shiv direct from source like (almost) everybody does, I'm trying to include the html5shiv.js in my app using Rails 3.2 Asset Pipeline.
I downloaded both minified and not-minified version of the javascript. The convention tells you to add third-party files into the vendors/assets folder. I have two questions now:
1) Which version (minified or not-minified) should I add to vendors/assets/javascrip folder?
2) Since it's a conditional reference <!--[if lt IE 9]>, how should I call the script?
I don't want to add it to the application.js manifest, because I want to keep it as a separate file and I want to use the condition. I'm kind of lost!
Any help would be very appreciated.
Thanks
You can use the unminified version of the JS if you like, Rails will compress it in production mode through the pipeline.
To keep the shiv as a separate file you can give it it's own manifest by creating a html5.js (or whatever) in your /vendor/assets/javascripts/ directory. In that file require the html5shiv (I assume the manifest and script are in the same dir).
//= require html5shiv
or
//= require html5shiv.min
And then include the manifest in your layout in the conditional block. In HAML something like:
== "<!--[if lt IE 9]>"
= javascript_include_tag 'html5'
== "<![endif]-->"
Remember to restart your app server before testing.
Or you may use directly
/[if lt IE 9]
%script{ src: "http://html5shim.googlecode.com/svn/trunk/html5.js", type: "text/javascript" }

pdfkit not rendering correctly in rails 3.1

I have followed the following railscast about adding pdfkit to an application, and I am having some issues with the generation of pdfs. Here are the following things that I have done:
I downloaded wkhtmltopdf via the homebrew package manager
brew install wkhtmltopdf
Then I added the pdfkit gem to my gemfile and ran the bundle install command. I added the following to my config/application.rb file
require 'pdfkit'
...
config.middleware.use PDFKit::Middleware, :print_media_type => true
I then changed my application layout file to include all stylesheet types.
If I run rake middleware, the command works and I can see the pdfkit middleware
When I try to append pdf to the end of my routes the application just hangs and I have to exit via the command line. If I create a link to the page I want to make into a pdf, it changes all of the markup so it looks like a corrupted file. (it looks like you opened a text file into a word processor or vice versa I can provide images if that helps) If I try to make css changes in my stylesheet they do not go into effect when I view them with the link to pdf. I am assuming that this has something to do with the new asset pipeline in rails has anyone else experienced this issue?
So I was right in assuming that my error had something to do with the asset pipeline, after doing some research it looks like you need to create a new initializer and add the following code:
ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}

Using Compass on Heroku: /tmp for stylesheets remotely and locally

I'm currently using Compass with Heroku using this configuration recommended on the Heroku knowledge base. Heroku has a read-only file system, and so the compiled stylesheets need to be stored in /tmp. This works fine remotely on Heroku; locally, however, Rails expects to find stylesheets in /public/stylesheets (when called through = stylesheet_link_tag 'screen.css', :media => 'screen, projection').
In order to solve the problem, I have created symbolic links in /public/stylesheets using ln -s tmp/stylesheets/screen.css public/stylesheets/screen.css and that seems to work.
Is there a way to solve this problem without using symbolic links, perhaps by changing some configuration in Rails? I've poked around without much success.
Here is my config/initializers/compass.rb:
require 'compass'
require 'compass/app_integration/rails'
Compass::AppIntegration::Rails.initialize!
# Required for Heroku:
require 'fileutils'
FileUtils.mkdir_p(Rails.root.join("tmp", "stylesheets"))
Compass::AppIntegration::Rails.initialize!
Rails.configuration.middleware.delete('Sass::Plugin::Rack')
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Sass::Plugin::Rack')
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Rack::Static',
:urls => ['/stylesheets'],
:root => "#{Rails.root}/tmp")
And here is my config/compass.rb:
project_type = :rails
project_path = Compass::AppIntegration::Rails.root
# Set this to the root of your project when deployed:
http_path = "/"
# Necessary for Heroku (original commented out:
css_dir = 'tmp/stylesheets'
#css_dir = "public/stylesheets/compiled"
sass_dir = 'app/views/stylesheets'
environment = Compass::AppIntegration::Rails.env
Any help would be greatly appreciated.
I was actually just about to set up Compass with our Rails application, which is hosted on Heroku, so cheers for giving me an excuse to work through this. :)
The answer is simple:
Modify 'config/compass.rb':
project_type = :rails
project_path = Compass::AppIntegration::Rails.root
http_path = "/"
environment = Compass::AppIntegration::Rails.env
if environment == 'production'
css_dir = "tmp/stylesheets"
sass_dir = "app/views/stylesheets"
else
css_dir = "public/stylesheets"
sass_dir = "app/stylesheets"
end
Then modify 'config/initializers/compass.rb':
require 'compass'
require 'compass/app_integration/rails'
Compass::AppIntegration::Rails.initialize!
require 'fileutils'
FileUtils.mkdir_p(Rails.root.join("tmp", "stylesheets"))
environment = Compass::AppIntegration::Rails.env
if environment == 'production'
Compass::AppIntegration::Rails.initialize!
Rails.configuration.middleware.delete('Sass::Plugin::Rack')
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Sass::Plugin::Rack')
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Rack::Static',
:urls => ['/stylesheets'],
:root => "#{Rails.root}/tmp")
end
... and voila, you're good.
ok, I'm a big heroku and compass fan myself so i've been through this many times
Heroku's docs, whilst giving correct information, provide poor advice in this instance.
When using compass, the best thing to do, 99.999% of the time is turn it off in production mode.
This means that you compile your stylesheets on your development machine and then add them to your git repo before pushing to heroku.
You will suffer a reasonably sizeable performance hit if you allow compass to compile on the server.
So here's what I do:
You should have a config.ru file at the base of your app. Open it and add the following:
require 'sass/plugin/rack'
use Sass::Plugin::Rack
Sass::Plugin.options[:never_update] = true
You can then remove quite a lot of the code from your initializer (especially the part where you unload Sass::Plugin::Rack). Additionally you will want to remove the if statement from compass.rb in config folder
Think about it, why would you want Sass to compile a stylesheet on the server? It just eats up processing power. Hope this helps,
EDIT::
PS - I should add that you will need to run compass watch from the command line now in order to get your stylesheets to compile in your dev environment
The recommended Heroku configuration will also work locally.
Removed the second 'Compass::AppIntegration::Rails.initialize!' from config/initializers/compass.rb, you only need it once.
Ensure your scss files are in 'app/views/stylesheets'
On both local and production servers the stylesheets will be compiled to tmp/stylesheets, and a request to /stylesheets will resolve to tmp/stylesheest. No need for two separate configurations.

How do I get the root directory of a gem in the Rspec tests for my Rails project?

I've got a gem, we'll call it ToastMitten, which I'm including in one of my Rails apps. I'm writing some tests for ToastMitten in which I need to load a file, and I want to specify a path from the root of the gem.
I just tried using Rails.root.to_s, but that gives me something like /Users/me/projects/toastmitten/spec/dummy. I would have expected that path to end at toastmitten/.
What am I doing wrong?
Rails.root.parent.to_s
If it always gives back your dummy Rails app, just move up to the parent.
It looks like you are using a Rails engine (generated with enginex, hence the dummy app in your spec folder). If you need to require a file in your test using an absolute path, you can use the following:
file = File.expand_path(File.join(File.dirname(__FILE__), 'path', 'to', 'file.ext'))
root = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
Can you be more specific on where you are trying to require this file?
Also, The root example may require more ..'s.