Sha256: 9a7c203a13eec1ff134a64bd1b42375a75366e879032109c0b1a8699b28c0d57

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Skr
    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]
                            unless source.blank?
                                self.code ||= Skr::Strings.code_identifier( source, length:max_length )
                            end
                        end
                    end

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

            end

        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stockor-0.1.7 lib/skr/concerns/code_identifier.rb
stockor-0.1.5 lib/skr/concerns/code_identifier.rb