Skip to content

Instantly share code, notes, and snippets.

@rafaels88
Last active August 26, 2018 20:40
Show Gist options
  • Save rafaels88/5529b3863c699b1cd4d20265c32d4a21 to your computer and use it in GitHub Desktop.
Save rafaels88/5529b3863c699b1cd4d20265c32d4a21 to your computer and use it in GitHub Desktop.
Hanami Sluggable (Slugify) Module
# lib/your_app/ext/sluggable.rb
module Sluggable
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def sluggable(field)
@field = field
end
def field
@field
end
end
def slug
send(self.class.field).slugify
end
end
@rafaels88
Copy link
Author

USAGE

class MyEntity
 include Hanami::Entity
 include Sluggable

 attributes :name, :slug, :other_field, :yet_another_field
 sluggable :name
end

Do not forget to create :slug field in database and mapping it to its entity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment