Difference Between Rails 2.2 and 2.3.5? ActionMailer.Utils - ruby-on-rails

I would think this is a Ruby difference, but I'm using the same Ruby version 1.8.7. This is related to this post (to answer "why do you need this?"). This code works in 2.2.2
Loading development environment (Rails 2.2.2)
>> module ActionMailer
>> Utils.normalize_new_lines("blah")
>> end
but in 2.3.5 it fails
Loading development environment (Rails 2.3.5)
>> module ActionMailer
>> Utils.normalize_new_lines("blah")
>> end
NoMethodError: undefined method `normalize_new_lines' for ActionMailer::Utils:Module
from (irb):2
What's new about 2.3.5 that this would fail? The method is there in 2.3.5, so this works
Loading development environment (Rails 2.3.5)
>> include ActionMailer
>> include Utils
>> normalize_new_lines("blah")
I realize this is probably an important Rails difference.

Looks like the code changed from version 2.2 to version 2.3.5
old:
module ActionMailer
module Utils #:nodoc:
def normalize_new_lines(text)
text.to_s.gsub(/\r\n?/, "\n")
end
module_function :normalize_new_lines
end
end
new:
module ActionMailer
module Utils #:nodoc:
def normalize_new_lines(text)
text.to_s.gsub(/\r\n?/, "\n")
end
end
end
I guess you could restore the old behavior by calling module_function yourself:
$ script/console
Loading development environment (Rails 2.3.5)
>> module ActionMailer
>> module Utils
>> module_function :normalize_new_lines
>> end
>> Utils.normalize_new_lines("blah")
>> end
=> "blah"
>>
EDIT: Or better yet just include the module (per Simone)
>> include ActionMailer::Utils

Related

Zeitwerk doesn't requires lib classes properly in Rails 6.1.6

I'm trying to update my app from Rails 5.2 to 6.1, and use Zeitwerk autoload, and I am having some issues for autoloading /lib classes.
I included this in config/application.rb:
config.load_defaults 5.2
config.autoloader = :zeitwerk
config.enable_dependency_loading = true
config.autoload_paths += Dir[Rails.root.join('lib/**/*')]
config.eager_load_paths += Dir[Rails.root.join('lib/**/*')]
And, when I run zeitwerk:check, it alerts me that:
Hold on, I am eager loading the application.
expected file lib/facades/coconut/v2/foo.rb to define constant Foo
It is declared as:
# /lib/facades/coconut/v2/foo.rb
module Coconut
module V2
class Foo
# ...
end
end
end
When I try to debug it (putting a binding.pry in some test file), Zeitwerk says it do not defined, until I call it without modules (namespaces):
[1] pry(#<#filename>)> Coconut::V2::Foo
NameError: uninitialized constant Coconut::V2::Foo
from (pry):1:in `setup`
[2] pry(#<#filename>)> Foo
Zeitwerk::NameError: expected file /my-app/lib/api/coconut/v2/foo.rb to define constant Foo, but didn't
from /usr/local/bundle/gems/zeitwerk-2.5.4/lib/zeitwerk/loader/callbacks.rb:25:in `on_file_autoloaded'
[3] pry(#<#filename>)> Coconut::V2::Foo
=> Coconut::V2::Foo
It sounds really strange, because there are another classes in /lib that follows the same structure, but it works well, example
# /lib/api/services/youtube/client.rb
module Services
module Youtube
class Client
# ...
end
end
end
Inspecting it with binding.pry in some test:
pry(#<#filename>)> Services::Youtube::Client
=> Services::Youtube::Client
Has anyone had this problem or know what could be happening?
System:
ruby 2.7.6p219
Rails 6.1.6
Zeitwerk (2.5.4)
An autoload path is a root directory, not its contents.
You need to remove the wildcards as documented here.

Rails module self.table_name_prefix is not working on production environment [duplicate]

This question already has an answer here:
Rails table_name_prefix is not working as expected
(1 answer)
Closed 1 year ago.
I have this code:
# app/models/ta.rb
module Ta
def self.table_name_prefix
'ta_'
end
end
...
# app/models/ta/article.rb
module Ta
class Article < ActiveRecord::Base
end
end
From the rails console...
# development environment
Loading development environment (Rails 4.1.6)
2.1.3 :001 > Ta::Article.table_name
=> "ta_articles"
2.1.3 :002 >
...
# production environment
Loading production environment (Rails 4.1.6)
2.1.3 :001 > Ta::Article.table_name
=> "articles"
2.1.3 :002 >
Why is this happening?
Add to config/initializers/namespace.rb something like:
require Rails.root.join('app', 'models', 'ta')
should solve your problem.

Initializer doesn't seem to be loading

I have an API wrapper with the following code:
module MyApi
mattr_accessor :app_id
end
I'm trying to set the value of app_id in an initializer like this:
# config/initializers/my_api.rb
MyApi.app_id = Rails.application.secrets.my_api["app_id"]
In my secrets file I have:
# secrets.yml
development:
my_api:
app_id: foo
But when I open my console or run tests I get this:
master ✗ $ rails c
Loading development environment (Rails 4.1.4)
2.2.0 :001 > MyApi.app_id
=> nil
2.2.0 :002 > MyApi.app_id = Rails.application.secrets.my_api["app_id"]
=> "foo"
2.2.0 :003 > MyApi.app_id
=> "foo"
I've followed gem readme's about using initializers but have never implemented one myself. Is there something I'm missing here? Using Rails 4.1.4.
Try adding this inside your module:
module MyApi
### Your code here
private
def self.setup
yield self
end
end
And in the initializer change it to have a setup block:
MyApi.setup do |config|
config.app_id = Rails.application.secrets.my_api["app_id"]
end
I have only built a few gems with initializers, but this worked for me.

Where is Rails.application defined?

Out of curiosity, where is Rails.application defined? If I do this in irb in a Rails-powered app, I got an error:
$ irb
1.9.3-p327 :001 > require 'rails/application'
=> true
1.9.3-p327 :002 > class App < Rails::Application; end
NoMethodError: undefined method `application' for Rails:Module
I have tried requiring more, like active_support and rails/railtie/configuration but no luck.
The purpose of this is to load a minimal Rails env where I can test an ActiveRecord::Base helper :)
Usually when working with Rails you use rails console instead of IRB. When you run rails console it will boot up your Rails application.
FWIW, Rails.application is defined in railties:
lib/rails.rb

What`s the diff between ruby 1.8.7 and 1.9.1 in module require?

There is a module
require 'iconv'
module Escape
def escape(string)
return_value = Iconv.conv('ascii//translit//IGNORE', 'utf-8', string).to_s
end
end
It`s work in 1.8.7 but not in 1.9.1
The error message is "NameError (uninitialized constant Escape::Iconv)"
and the follow is work in 1.9.1,Why??????? (my rails is rails 3 in ubuntu)
module Escape
def escape(string)
require 'iconv'
return_value = Iconv.conv('ascii//translit//IGNORE', 'utf-8', string).to_s
end
end
don't use 1.9.1 for rails3, instead use 1.9.2 or 1.8.7 . read here in the comments: http://weblog.rubyonrails.org/2010/2/5/rails-3-0-beta-release

Resources