Module: Ardb::HasSlug::ClassMethods

Defined in:
lib/ardb/has_slug.rb

Instance Method Summary collapse

Instance Method Details

#ardb_has_slug_configObject



45
46
47
# File 'lib/ardb/has_slug.rb', line 45

def ardb_has_slug_config
  @ardb_has_slug_config
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
# File 'lib/ardb/has_slug.rb', line 21

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

  @ardb_has_slug_config.merge!({
    :attribute         => options[:attribute] || DEFAULT_ATTRIBUTE,
    :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(self.ardb_has_slug_config[:attribute], :on => :update)
  validates_uniqueness_of(self.ardb_has_slug_config[:attribute], {
    :case_sensitive => true,
    :scope          => options[:unique_scope]
  })

  after_create :ardb_has_slug_generate_slug
  after_update :ardb_has_slug_generate_slug
end