mardi 14 août 2018

Update globalize3 to globalize

I have an old Rails 3 application; I updated it to rails 4. But when I update the globalize3 to globalize gem I lost translates attributes:

class Role < ActiveRecord::Base
  include MaskedPermissions

  # Relations
  has_many :users, inverse_of: :role, dependent: :restrict
  has_many :folder_permissions, class_name: 'RoleCmsPermission', inverse_of: :role, dependent: :destroy

  # Attributes
  translates :name

  attr_accessible :codename, :client_role, :translations_attributes

  accepts_nested_attributes_for :translations

  # Validations

  # Delegations

  # Callbacks

  # Scopes

  # Class methods
  def self.codenames
    pluck(:codename)
  end

  # Define a codename method for each role present
  Role.codenames.each do |codename|
    define_singleton_method(codename) do
      where(codename: codename).first
    end
  end if ActiveRecord::Base.connection.table_exists? 'roles' # Otherwise rake tasks fail before roles table is created
end

Then when I use this class I get:

[12] pry(Role):1> cd all.sample
  Role Load (0.9ms)  SELECT `roles`.* FROM `roles`
[13] pry(#<Role>):2> self
=> #<Role id: 19, codename: "auditor", client_role: true, created_at: "2013-05-13 11:15:30", updated_at: "2013-05-13 11:15:30", global: false, permissions_mask: 1>
[14] pry(#<Role>):2> name
NoMethodError: undefined method `zero?' for nil:NilClass
from /Users/toni/.rvm/gems/ruby-2.1.6@afs-update/gems/activerecord-4.0.9/lib/active_record/associations/alias_tracker.rb:30:in `aliased_name_for'

The old app has a initilializer like this, I need to remove this, but it seems to do the same as inside globalize:

    module Globalize
  module ActiveRecord
    module ActMacro
      def translates(*attr_names)

        options = attr_names.extract_options!
        attr_names = attr_names.map(&:to_sym)

        unless translates?
          options[:table_name] ||= "#{table_name.singularize}_translations"
          options[:foreign_key] ||= class_name.foreign_key

          class_attribute :translated_attribute_names, :translation_options, :fallbacks_for_empty_translations
          self.translated_attribute_names = []
          self.translation_options        = options
          self.fallbacks_for_empty_translations = options[:fallbacks_for_empty_translations]

          include InstanceMethods
          extend  ClassMethods, Migration

          translation_class.table_name = options[:table_name] if translation_class.table_name.blank?

          has_many :translations, :class_name  => translation_class.name,
                                  :foreign_key => options[:foreign_key],
                                  :dependent   => :destroy,
                                  :extend      => HasManyExtensions

          if options[:required]
            validates :translations, associated: true
          end

          after_create :save_translations!
          after_update :save_translations!

          if options[:versioning]
            ::ActiveRecord::Base.extend(Globalize::Versioning::PaperTrail)

            translation_class.has_paper_trail
            delegate :version, :versions, :to => :translation
          end

          translation_class.instance_eval %{
            attr_accessible :#{(attr_names | [:locale]).join(', :')}
          }

          # detect and apply serialization
          attr_names.each do |attr_name|
            serializer = self.serialized_attributes[attr_name.to_s]

            if serializer.present?
              if defined?(::ActiveRecord::Coders::YAMLColumn) &&
                 serializer.is_a?(::ActiveRecord::Coders::YAMLColumn)

                serializer = serializer.object_class
              end

              translation_class.send :serialize, attr_name, serializer
            end
          end
        end

        new_attr_names = attr_names - translated_attribute_names
        new_attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
        self.translated_attribute_names += new_attr_names
        if options[:required] && new_attr_names.present?
          translation_class.class_eval do
            validates *new_attr_names, presence: true
          end
        end
      end

    end

  end
end

I really do not know what to do.

Aucun commentaire:

Enregistrer un commentaire