Load locales/I18n files from Ruby gem into Rails - ruby-on-rails-3

I have a ruby gem that I'm working on that includes some locale/I18n files that I would like to be loaded if and when the gem is used by Rails.
I've looked around a little and it looks like I need to provide a Railtie - I've found some examples here on stackoverflow and elsewhere, but I just can't get it to work.
Ideally, I would like this to work for Rails 3 and 4.
Here's the layout of my gem:
+ lib
+ my_gem
my_gem.rb
version.rb
+ locales
en.yml
From what I've found it seems I need to add lib/my_gem/railtie.rb and put some code in there which appends the path to the .yml files in the locales directory of my gem. I've tried various different ways of doing this but I have been unable to get it to work.

You should have this layout:
+ lib
+ my_gem
engine.rb
my_gem.rb
version.rb
+ config
+ locales
en.yml
In the engine.rb file you should extends Rails::Engine, like this:
module YourGem
class Engine < Rails::Engine
end
end
It should work! =)

Related

How to set sinatra-authentication to use erb instead of haml?

I'm trying to set up the sinatra-authentication gem in a simple sinatra app, and running into an issue where sinatra can't find the correct views. I understand that sinatra-authentication uses haml by default, but I'm using erb in this app.
This in mind, I found in the sinatra-authenticaiton docs that there is a setting which allows you to change the template engine, by adding the following to your app file:
configure do
set :template_engine, :erb # for example
end
I've added this to my app.rb file, and sinatra is still looking for the signup.haml when I try to hit the /signup route in my app.
A couple of notes:
I've included the gem in my Gemfile, and successfuly run a bundle install on my app.
source 'https://rubygems.org'
gem 'sinatra'
gem 'data_mapper'
gem 'pg'
gem 'dm-postgres-adapter'
gem 'sinatra-authentication'
I saw something in the documentation that suggested that I may need to specify the location of my view files, so I added the following to my configuration block.
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
**I think I've required the gem accurately in my app file by adding
require "sinatra-authentication"
use Rack::Session::Cookie, :secret => 'mys3cr3tk3y'
This gist is a current representation of my app.rb file in the root of my sinatra app. https://gist.github.com/rriggin/5378641#file-gistfile1-txt
Here is a screenshot of the error sinatra throws: http://cl.ly/image/0y041t0K3u3O
When I run the app locally, a 'dm-users' table is created in my local db as expected.
Is there another configuration setting that I'm missing in order to get sinatra-authentication to properly look for the erb templates rather than haml files. Any help would be greatly appreciated.
Thanks
The specs don't test that the template_engine setting works, and looking at the way the setting is called, I believe it's not correct, i.e.
send settings.template_engine, get_view_as_string("index.#{settings.template_engine}"), :layout => use_layout?
might better work as:
send app.settings.template_engine, get_view_as_string("index.#{app.settings.template_engine}"), :layout => use_layout?
that's what I reckon. If you fork the project, change the line and add it to your Gemfile and it works then consider writing a quick spec for it and you'll have improved the mainline of that project as well as fixed your problem.

Gem with models

I created a Gem with models (actually, extracted it from the main project) to share amongst the projects we have in our platform.
We have dozens of models, so instead of requiring them one by one, I wrote the following code:
Gem.find_files("my_gem/models/*.rb").each { |path| require path }
I access one of the projects that has my_gem in the gem file and running rails c I get the following output:
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined method `has_attached_file' for #<Class:0x007fad4b93ccb8> (NoMethodError)
One of my models is using the gem paperclip, what is weird is:
If I remove the line declared above to load all the models
automatically, rails c runs fine
If I try to include onlye the model that uses paperclip require
"my_gem/models/paperclip_model" I receive the same error
So then I change my gem to not load any model, and when I try to reference any model from rails console, it says the class is not loaded, but then I run Gem.find_files("my_gem/models/*.rb").each { |path| require path } or require "my_gem/models/paperclip_model" 'they work perfectly and I am able to work with the model.
Has any of you seen the same issue?
Seems that changing require for autoload solved the problem
I changed Gem.find_files("my_gem/models/*.rb").each { |path| require path }
for
Gem.find_files("my_gem/models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "my_gem/models/#{filename}"
end
and now it is working.
It sounds like one of the models in your gem depends on Paperclip, but you don't explicitly set it as a dependency. So what's happening is that if your models get loaded before paperclip gets loaded, you'll see the UndefinedMethod error for has_attached_file.
If you use your models in a Rails application which has paperclip as a dependency, and you load those models after the console (or server) has spun up, Paperclip will be present, so you won't see this error.
The solution is to explicitly add paperclip as a dependency in your gemspec, something like:
s.add_dependency('paperclip')
Assuming that this gem will always be used in the context of a Rails application, this should work. If not, you might also need to add the following line to the top of your models that use paperclip:
require "paperclip"

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
}

how to config ruport in the environment.rb

i have installed ruport gem along with its ruport-utils packages and act_as_reportable gem too....
e
"Ash::Application.initialize!
Rails::Initializer.run do |config|
config.after_initialize do
require "ruport"
end
end"
when executing this in the environment.rb file... it shows this "
c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.0.9/lib/rails/application.rb:63:in `inherited': You cannot have more than one Rails::Application (RuntimeError)"
what am i doing wrong.... can somebody help me with properly configuration method of ruport.
ok found wht i was doing wrong... apparently the source i was referring for was for earlier verion... with the new one just put this " config.autoload_paths += %W(::RAILS.root.to_s/app/reports) " in the config/environments/development.rb file , also specify yur gem in the gemfile and do a bundle insatll. and and also for generating textile file ie for rendering the report in html format you would be required to install RedCloth gem version 3.0.3(thats what i have installed in my system.)
The most reliable way to do so, and be sure that Ruport is loaded at the proper time, is to add the code to require Ruport to a config.after_initialize block. The relevant section of the configuration file is shown below.
Rails::Initializer.run do |config|
config.after_initialize do
require "ruport"
end
end

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.