Sha256: 389b4ed111703952954305415cf5ec41ec74ee03aa4cb55ffe340467f5ad122e

Contents?: true

Size: 1.99 KB

Versions: 6

Compression:

Stored size: 1.99 KB

Contents

module Lanes
    module Concerns

        # @see ClassMethods
        module VisibleIdIdentifier

            extend ActiveSupport::Concern

            module InstanceMethods

                # setup the visible id to the next available #{Lanes::SequentialId}
                # @return [Integer] the assigned ID
                def assign_visible_id!
                    self.visible_id = Lanes::SequentialId.next_for( self.class )
                end
            end


            # ### Visible ID Identifier Concern
            # This adds the {#has_visible_id} class methods
            module ClassMethods

                # An auto-incrementing number that's user-visible.
                # The visible_id is stored as an integer, but a string index is generated for
                # querying by the sql like operator. The **with_visible_id** scope is available for this purpose
                #
                # The next number an also be adjusted by the end-user by setting {Lanes::SequentialId}
                # so they can set the numbers to start at
                # a specific point, which is useful for getting Invoice and other
                # numbers to match up to a legacy system
                def has_visible_id
                    include InstanceMethods
                    validates :visible_id, :presence=>{
                                  :message=>"ID was not set (should be automatically chosen)"
                              }
                    alias_attribute :record_identifier, :visible_id
                    before_validation :assign_visible_id!, :on=>:create

                    export_scope :with_visible_id, lambda{ | visid |
                        if visid.to_s =~/%/
                            where( 'cast(visible_id as varchar) like ?', visid.to_s )
                        else
                            where( 'cast(visible_id as varchar) = ?',    visid.to_s )
                        end
                    }

                end

            end
        end

    end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lanes-0.1.0 lib/lanes/concerns/visible_id_identifier.rb
lanes-0.0.8 lib/lanes/concerns/visible_id_identifier.rb
lanes-0.0.5 lib/lanes/concerns/visible_id_identifier.rb
lanes-0.0.3 lib/lanes/concerns/visible_id_identifier.rb
lanes-0.0.2 lib/lanes/concerns/visible_id_identifier.rb
lanes-0.0.1 lib/lanes/concerns/visible_id_identifier.rb