Sha256: 867d25324735014905d1b79003ddb04fac8d9160b0fe3c01c1017cc071e02328

Contents?: true

Size: 665 Bytes

Versions: 19

Compression:

Stored size: 665 Bytes

Contents

module Ish::Utils

  private

  def set_slug
    return if slug
    if name
      new_slug = name.downcase.gsub(/[^a-z0-9\s]/i, '').gsub(' ', '-')
    else
      new_slug = '1'
    end
    if self.class.where( slug: new_slug ).first
      loop do
        if new_slug[new_slug.length-1].to_i != 0
          # inrement last digit
          last_digit = new_slug[new_slug.length-1].to_i
          new_slug = "#{new_slug[0...new_slug.length-1]}#{last_digit+1}"
        else
          # add '-1' to the end
          new_slug = "#{new_slug}-1"
        end
        break if !self.class.where( slug: new_slug ).first
      end
    end
    self.slug = new_slug
  end

end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ish_models-0.0.33.158 lib/ish/utils.rb
ish_models-0.0.33.157 lib/ish/utils.rb
ish_models-0.0.33.156 lib/ish/utils.rb
ish_models-0.0.33.154 lib/ish/utils.rb
ish_models-0.0.33.153 lib/ish/utils.rb
ish_models-0.0.33.152 lib/ish/utils.rb
ish_models-0.0.33.151 lib/ish/utils.rb
ish_models-0.0.33.150 lib/ish/utils.rb
ish_models-0.0.33.149 lib/ish/utils.rb
ish_models-0.0.33.148 lib/ish/utils.rb
ish_models-0.0.33.147 lib/ish/utils.rb
ish_models-0.0.33.146 lib/ish/utils.rb
ish_models-0.0.33.145 lib/ish/utils.rb
ish_models-0.0.33.144 lib/ish/utils.rb
ish_models-0.0.33.143 lib/ish/utils.rb
ish_models-0.0.33.142 lib/ish/utils.rb
ish_models-0.0.33.141 lib/ish/utils.rb
ish_models-0.0.33.140 lib/ish/utils.rb
ish_models-0.0.33.139 lib/ish/utils.rb