Tag: gem

Ransack – no translations for ActiveRecord nested models

Ransack is a great gem. You can consider it as a new better (with sparkles!) version of MetaSearch. Unfortunately, it seems that it is not translating ActiveRecord attributes, when they are nested.
For example:

module System
  class Setting < ActiveRecord::Base
    validates :name,
      presence: true
  end
end

We have a name attribute on a System::Setting model. According to Rails doc translation yaml for such a structure should be similar to this one:

pl:
  activerecord:
    attributes:
      "system/setting":
        name: Nazwa

Unluckily this won't work (it did with MetaSearch). When you look into Ransack /lib/ransack/translate.rb file, you will find such a piece of code (around line 25):

defaults = base_ancestors.map do |klass|
  :"ransack.attributes.#{klass.model_name.singular}.#{original_name}"
end

You can see, that Ransack is enclosing all attributes in it's own namespace. I can't do this, since I have gems that use "original" Rails convention (activerecord.attributes), so I had to replace those lines with:

defaults  = []
base_ancestors.map do |klass|
  defaults << :"activerecord.attributes.#{klass.model_name.i18n_key}.#{original_name}"
  defaults << :"ransack.attributes.#{klass.model_name.singular}.#{original_name}"
end

After that, Ransack is supporting both namespaces. The whole translate file that you should include in your project should look like this:

I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'locale', '*.yml')]

# This is a temporary workaround for this issue:
# https://github.com/ernie/ransack/issues/273
# It allows names to be translated in sort_link @search, :name based on their
# activerecord.attributes scope
module Ransack
  module Translate
    def self.attribute(key, options = {})
      unless context = options.delete(:context)
        raise ArgumentError, "A context is required to translate attributes"
      end

      original_name = key.to_s
      base_class = context.klass
      base_ancestors = base_class.ancestors.select { |x| x.respond_to?(:model_name) }
      predicate = Predicate.detect_from_string(original_name)
      attributes_str = original_name.sub(/_#{predicate}$/, '')
      attribute_names = attributes_str.split(/_and_|_or_/)
      combinator = attributes_str.match(/_and_/) ? :and : :or
      defaults  = []
      base_ancestors.map do |klass|
        defaults << :"activerecord.attributes.#{klass.model_name.i18n_key}.#{original_name}"
        defaults << :"ransack.attributes.#{klass.model_name.singular}.#{original_name}"
      end

      translated_names = attribute_names.map do |attr|
        attribute_name(context, attr, options[:include_associations])
      end

      interpolations = {}
      interpolations[:attributes] = translated_names.join(" #{Translate.word(combinator)} ")

      if predicate
        defaults << "%{attributes} %{predicate}"
        interpolations[:predicate] = Translate.predicate(predicate)
      else
        defaults << "%{attributes}"
      end

      defaults << options.delete(:default) if options[:default]
      options.reverse_merge! :count => 1, :default => defaults
      I18n.translate(defaults.shift, options.merge(interpolations))
    end

  end
end

Invalid byte sequence in US-ASCII (ArgumentError) with Ruby on Rails

Lately when starting Unicorn instances, I've encountered following issue:

$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D
/gems_path/lib/bundler.rb:294:in `block in load_gemspec_uncached': 
  invalid byte sequence in US-ASCII (ArgumentError)
	from /gems_path/lib/bundler.rb:291:in `chdir'
	from /gems_path/lib/bundler.rb:291:in `load_gemspec_uncached'
	from /gems_path/lib/bundler.rb:282:in `load_gemspec'
	from /gems_path/lib/bundler/source.rb:411:in `block in load_spec_files'
	from /gems_path/lib/bundler/source.rb:410:in `each'
	from /gems_path/lib/bundler/source.rb:410:in `load_spec_files'
	from /gems_path/lib/bundler/source.rb:799:in `load_spec_files'
	from /gems_path/lib/bundler/source.rb:381:in `local_specs'
	from /gems_path/lib/bundler/source.rb:774:in `specs'
	from /gems_path/lib/bundler/lazy_specification.rb:53:in `__materialize__'
	from /gems_path/lib/bundler/spec_set.rb:86:in `block in materialize'
	from /gems_path/lib/bundler/spec_set.rb:83:in `map!'
	from /gems_path/lib/bundler/spec_set.rb:83:in `materialize'
	from /gems_path/lib/bundler/definition.rb:113:in `specs'
	from /gems_path/lib/bundler/definition.rb:158:in `specs_for'
	from /gems_path/lib/bundler/definition.rb:147:in `requested_specs'
	from /gems_path/lib/bundler/environment.rb:23:in `requested_specs'
	from /gems_path/lib/bundler/runtime.rb:11:in `setup'
	from /gems_path/lib/bundler.rb:116:in `setup'
	from /gems_path/lib/bundler/setup.rb:7:in `<top (required)>'
	from /gems_path/rubygems/custom_require.rb:36:in `require'
	from /gems_path/rubygems/custom_require.rb:36:in `require'

There are two ways to fix this. You can export language settings to your shell:

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

or you can put this into your Gemfile:

if RUBY_VERSION =~ /1.9/
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
end

Both methods should work.

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑