padrino can't find active_support methods - time

In my Padrino application i'm trying to get the time 10 years from now using
10.year.from_now
I'm getting:
NoMethodError: undefined method `year' for 10:Fixnum
Any idea why ?!

ActiveSupport is not fully loaded by default this because differently than rails Padrino aims to be small and fast.
If you use ActiveRecord a full set of ActiveSupport will be loaded otherwise you need to require (i.e. in boot.rb) your dependencies. In your case:
require 'active_support/core_ext'
require 'active_support/duration'
My suggestion is to load a full active_support set whenever is really needed.

Related

NameError: undefined local variable or method `broadcast_messages' for #<RailsStdoutLogging::StdoutLogger:0x007f4d099fb6b0>

Title error when accessing ActiveRecord model only on heroku with rails 5.0.0-beta1. Works on production env heroku. Problem persists anytime Rails.logger is set to STDOUT wether between gem 'rails_stdout_logging' or in config/environment/production.rb
I've reviewed implementation and see broadcast_messages as a method created by the accessor macro. I'm at a loss and am looking for suggestions to troubleshoot.
Turns out heroku's rails_stdout_logging gem which is included as part of heroku's rails_12factor gem has a bug with rails 5.0.0-beta1.
StdoutLogger inherits from stdlib's Logger rather than ActiveSupport's Logger via < ::Logger. I've got a workaround on my fork but it injects an ActiveSupport dependency when not using rails. I'm new to ruby so I don't know best practice to make a more eloquent fix.
See https://github.com/MattWalston/rails_stdout_logging.

Using `wicked_pdf` in a non-rails project

All the documentation for wicked_pdf assume you are using rails.
But I want to use this feature in a non-rails environment (a Jekyll plugin, written in Ruby).
Trying to use pdf = WickedPdf.new.pdf_from_string(page.content) returns Error: uninitialized constant WickedPdf.
How do I initialize WickedPdf without the rails generate wicked_pdf provided in their documentation?
I had to add gem 'activesupport' to my gemfile, then use require 'wicked_pdf' in my source code.
wicked_pdf depends on active_support/core_ext - as you can see in lib/wicked_pdf.rb. However you can use it outside of controller "context" like this...
pdf = WickedPdf.new.pdf_from_string('<h1>My life is wicked.</h1>')

Tests won't work with MongoMapper in Sinatra

I hooked up MongoMapper with Sinatra and everything works fine except for the testing. I have Autotest with Rack Testing and Rspec installed. Whenever I run autotest, it tells me
/home/jason/ror/sbmongo/main.rb:11:in `<top (required)>': uninitialized constant
MongoMapper (NameError)
Here is the line of code it refers to in my main.rb file.
MongoMapper.database = 'testdb'
What is the problem and how could I fix this?
The order that things are called via require in Ruby does make a difference, as a constant declared in a library will only be visible after a the library has been required.
When running RSpec, it's best to put general set up code in one place - the spec_helper.rb file - and then in the individual specs, (for example, when specing main.rb) require 'spec_helper and then require main.rb. If each code file requires the libraries it needs in the right order, then your specs will run without a problem too. If not, it's a sign that the order of requires isn't quite right.

Localizing numbers in Ruby without Rails

I've got a Ruby app that is not using Rails, but needs localization. I've been using the I18n gem, which works well for dates, times, and strings. It doesn't seem to do numbers, though, unless I'm missing something.
I found the NumberHelper class in ActionView from Rails, and this question says how you can include it in a non-Rails app. However, making my app dependent on the whole activemodel framework seems like overkill just to get a few numeric localization routines.
Is there a better way to approach this? Should I be using one of the other Ruby localization libraries instead?
I've used the gem R18n for that: https://github.com/ai/r18n
It can do the same stuff as I18n but is more flexible and can also localize numbers. It does not have dependencies of other gems.
Basic usage:
Add r18n-core to Gemfile
Initialize with `R18n.extension_places << 'path/to/yml/files'
And then either:
i18n = R18n::I18n.new('en')
i18n.t[:foo][:bar] # => "Translated text"
i18n.l(123.45) # => "123.45"
Or the more static version:
R18n.locale = 'en'
R18n.t[:foo][:bar]
R18n.l(123.45)
Helpers exists (in other gems) for rails and sinatra

Getting around ruby-units conflict with activesupport

I want to use the ruby-units with a rails 3 project, but it seems like it conflicts with activesupport.
It looks to me like both activesupport and ruby-units create a to() method for String. For some reason, ruby-units one wins, and so whenever to() is called inside rails it throws an error. (Unit not recognized)
I want to know the best way to deal with this. I don't care about having the to() method from ruby-units, I just don't want it to interfere with rails. I'd like to avoid forking if there's another approach.
To see my problem:
Add to your gemfile
gem 'ruby-units'
Open up rails console (I'm on rails 3 with ruby 1.9.2) and try apples.to(1)
Without ruby-units:
"ap"
With:
"'apples' Unit not recognized"
The answer to this is to require the ruby-units library before the rails library in the Gemfile:
gem 'ruby-units'
gem 'rails'
Obviously you then won't be able to use .to() on strings to access the ruby-units conversion.

Resources