Sha256: 57ad2d9671189e3b338ae2ac1e5de113816c219b89016dfe3f1ba474b0847e34

Contents?: true

Size: 1001 Bytes

Versions: 50

Compression:

Stored size: 1001 Bytes

Contents

module Wco::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.blank?
    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

50 entries across 50 versions & 1 rubygems

Version Path
wco_models-3.1.0.81 app/models/wco/utils.rb
wco_models-3.1.0.80 app/models/wco/utils.rb
wco_models-3.1.0.79 app/models/wco/utils.rb
wco_models-3.1.0.78 app/models/wco/utils.rb
wco_models-3.1.0.77 app/models/wco/utils.rb
wco_models-3.1.0.76 app/models/wco/utils.rb
wco_models-3.1.0.75 app/models/wco/utils.rb
wco_models-3.1.0.74 app/models/wco/utils.rb
wco_models-3.1.0.73 app/models/wco/utils.rb
wco_models-3.1.0.72 app/models/wco/utils.rb
wco_models-3.1.0.71 app/models/wco/utils.rb
wco_models-3.1.0.70 app/models/wco/utils.rb
wco_models-3.1.0.69 app/models/wco/utils.rb
wco_models-3.1.0.67 app/models/wco/utils.rb
wco_models-3.1.0.66 app/models/wco/utils.rb
wco_models-3.1.0.65 app/models/wco/utils.rb
wco_models-3.1.0.64 app/models/wco/utils.rb
wco_models-3.1.0.63 app/models/wco/utils.rb
wco_models-3.1.0.62 app/models/wco/utils.rb
wco_models-3.1.0.61 app/models/wco/utils.rb