Code Monkey home page Code Monkey logo

Comments (10)

jondal avatar jondal commented on August 30, 2024

+1

from mongoid-slug.

shenoudab avatar shenoudab commented on August 30, 2024

+1

from mongoid-slug.

digitalplaywright avatar digitalplaywright commented on August 30, 2024

The best practice for this is to create an embedded collection titles that holds the localized name:

class Album
  include Mongoid::Document
  embeds_many :titles
end

class Title
  include Mongoid::Document
  include Mongoid::Slug

  field :language, type: String

  field :name, type: String
  slug :name

  embedded_in :album
end

Please let us know if that works for you.

from mongoid-slug.

jschwindt avatar jschwindt commented on August 30, 2024

I've just crafted an modified version of the original mongoid_slug that fit our needs. If anyone can test it and send some feedback it will be very welcome: https://github.com/comenta-tv/mongoid_localized_slug

from mongoid-slug.

digitalplaywright avatar digitalplaywright commented on August 30, 2024

@jschwindt I would like to understand this problem better. How would you ideally like localized finds to work? Do you always store one slug and then do a lookup in a hash for translations when calling to_params?

from mongoid-slug.

jschwindt avatar jschwindt commented on August 30, 2024

@digitalplaywright: I converted the slug attribute from a String to an Array and for every translations I store the corresponding slug in that array. Then the find method looks for any occurrence of the slug you are looking for regardless of its language. For example:

I18n.locale = :en
book = Book.create title: 'English title'
I18n.locale = :es
book = Book.create title: 'Título en español'
book.save

# now book contains:
book.title_translations   # { en: 'English title', es: 'Título en español' }
book.slug                 # [ 'english-title', 'titulo-en-espanol' ]

# the to_param method will generate the slug according to the language
I18n.locale = :en
book.to_param   # 'english-title'
I18n.locale = :es
book.to_param   # 'titulo-en-espanol'

# and the following find will return the same Book object:
b1 = Book.find 'english-title'
b2 = Book.find 'titulo-en-espanol'

b1 == b2  # true

Does it make sense to you?

from mongoid-slug.

digitalplaywright avatar digitalplaywright commented on August 30, 2024

@jschwindt That makes sense. How does this work with slug history? Also, why did you choose to use an array instead of an embedded colleciton?

from mongoid-slug.

jschwindt avatar jschwindt commented on August 30, 2024

I had to remove the history feature because we didn't need it in our project and because the code started to get a bit messy.

Regarding not using an embedded collection, I thought an array was simple enough for what I needed, easy to build, to add an index and to search on.

from mongoid-slug.

digitalplaywright avatar digitalplaywright commented on August 30, 2024

You can index and search on embedded fields in Mongoid, like e.g:

class Author
include Mongoid::Document
index({:'books.name' => 1})

end

Author.where("books.name" => "sewid").all

So you could index on two attributes in an embedded document: a field for the language and a field for the slug. However, if a domain specific slug implementation works well for your use-case I don't see any problems with that.

from mongoid-slug.

digitalplaywright avatar digitalplaywright commented on August 30, 2024

I will close this issue for now since embedded documents can be used for this purpose, and @jschwindts method can not be merged with master since it conflicts with core functionality.

from mongoid-slug.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.