globalize/globalize

attribute_present?(nil) raises NoMethodError

atd opened this issue · 1 comments

atd commented

In a globalized model, attribute_present?(nil) raises error:

Post.first.attribute_present? nil
NoMethodError: undefined method `to_sym' for nil:NilClass
from /home/..../gems/globalize-5.3.0/lib/globalize/active_record/class_methods.rb:38:in `translated?'

However, this does not happen in a not globalized model

Comment.first.attribute_present? nil
=> false

This is the default behavior of ActiveRecord. It causes problems with gems like counter_culture, which relays in default behavior.

# Activate the gem you are reporting the issue against.
gem 'activerecord', '5.2.2.1'
gem 'globalize', '5.3.0'
require 'active_record'
require 'globalize'
require 'minitest/autorun'
require 'logger'

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
  end

  create_table :post_translations, force: true do |t|
    t.references :post
    t.string     :title
    t.text       :content
    t.string     :locale
  end
end

class Post < ActiveRecord::Base
  translates :content, :title
end

class BugTest < Minitest::Test
  def test_association_stuff
    post = Post.create!(title: 'HI')

    assert_equal false,  post.attribute_present?(nil)
  end
end
stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.