Sha256: c2603d679d4d2c2bf9e872d3b76b629aabe9c8dc7ad9a63414e670c9675c25a6

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

module Hippo
    module Concerns

        # @see ClassMethods
        module CodeIdentifier

            extend ActiveSupport::Concern

            # ### Code Identifier Concern
            # This adds the {#has_code_identifier} class method
            module ClassMethods

                # 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}
                #
                # @param max_length [Integer] how long the code is allowed to be
                # @param from [Symbol], method to call for a string to base the code upon
                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

            end

        end
    end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hippo-fw-0.9.9 lib/hippo/concerns/code_identifier.rb
hippo-fw-0.9.8 lib/hippo/concerns/code_identifier.rb
hippo-fw-0.9.7 lib/hippo/concerns/code_identifier.rb
hippo-fw-0.9.6 lib/hippo/concerns/code_identifier.rb
hippo-fw-0.9.5 lib/hippo/concerns/code_identifier.rb
hippo-fw-0.9.4 lib/hippo/concerns/code_identifier.rb
hippo-fw-0.9.3 lib/hippo/concerns/code_identifier.rb
hippo-fw-0.9.2 lib/hippo/concerns/code_identifier.rb
hippo-fw-0.9.1 lib/hippo/concerns/code_identifier.rb