Sha256: cf41dd7f51c6faa1942492b811c9b5494d8820b9db2bdf1e271ead4e18efda82

Contents?: true

Size: 993 Bytes

Versions: 138

Compression:

Stored size: 993 Bytes

Contents

module Ish::Utils

  def export
    out = {}
    %w| created_at updated_at |.map do |f|
      out[f] = send(f)
    end
    export_fields.map do |field|
      if field[-3..-1] == '_id'
        out[field] = send(field).to_s
      else
        out[field] = send(field)
      end
    end
    out[:_id] = id.to_s
    out.with_indifferent_access
  end

  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

138 entries across 138 versions & 1 rubygems

Version Path
ish_models-3.0.0.1 lib/ish/utils.rb
ish_models-0.0.33.304 lib/ish/utils.rb
ish_models-0.0.33.303 lib/ish/utils.rb
ish_models-0.0.33.302 lib/ish/utils.rb
ish_models-0.0.33.301 lib/ish/utils.rb
ish_models-0.0.33.300 lib/ish/utils.rb
ish_models-0.0.33.299 lib/ish/utils.rb
ish_models-0.0.33.298 lib/ish/utils.rb
ish_models-0.0.33.297 lib/ish/utils.rb
ish_models-0.0.33.296 lib/ish/utils.rb
ish_models-0.0.33.295 lib/ish/utils.rb
ish_models-0.0.33.294 lib/ish/utils.rb
ish_models-0.0.33.293 lib/ish/utils.rb
ish_models-0.0.33.292 lib/ish/utils.rb
ish_models-0.0.33.291 lib/ish/utils.rb
ish_models-0.0.33.287 lib/ish/utils.rb
ish_models-0.0.33.286 lib/ish/utils.rb
ish_models-0.0.33.285 lib/ish/utils.rb
ish_models-0.0.33.284 lib/ish/utils.rb
ish_models-0.0.33.283 lib/ish/utils.rb