Module: MuchSlug

Defined in:
lib/much-slug.rb,
lib/much-slug/slug.rb,
lib/much-slug/version.rb,
lib/much-slug/activerecord.rb,
lib/much-slug/has_slug_registry.rb

Defined Under Namespace

Modules: ActiveRecord, Slug Classes: HasSlugRegistry

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.default_allow_underscoresObject



21
22
23
# File 'lib/much-slug.rb', line 21

def self.default_allow_underscores
  false
end

.default_attributeObject



9
10
11
# File 'lib/much-slug.rb', line 9

def self.default_attribute
  "slug"
end

.default_preprocessorObject



13
14
15
# File 'lib/much-slug.rb', line 13

def self.default_preprocessor
  :to_s
end

.default_separatorObject



17
18
19
# File 'lib/much-slug.rb', line 17

def self.default_separator
  "-"
end

.has_slug_changed_slug_values(record) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/much-slug.rb', line 30

def self.has_slug_changed_slug_values(record)
  record.class.much_slug_has_slug_registry.each do |attribute, entry|
    # ArgumentError: no receiver given` raised when calling `instance_exec`
    # on non-lambda Procs, specifically e.g :downcase.to_proc.
    # Can't call `instance_eval` on stabby lambdas b/c `instance_eval` auto
    # passes the receiver as the first argument to the block and stabby
    # lambdas may not expect that and will ArgumentError.
    slug_source_value =
      if entry.source_proc.lambda?
        record.instance_exec(&entry.source_proc)
      else
        record.instance_eval(&entry.source_proc)
      end

    slug_value =
      Slug.new(
        slug_source_value,
        preprocessor:      entry.preprocessor_proc,
        separator:         entry.separator,
        allow_underscores: entry.allow_underscores,
      )
    next if record.public_send(attribute) == slug_value
    yield attribute, slug_value
  end
end

.update_slugs(record) ⇒ Object



25
26
27
28
# File 'lib/much-slug.rb', line 25

def self.update_slugs(record)
  record.send("much_slug_has_slug_update_slug_values")
  true
end