Giter VIP home page Giter VIP logo

mongoid-slug's Introduction

Mongoid Slug

Mongoid Slug generates a URL slug or permalink based on a field or set of fields in a Mongoid model.

Examples

Here's a book that embeds many authors:

class Book
  include Mongoid::Document
  include Mongoid::Slug
  field :title
  slug  :title
  embeds_many :authors
end

class Author
  include Mongoid::Document
  include Mongoid::Slug
  field :first_name
  field :last_name
  slug  :first_name, :last_name
  embedded_in :book, :inverse_of => :authors
end

The finders in our controllers should look like:

def find_book
  @book = Book.where(:slug => params[:id]).first
end

def find_book_and_author
  @book = Book.where(:slug => params[:book_id]).first
  @author = @book.authors.where(:slug => params[:id]).first
end

If you are wondering why I did not include a find_by_slug helper, read on.

To demo some more functionality in the console:

>> book = Book.create(:title => "A Thousand Plateaus")
>> book.to_param
"a-thousand-plateaus"
>> book.update_attributes(:title => "Anti Oedipus")
>> book.to_param
"anti-oedipus"
>> Book.where(:slug => 'anti-oedipus').first
#<Book _id: 4c23b1f7faa4a7479a000009, slug: "anti-oedipus", title: "Anti Oedipus">
>> author = book.authors.create(:first_name => "Gilles", :last_name => "Deleuze")
>> author.to_param
=> "gilles-deleuze"
>> author.update_attributes(:first => "Félix", :last_name => "Guattari")
>> author.to_param
=> "félix-guattari"
>> book.authors.where(:slug => 'felix-guattari).first
=> #<Author _id: 4c31e362faa4a7050e000003, slug: "félix-guattari", last_name: "Guattari", first_name: "Félix">

slug takes :as and :scoped as arguments. See models in specs for more examples.

mongoid-slug's People

Contributors

hakanensari avatar emvu avatar gerhard avatar sagmor avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

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.