how to require "prawn/measurement_extensions" in rails - ruby-on-rails

I wrote some demo code in ruby with prawn and want to move this code into my rails app
In the ruby code i use the measurement_extensions for prawn:
require "prawn/measurement_extensions"
In my rails app i put the same line into my controller, but this provoces an error:
cannot load such file -- prawn/measurement_extensions
So whats wrong?

You could make your rails ruby file inherit from Prawn::Document and require the extension within it e.g.
class NameOfClass < Prawn::Document
require "prawn/measurement_extensions"
#code
end

From the prawn documentation:
Add prawn to your Gemfile:
gem 'prawn'
Install:
$ bundle install
Create a file called config/initializers/prawn.rb and in it, add one of the following lines, depending on your Prawn version:
Prawn through 0.8.4:
require "prawn/core"
Prawn 0.9+ or git:
require "prawn"
Now, Prawn should be ready to go.

Related

Trying to use PDFKit but dont know where to initialize

After running into difficulty with Prawn I have decided to use PDFKit. In doing so I followed the following link:
Rails 3 + PDFKit: How to convert a view to PDF?
The problem is that my ruby on rails application is quite unusual in that it doesnt seem to follow the usual structure. In the link it says to place the following in the initializer:
require 'pdfkit'
middleware.use PDFKit::Middleware
PDFKit.configure do |config|
config.wkhtmltopdf = 'windows_path_to_wkhtmltopdf'
end
Where would i place this? I have an app.rb file which i tried to place this in but got the following error:
undefined method `use' for [[Sass::Plugin::Rack, [], nil]]:Array
The stacktrace points to the line:
middleware.use PDFKit::Middleware
Non-Rails Rack apps
in config.ru
require 'pdfkit'
use PDFKit::Middleware
From the documentation
I'm assuming you're using a config.ru setup
I have an application that uses WickedPDF, and there I only need to place
gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'
And then you can simply use WickedPdf object from the application. I am not sure how your application is laid out because it does not appear to be using bundler.

Using a gem in Ruby on Rails does not seem to work

I have installed the gem ruby-imagespec
The code looks like the following:
class TestController < ApplicationController
def ratio(fid)
file = File.new("#{Rails.public_path}/test/#{fid}.swf", "rb")
dimensions = ImageSpec.new(file)
return dimensions
end
helper_method :ratio
end
When I try to run this I get an error:
"uninitialized constant TestController::ImageSpec" on line 7 The
gem is listed in the gemfile and explicitly requiring it does not
work.
Where is the error? The gem is installed correctly and this code is just like the code from the gem's README
Looking at the ruby-imagespec repository, the file you need to require appears to be lib/image_spec.rb. Try require 'image_spec'.
ruby-imagespec is an unsupported gem, please try fastimage:
add gem 'fastimage' in your Gemfile
here the documentation https://github.com/sdsykes/fastimage

Local gem class is not found in Rails application

I have written a generator which creates the following ruby file and folder:
app/tests/test.rb
in the test.rb file I have a Test class which looks like this:
class Test < MyCustomModule::MyCustomClass::Base
...
end
Now, I want to use its functionality in one of the show.html.erb files creating new instance like this:
Test.new(...).render(...).html_safe
but I am getting the following error:
uninitialized constant MyCustomModule::MyCustomClass::Base
I have use the following answer to link my gem and my rails application. It seems to work as I am able to use the generator, but the gem module and class are not seen in the rails application.
Could anyone tell how to fix this issue?
I have try to follow the tips posted here but still nothing changed:
Adding config.autoload_paths += Dir["#{config.root}/lib/**/"] in application.rb file
I have created my gem structure looking at CarrierWave gem, so the naming should be correct
I try to disable config.threadsafe! but it is already disabled since config.cache_classes and config.eager_load are set to false in development
DEPRECATION WARNING: config.threadsafe! is deprecated. Rails
applications behave by default as thread safe in production as long as
config.cache_classes and config.eager_load are set to true.
Also, looking at adding-asset-to-your-gems rails documentation, it is said that:
A good example of this is the jquery-rails gem which comes with Rails
as the standard JavaScript library gem. This gem contains an engine
class which inherits from Rails::Engine. By doing this, Rails is
informed that the directory for this gem may contain assets and the
app/assets, lib/assets and vendor/assets directories of this engine
are added to the search path of Sprockets.
So, I have done this, and put my model class file in assets folder, but the result is the same.
The following screenshots demonstrate my real case:
The screenshot below displays my gem file structure
Here you can see how I am loading the gem in my Rails application Gemfile:
gem 'thumbnail_hover_effect', '0.0.3', github: 'thumbnail_hover_effec/thumbnail_hover_effec', branch: 'master'
Then I am using the gem generator a ruby file with a cutstom name in app/thumbnails/test.rb folder with the following code:
class Test < ThumbnailHoverEffect::Image::Base
...
end
and trying to use the Test class gives me uninitialized constant ThumbnailHoverEffect::Image::Base error.
Back in the gem files, these are how the thumbnail_hover_effect file looks like
require 'thumbnail_hover_effect/version'
require 'thumbnail_hover_effect/engine'
require 'thumbnail_hover_effect/image'
module ThumbnailHoverEffect
# Your code goes here...
end
and hoe the image file looks like:
module ThumbnailHoverEffect
#
class Image
...
end
end
From what you've posted here there is no ThumbnailHoverEffect::Image::Base defined. Rails autoloading conventions (which you should not be depending on a gem btw, more on that later) would be looking for this file in thumbnail_hover_effect/image/base.rb, but the directory structure you printed does not have that. Of course you could define the class in thumbnail_hover_effect/image.rb and it would work, but the abridged snippet you posted does not show that. So where is ThumbnailHoverEffect::Image::Base defined?
If it's in thumbnail_hover_effect/image/base.rb then that would indicate the file is not being loaded. You can sanity check this by putting a puts 'loading this stupid file' at the top of thumbnail_hover_effect/image/base.rb. That will allow you to bisect the problem by seeing whether there is a problem with your definition of the class, or whether the problem is with loading the proper files. Debugging is all about bisecting the problem.

Rails bootstrap gem monkeypatching method not working

I'm using the excellent twitter-bootstrap-rails gem. There is a helper within that gem (NavbarHelper) which is used to generate Bootstrap navbars with a Ruby helper. I want to monkey patch the gem such that the dropdown lists won't have carets.
So, I looked into the source and found the relevant method here. All I have to do is override it. I created a new file in config/initializers called navbar.rb with the following content:
NavbarHelper.module_eval do
def name_and_caret(name)
"HELLO WORLD"
end
end
Presumably, all of the dropdown titles then should be rendered as "HELLO WORLD" in my app (as referenced by the gem source). However, this is not occurring, and the gem does not appear to be monkeypatched at all.
I tried placing puts NavbarHelper.methods - Object.methods in the initializers file, and there were no results, which makes me think that Rails is not loading the gem correctly before the initializers. I have also checked and verified that the gem is not using autoload for its helpers.
Edit
What may be complicating this is the fact that my Gemfile includes the gem in the following manner:
gem 'twitter-bootstrap-rails', git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git', branch: 'bootstrap3'
I'm not sure if this specific versioning means the monkeypatching doesn't work.
Edit #2
It seems there is only one version of the gem on my system, so I don't think that's the issue. Also, I have tried placing require 'twitter-bootstrap-rails at the top of the initializers file, with no results.
The problem is that you patch the method on this module but the module already got included at this point. Try to define this in your application_helper.rb
def name_and_caret(name)
super("blub #{name}")
end

Creating an install generator for a Ruby gem

I'm pretty new to building Ruby gems and am taking a stab at my first one. I'm in the middle of writing a generator for my gem that will generate a migration in my Rails app. I'm looking to simply include the gem in the Rails application, run "rails g mygem:install" to have it create the migrations, then run "rake db:migrate" to finalize everything.
I've found several different ways to accomplish similar tasks, but so far nothing has worked. I cannot seem to get the Rails application to find the generator. The latest guide I've tried is located here: http://www.railsdispatch.com/posts/how-rails-3-enables-more-choices-part-1.
Here is what my current gem structure looks like:
-lib/
-generators/
-templates/
-some_migration.rb
-install_generator.rb
-gemname/
-rails/
-railtie.rb
-engine.rb
-tasks/
-gemname.rake
-gemname.rb
-spec/
-gemname.gemspec
Here is what my install_generator.rb file looks like:
require 'rails'
module Gemname
class InstallGenerator < ::Rails::Generators::Base
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
desc "add the migrations"
def self.next_migration_number(path)
unless #prev_migration_nr
#prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
else
#prev_migration_nr += 1
end
#prev_migration_nr.to_s
end
def copy_migrations
migration_template "some_migration.rb", "db/migrate/some_migration.rb"
end
end
end
I'm not sure if there's something I'm missing. I'm testing with a Rails 3.2 application that has my gem listed in its Gemfile and the gem is installed. Is there anything wrong with gem folder structure that might be preventing my generator from showing up? Do I need to require something somewhere?
Any help is appreciated.
Rails no longer automatically loads everything in lib by default, like it used to. For a few methods of requiring your files, check out the answers to this question - Best way to load module/class from lib folder in Rails 3?.
Well, I'm not sure which of the changes I made fixed my problem, but here is what I did:
Changed the name of my generator to gemname_generator.rb and the directory structure of my generator to this:
- lib/
-generators/
-templates/
-gemname_generator.rb
Added this line to my generator:
namespace 'gemname'
I'm now able to run "rails g gemname" in my Rails application and have it call my generator.

Resources