Module: Ardb::HasSlug::ClassMethods

Defined in:
lib/ardb/has_slug.rb

Instance Method Summary collapse

Instance Method Details

#ardb_has_slug_configsObject



48
49
50
# File 'lib/ardb/has_slug.rb', line 48

def ardb_has_slug_configs
  @ardb_has_slug_configs
end

#has_slug(options = nil) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ardb/has_slug.rb', line 21

def has_slug(options = nil)
  options ||= {}
  raise(ArgumentError, "a source must be provided") unless options[:source]

  attribute = (options[:attribute] || DEFAULT_ATTRIBUTE).to_sym
  @ardb_has_slug_configs[attribute].merge!({
    :source_proc       => options[:source].to_proc,
    :preprocessor_proc => (options[:preprocessor] || DEFAULT_PREPROCESSOR).to_proc,
    :separator         => options[:separator] || DEFAULT_SEPARATOR,
    :allow_underscores => !!options[:allow_underscores]
  })

  # since the slug isn't written till an after callback we can't always
  # validate presence of it
  validates_presence_of(attribute, :on => :update)

  if options[:skip_unique_validation] != true
    validates_uniqueness_of(attribute, {
      :case_sensitive => true,
      :scope          => options[:unique_scope]
    })
  end

  after_create :ardb_has_slug_generate_slugs
  after_update :ardb_has_slug_generate_slugs
end