Module: MongoMapper::Plugins::Sluggable::ClassMethods

Defined in:
lib/mm-sluggable.rb

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/mm-sluggable.rb', line 9

def find(*args)
  arg_f = args.first
  if (args.size == 1) && arg_f.is_a?(String) && ( arg_f !~ /^[0-9a-f]{24}$/i )
    options = slug_options
    first options[:key] => arg_f
  else
    super *args
  end
end

#sluggable(to_slug = :title, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mm-sluggable.rb', line 19

def sluggable(to_slug = :title, options = {})
  class_attribute :slug_options

  self.slug_options = {
    :to_slug      => to_slug,
    :key          => :slug,
    :index        => true,
    :method       => :parameterize,
    :scope        => nil,
    :max_length   => 256,
    :callback     => [:before_validation, {:on => :create}],
    :force        => false
  }.merge(options)

  key slug_options[:key], String, :index => slug_options[:index]

  if slug_options[:callback].is_a?(Array)
    self.send(slug_options[:callback][0], :set_slug, slug_options[:callback][1])
  else
    self.send(slug_options[:callback], :set_slug)
  end
end