Giter Club home page Giter Club logo

sequel_sluggable's Introduction

sequel_sluggable

Install

Add gemcutter.org source if you don’t have it:

gem source http://gemcutter.org

Install:

[sudo] gem install sequel_sluggable

Usage

This plug-in provide functionality to allow Sequel::Model to have a slug. Slug is created in ether the before_create or before_update hooks.

To use plug-in you add plug-in to your model:

class MyModel < Sequel::Model
  plugin :sluggable, :source => :name
end

You can use following options:

frozen

Should slug be frozen once it’s generated? Default true.

sluggator

Proc or Symbol to call to create slug.

source

Column which value will be used to generate slug.

target

Column where slug will be written, defaults to :slug.

Options frozen, sluggator and target are optional.

Options are inherited when you use inheritance for your models. However you can only set options via plugin method.

You can access options for current model via reader Model#sluggable_options which is readonly.

When is slug generated?

By default slug is generated ONLY when model is created and you <strong>didn’t set it</strong>. <strong>When you update model slug is not updated by default</strong> but if you set :frozen => false, slug will be regenerated on update. Some examples:

class Item < Sequel::Model
  plugin :source => :name
  ...
end

Item.create(:name => 'X')      # Generates slug

i = Item.new(:name => 'X')
i.slug = 'X Y'                 # Sets slug manualy
i.save                         # Slug is not regenerated but the set slug is used
i.slug                         # => x-y
i.update(:name => 'Y')         # Won't regenerate slug, because slug is frozen by default
i.slug                         # => x-y

BUT:

class Item < Sequel::Model
  plugin :source => :name, :frozen => false
  ...
end

i = Item.create(:name => 'X')  # Generates slug
i.update(:name => 'Y')         # Will regenerate slug, because slug is now not frozen
i.slug                         # => y

Access/Set slug

You can access slug via your normal Sequel reader. By default that will be Model#slug method. If you customize this via :target option than you have Model#:target.

Writer for the slug is generated depending on your :target option. Default will be Model#slug= otherwise Model#:target=. You can call setter to set the slug before the creating or updating model.

Algorithm customization

You can customize algorithm of the slug creation in several places. If you provide :sluggator Proc or Symbol the sluggator will be called:

class MyModel < Sequel::Model
  plugin :sluggable,
         :source    => :name,
         :sluggator => Proc.new {|value, model| do_something }
end

OR

class MyModel < Sequel::Model
  plugin :sluggable, :source => :name, :sluggator => :my_to_slug
end

If you don’t provide :sluggator sequel_sluggable will try to use Model#to_slug(value). So if you have in your model this method it will be used:

class MyModel < Sequel::Model
  plugin :sluggable, :source => :name

  def to_slug(value)
    value.upcase
  end
end

If you don’t define Model#to_slug or :sluggator sequel_sluggable will use it’s own default implementation which does following:

'value_of_the_source_column'.chomp.downcase.gsub(/[^a-z0-9]+/,'-')

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but

    bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Contributors

  • Pavel Kunc

  • Jakub “Botanicus” Stastny

Copyright © 2009 Pavel Kunc. See LICENSE for details.

sequel_sluggable's People

Contributors

pk avatar

Watchers

Iain Barnett 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.