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.0"

Class Method Summary collapse

Class Method Details

.default_allow_underscoresObject



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

def self.default_allow_underscores
  false
end

.default_attributeObject



6
7
8
# File 'lib/much-slug.rb', line 6

def self.default_attribute
  "slug"
end

.default_preprocessorObject



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

def self.default_preprocessor
  :to_s
end

.default_separatorObject



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

def self.default_separator
  "-".freeze
end

.has_slug_changed_slug_values(record_instance) ⇒ Object



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

def self.has_slug_changed_slug_values(record_instance)
  record_instance.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.instance_exec(&entry.source_proc)
      else
        record_instance.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_instance.send(attribute) == slug_value
    yield attribute, slug_value
  end
end

.update_slugs(record) ⇒ Object



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

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