Module: DataMapper::Is::Sluggable

Defined in:
lib/dm-is-sluggable/is/sluggable.rb

Defined Under Namespace

Classes: InvalidSlugError

Instance Method Summary collapse

Instance Method Details

#is_sluggable(source = :to_slug, opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dm-is-sluggable/is/sluggable.rb', line 9

def is_sluggable(source = :to_slug, opts = {})
  class_variable_set(:@@dm_is_sluggable_options, {})

  extend ClassMethods
  include InstanceMethods
  
  slug_options[:source] = source
  
  property_name = if opts[:property].is_a?(Hash)
    opts[:property].delete(:name).to_sym
  else
    (opts[:property] || :slug).to_sym
  end
  property_opts = opts[:property].is_a?(Hash) ? opts[:property] : {}
  slug_options[:property_name] = property_name
  property property_name, String, property_opts.merge(:unique_index => true) unless slug_property
  
  slug_options[:permanent?] = opts[:permanent?] || false
  slug_options[:max_length] = opts[:max_length]
  
  DataMapper.logger.debug(slug_options.inspect)
  
  before :save, :sluggify
end