Sha256: 393759dcf580398718a133400dae4b30a48e94e0ca2aeb0c95337cdf3fe85978

Contents?: true

Size: 1.5 KB

Versions: 12

Compression:

Stored size: 1.5 KB

Contents

module Lanes
    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 ||= Lanes::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

12 entries across 12 versions & 1 rubygems

Version Path
lanes-0.8.3 lib/lanes/concerns/code_identifier.rb
lanes-0.8.2 lib/lanes/concerns/code_identifier.rb
lanes-0.8.1 lib/lanes/concerns/code_identifier.rb
lanes-0.8.0 lib/lanes/concerns/code_identifier.rb
lanes-0.7.0 lib/lanes/concerns/code_identifier.rb
lanes-0.6.1 lib/lanes/concerns/code_identifier.rb
lanes-0.6.0 lib/lanes/concerns/code_identifier.rb
lanes-0.5.6 lib/lanes/concerns/code_identifier.rb
lanes-0.5.5 lib/lanes/concerns/code_identifier.rb
lanes-0.5.0 lib/lanes/concerns/code_identifier.rb
lanes-0.4.0 lib/lanes/concerns/code_identifier.rb
lanes-0.3.0 lib/lanes/concerns/code_identifier.rb