Sha256: ec2ab2b1edbbe9047a964da1a7798203ea139f13a127bf1c785759dc0711be73

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module C80Estate
  class ItemProp < ActiveRecord::Base

    belongs_to :area
    belongs_to :property
    belongs_to :prop_name

    before_save :before_save_format_value

    private

    def self.capz
      [24, 36, 46]
    end

    def self.uppz
      [27, 37, 38]
    end

    def self.siz
      [23]
    end

    def before_save_format_value

      v = self.value
      uom = prop_name.uom

      # удаляем пробелы в начале и в конце строки
      v = v.strip! || v

      # числовые значения преобразуем в числа
      if uom.present? && uom.is_number

        v = v.gsub(' ', '')
        v = v.gsub(',', '.')
        v = v[/([0-9.]+)/]

        # нечисловые значения: либо capitalize, либо upcase, либо downcase
      else

        if prop_name_id.in?(ItemProp.capz)
          v = v.mb_chars.capitalize.to_s
        elsif prop_name_id.in?(ItemProp.uppz)
          v = v.mb_chars.upcase.to_s
        else
          v = v.mb_chars.downcase.to_s
        end

      end

      if prop_name_id.in?(ItemProp.siz)
        v = v.gsub(',', '.')
        sizes = v.scan(/([0-9,]+)/)
        oum = v.scan(/[мс]*м/)
        v = sizes.join(" x ")
        if oum.count > 0
          v += " #{oum[0]}"
        else
          v += ' мм'
        end
      end


      self.value = v

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
c80_estate-0.1.0 app/models/c80_estate/item_prop.rb