ledger_accountable 0.0.6.pre → 0.0.8.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 1276ff12e3d9530cc0828b419d62138ce14291afe0265be521ae41bbe938a629
         | 
| 4 | 
            +
              data.tar.gz: 9895169c6b508de3060d4b0289093332a6df38cb68814a6e822410689579fbed
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 91c8da55c86246f96deb68a530dd94d17249f495cfc3b370e8837418144417c71ea12ef335a6dbd53dacbf67a721455991cd8b67d1f56b9058865653c644ce12
         | 
| 7 | 
            +
              data.tar.gz: 1b43cf14d6dfbaca1453e64ab1ddd5cf2a5564dd8de76c82d33f5b94a259759cd266d8e7e5e9031c0d3c489fc42bc31891ab0ddc37e27fda952bf3c9a162dcf1
         | 
| @@ -6,12 +6,17 @@ class LedgerEntry < ActiveRecord::Base | |
| 6 6 | 
             
              belongs_to :ledger_item, polymorphic: true, optional: false
         | 
| 7 7 |  | 
| 8 8 | 
             
              enum entry_type: { addition: 0, deletion: 1, modification: 2 }
         | 
| 9 | 
            +
              enum transaction_type: { debit: 0, credit: 1 }
         | 
| 9 10 |  | 
| 10 11 | 
             
              store :metadata, coder: JSON
         | 
| 11 12 |  | 
| 13 | 
            +
              validates :transaction_type, presence: true
         | 
| 12 14 | 
             
              validates :amount_cents, presence: true
         | 
| 13 15 | 
             
              validates :entry_type, presence: true
         | 
| 14 16 |  | 
| 17 | 
            +
              scope :debits, -> { where(transaction_type: :debit) }
         | 
| 18 | 
            +
              scope :credits, -> { where(transaction_type: :credit) }
         | 
| 19 | 
            +
             | 
| 15 20 | 
             
              def to_itemized_s(line_type = :line)
         | 
| 16 21 | 
             
                I18n.t! "#{TRANSLATION_PREFIX}.#{ledger_item_type.constantize.model_name.param_key}.#{line_type}",
         | 
| 17 22 | 
             
                        metadata.symbolize_keys
         | 
| @@ -3,6 +3,7 @@ class CreateLedgerEntries < ActiveRecord::Migration[6.1] | |
| 3 3 | 
             
                create_table :ledger_entries do |t|
         | 
| 4 4 | 
             
                  t.references :owner, polymorphic: true, null: false
         | 
| 5 5 | 
             
                  t.references :ledger_item, polymorphic: true, null: false
         | 
| 6 | 
            +
                  t.integer :transaction_type, null: false
         | 
| 6 7 | 
             
                  t.integer :entry_type, null: false
         | 
| 7 8 | 
             
                  t.integer :amount_cents, default: 0, null: false
         | 
| 8 9 | 
             
                  t.text :metadata
         | 
    
        data/lib/ledger_accountable.rb
    CHANGED
    
    | @@ -1,8 +1,8 @@ | |
| 1 1 | 
             
            # LedgerAccountable adds ledger functionality to any model that acts as an item in a ledger.
         | 
| 2 2 | 
             
            #
         | 
| 3 3 | 
             
            # It supports tracking two types of ledger entries:
         | 
| 4 | 
            -
            # 1. Debits: items which decrease the ledger balance (for example, an item being sold)
         | 
| 5 | 
            -
            # 2. Credits: items which increase the ledger balance (for example, a payment taken)
         | 
| 4 | 
            +
            # 1. Debits: outgoing items which decrease the ledger balance (for example, an item being sold)
         | 
| 5 | 
            +
            # 2. Credits: incoming items which increase the ledger balance (for example, a payment taken)
         | 
| 6 6 | 
             
            #
         | 
| 7 7 | 
             
            # Usage:
         | 
| 8 8 | 
             
            # Include LedgerAccountable in a model to have it generate ledger entries based on its
         | 
| @@ -87,7 +87,7 @@ module LedgerAccountable | |
| 87 87 | 
             
                # the name of the attribute or method which determines the ledger amount
         | 
| 88 88 | 
             
                class_attribute :ledger_net_amount_method
         | 
| 89 89 | 
             
                # the type of ledger entry to create - debit or credit
         | 
| 90 | 
            -
                class_attribute : | 
| 90 | 
            +
                class_attribute :transaction_type
         | 
| 91 91 | 
             
                # attributes of the LedgerAccountable that should trigger a ledger entry when changed
         | 
| 92 92 | 
             
                class_attribute :ledger_attributes
         | 
| 93 93 | 
             
              end
         | 
| @@ -97,7 +97,7 @@ module LedgerAccountable | |
| 97 97 | 
             
                # to be updated when the provided attributes (or ledger owner) are changed
         | 
| 98 98 | 
             
                def track_ledger(ledger_owner, options = {})
         | 
| 99 99 | 
             
                  validate_and_assign_ledger_owner(ledger_owner)
         | 
| 100 | 
            -
                   | 
| 100 | 
            +
                  validate_and_assign_transaction_type(options)
         | 
| 101 101 | 
             
                  validate_and_assign_ledger_amount_attribute(options)
         | 
| 102 102 | 
             
                  validate_net_amount_method(options)
         | 
| 103 103 | 
             
                  validate_and_assign_ledger_attributes(options)
         | 
| @@ -112,13 +112,13 @@ module LedgerAccountable | |
| 112 112 | 
             
                  self.ledger_owner = ledger_owner
         | 
| 113 113 | 
             
                end
         | 
| 114 114 |  | 
| 115 | 
            -
                def  | 
| 115 | 
            +
                def validate_and_assign_transaction_type(options)
         | 
| 116 116 | 
             
                  if options[:type].present?
         | 
| 117 117 | 
             
                    raise 'LedgerAccountable type must be :debit or :credit' unless %i[debit credit].include?(options[:type])
         | 
| 118 118 |  | 
| 119 | 
            -
                    self. | 
| 119 | 
            +
                    self.transaction_type = options[:type]
         | 
| 120 120 | 
             
                  else
         | 
| 121 | 
            -
                    self. | 
| 121 | 
            +
                    self.transaction_type = :credit
         | 
| 122 122 | 
             
                  end
         | 
| 123 123 | 
             
                end
         | 
| 124 124 |  | 
| @@ -162,7 +162,6 @@ module LedgerAccountable | |
| 162 162 | 
             
                        "LedgerAccountable model '#{model_name}' specified #{self.class.ledger_amount_attribute} for track_ledger :amount, but does not implement #{self.class.ledger_amount_attribute}"
         | 
| 163 163 | 
             
                end
         | 
| 164 164 |  | 
| 165 | 
            -
                ledger_amount_multiplier = self.class.ledger_type == :credit ? 1 : -1
         | 
| 166 165 | 
             
                ledger_amount_multiplier * (send(self.class.ledger_amount_attribute) || 0)
         | 
| 167 166 | 
             
              end
         | 
| 168 167 |  | 
| @@ -170,7 +169,8 @@ module LedgerAccountable | |
| 170 169 | 
             
              # stored on the LedgerAccountable object
         | 
| 171 170 | 
             
              def net_ledger_amount
         | 
| 172 171 | 
             
                if self.class.ledger_net_amount_method
         | 
| 173 | 
            -
                  send(self.class.ledger_net_amount_method)
         | 
| 172 | 
            +
                  net_amount_result = send(self.class.ledger_net_amount_method)
         | 
| 173 | 
            +
                  ledger_amount_multiplier * net_amount_result
         | 
| 174 174 | 
             
                else
         | 
| 175 175 | 
             
                  unless attribute_method?(self.class.ledger_amount_attribute.to_s)
         | 
| 176 176 | 
             
                    # if a method is provided to compute ledger_amount,
         | 
| @@ -179,9 +179,9 @@ LedgerAccountable model '#{model_name}' appears to use a method for track_ledger | |
| 179 179 | 
             
            but did not provide an option for :net_amount. This can lead to unexpected ledger entry amounts when modifying #{model_name}.
         | 
| 180 180 | 
             
            "
         | 
| 181 181 | 
             
                  end
         | 
| 182 | 
            -
             | 
| 183 | 
            -
                   | 
| 184 | 
            -
                   | 
| 182 | 
            +
             | 
| 183 | 
            +
                  previous_ledger_amount = ledger_amount_multiplier * attribute_was(self.class.ledger_amount_attribute)
         | 
| 184 | 
            +
                  ledger_amount - (previous_ledger_amount || 0)
         | 
| 185 185 | 
             
                end
         | 
| 186 186 | 
             
              end
         | 
| 187 187 |  | 
| @@ -305,6 +305,7 @@ but did not provide an option for :net_amount. This can lead to unexpected ledge | |
| 305 305 | 
             
                        # which will rollback the attempt to save the LedgerAccountable object
         | 
| 306 306 | 
             
                        owner: owner,
         | 
| 307 307 | 
             
                        ledger_item: self,
         | 
| 308 | 
            +
                        transaction_type: self.transaction_type,
         | 
| 308 309 | 
             
                        entry_type: entry_type,
         | 
| 309 310 | 
             
                        amount_cents: amount,
         | 
| 310 311 | 
             
                        metadata: metadata
         | 
| @@ -352,6 +353,10 @@ but did not provide an option for :net_amount. This can lead to unexpected ledge | |
| 352 353 | 
             
                end
         | 
| 353 354 | 
             
              end
         | 
| 354 355 |  | 
| 356 | 
            +
              def ledger_amount_multiplier
         | 
| 357 | 
            +
                self.class.transaction_type == :credit ? 1 : -1
         | 
| 358 | 
            +
              end
         | 
| 359 | 
            +
             | 
| 355 360 | 
             
              # An overrideable method to determine if the object should be persisted in the ledger
         | 
| 356 361 | 
             
              #
         | 
| 357 362 | 
             
              # For example, payments should not be recorded in a ledger until they're finalized
         | 
| @@ -379,3 +384,4 @@ but did not provide an option for :net_amount. This can lead to unexpected ledge | |
| 379 384 | 
             
                @_destroy_callback_already_called ||= false
         | 
| 380 385 | 
             
              end
         | 
| 381 386 | 
             
            end
         | 
| 387 | 
            +
             | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: ledger_accountable
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.8.pre
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Brendan Maclean
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2024-06- | 
| 12 | 
            +
            date: 2024-06-24 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: activerecord
         |