Module: Hippo::Concerns::CodeIdentifier::ClassMethods

Defined in:
lib/hippo/concerns/code_identifier.rb

Overview

### Code Identifier Concern This adds the #has_code_identifier class method

Instance Method Summary collapse

Instance Method Details

#has_code_identifier(from: nil, max_length: 10) ⇒ Object

A 2-10 character string that identifies an entity, such as a Customer, Vendor, or SKU. The code is often assigned by the user, but may also be auto-generated by Skr::Strings.code_identifier

Parameters:

  • max_length (Integer) (defaults to: 10)

    how long the code is allowed to be

  • from (Symbol) (defaults to: nil)

    , method to call for a string to base the code upon


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hippo/concerns/code_identifier.rb', line 19

def has_code_identifier( from: nil, max_length: 10 )

    validates :code, :length=>2..max_length, :presence=>true, :uniqueness=>true

    alias_attribute :record_identifier, :code

    if from
        before_validation(:on=>:create) do
            source = self[from]
            if code.blank? && !source.blank?
                self.code = Hippo::Strings.code_identifier(source, length:max_length, padding: '')
            end
        end
    end

    before_validation do
        self.code = self.code.upcase unless self.code.blank?
    end
end