mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.97k stars 1.19k forks source link

Using globalize gem #755

Open nitsujri opened 8 years ago

nitsujri commented 8 years ago

Thanks for this amazing gem.

We translated all of our tags and the migration works great. The trouble we're having now is new tags don't write to the table tags.name column. The causes the name to be blank even though the tag is created the first time.

When we try to reuse that tag, because of the blank name it runs into problems with:

self.find_or_create_all_with_like_by_name(*list)

The hack we built is to run self.update_columns(name: self.name) on after_create. The issue with this is it requires tag.name to always be in sync with tag_translation.name for :en.

What is the best way to use globalize? Can the dependence on tags.name be circumvented or removed?

kevin-jj commented 8 years ago

+1

lorenzosinisi commented 8 years ago

@nitsujri What about having

user.set_tag_list_on(:german, 'kein, tag, hier, suche, etc') user.tag_list_on(:german) # german tags by context

user.set_tag_list_on(:english, 'no, tags, fund, here, sorry') user.tag_list_on(:english) # english tags by context

I am of the idea that tags should always be unique and not automatically scoped or translated by the system or it may cause some problem. Let's say you have them translated but you need an api endpoint to retrieve them later (without distiction between english and germans). How would you manage that if they are scoped already? Instead, in that way, you scope them only when you need and re-use that 'context' feature already present in the gem

nitsujri commented 8 years ago

@lorenzosinisi, that does not follow the paradigms set by the globalize or I18n gems.

In the end, that doesn't solve the dependency on tag.name which is the primary problem I am raising here.

I have to sync tag.name to :en as a hack, not a solution.

kevin-jj commented 8 years ago

I overrided some method in the ActsAsTaggableOn::Tag to implement the localization of model Tag, but the name field in the tags table is still blank.

module ActsAsTaggableOn
  class Tag < ::ActiveRecord::Base
    translates :name,:slug

    def cache_key
      super + '-' + Globalize.locale.to_s
    end

    extend FriendlyId
    friendly_id :name,:use => [:slugged,:finders,:globalize]

    def self.named(name)
      if ActsAsTaggableOn.strict_case_match
        where(name: as_8bit_ascii(name))
      else
        where(name: as_8bit_ascii(unicode_downcase(name)))
      end
    end

    def self.named_any(list)
      arr = []

      list.each do |tag|
        t = ActsAsTaggableOn::Tag.where(name: tag).first
        arr << t if t.present?
      end

      return arr
    end
  end
end
alebacca89 commented 7 years ago

Can yuo put this code on initializer:

module ActsAsTaggableOn class Tag < ::ActiveRecord::Base def self.named_any(list) clause = list.map { |tag| sanitize_sql_for_named_any(tag).force_encoding('BINARY') }.join(' OR ')

  joins(:translations).where(clause)
end

private
def self.sanitize_sql_for_named_any(tag)
  #raise "#{tag}"
  if ActsAsTaggableOn.strict_case_match
    sanitize_sql(["tag_translations.name = #{binary}?", as_8bit_ascii(tag)])
  else
    sanitize_sql(['LOWER(tag_translations.name) = LOWER(?)', as_8bit_ascii(unicode_downcase(tag))])
  end
end

end end

ghost commented 7 years ago

@alebacca89 Thanks. That's works only for CRUD tag, but not work tagged_with.