Coder Social home page Coder Social logo

pablo-merino / mongoid-slug Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mongoid/mongoid-slug

1.0 3.0 1.0 292 KB

Generates a URL slug/permalink based on fields in a Mongoid-based model

Home Page: http://github.com/hakanensari/mongoid-slug

License: MIT License

Ruby 100.00%

mongoid-slug's Introduction

Mongoid Slug

Mongoid Slug generates a URL slug or permalink based on one or more fields in a Mongoid model. It sits idly on top of [stringex] 1, supporting non-Latin characters.

[![travis] 2] 3

Installation

Add to your Gemfile:

gem 'mongoid_slug'

Usage

Set up a slug:

class Book
  include Mongoid::Document
  include Mongoid::Slug

  field :title
  slug :title
end

Find a record by its slug:

# GET /books/a-thousand-plateaus
book = Book.find_by_slug params[:book_id]

[Read here] 4 for all available options.

Scoping

To scope a slug by a reference association, pass :scope:

class Company
  include Mongoid::Document
  
  references_many :employees
end

class Employee
  include Mongoid::Document
  include Mongoid::Slug
  
  field :name
  referenced_in :company
  
  slug  :name, :scope => :company
end

In this example, if you create an employee without associating it with any company, the scope will fall back to the root employees collection.

Currently, if you have an irregular association name, you must specify the :inverse_of option on the other side of the assocation.

Embedded objects are automatically scoped by their parent.

The value of :scope can alternatively be a field within the model itself:

class Employee
  include Mongoid::Document
  include Mongoid::Slug
  
  field :name
  field :company_id
  
  slug  :name, :scope => :company_id
end

History

To specify that the history of a document should be kept track of, pass :history with a value of true.

class Page
  include Mongoid::Document
  include Mongoid::Slug
  
  field :title
  
  slug :title, history: true
end

The document will then be returned for any of the saved slugs:

page = Page.new title: "Home"
page.save
page.update_attributes title: "Welcome"

Page.find_by_slug("welcome") == Page.find_by_slug("home") #=> true

Reserved Slugs

Pass words you do not want to be slugged using the reserve option:

class Friend
  include Mongoid::Document

  field :name
  slug :name, reserve: ['admin', 'root']
end

friend = Friend.create name: 'admin'
Friend.find_by_slug('admin') # => nil
friend.slug # => 'admin-1'

mongoid-slug's People

Contributors

hakanensari avatar douwem avatar astjohn avatar empact avatar tiendung avatar ches avatar ajsharp avatar zefer avatar etehtsea avatar siong1987 avatar da-z avatar bdmac avatar eagleas avatar dummied avatar fklingler avatar gerhard avatar lewinski avatar iamnader avatar pablo-merino avatar fxposter avatar sporkd avatar sagmor avatar trevor avatar dmathieu avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  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.