Module: Straight::OrderModule

Included in:
Order
Defined in:
lib/straight/order.rb

Overview

This module should be included into your own class to extend it with Order functionality. For example, if you have a ActiveRecord model called Order, you can include OrderModule into it and you’ll now be able to do everything to check order’s status, but you’ll also get AR Database storage funcionality, its validations etc.

The right way to implement this would be to do it the other way: inherit from Straight::Order, then include ActiveRecord, but at this point ActiveRecord doesn’t work this way. Furthermore, some other libraries, like Sequel, also require you to inherit from them. Thus, the module.

When this module is included, it doesn’t actually include all the methods, some are prepended (see Ruby docs on #prepend). It is important specifically for getters and setters and as a general rule only getters and setters are prepended.

If you don’t want to bother yourself with modules, please use Straight::Order class and simply create new instances of it. However, if you are contributing to the library, all new funcionality should go to either Straight::OrderModule::Includable or Straight::OrderModule::Prependable (most likely the former).

Defined Under Namespace

Modules: Includable, Prependable Classes: IncorrectAmount

Constant Summary collapse

STATUSES =

Worth noting that statuses above 1 are immutable. That is, an order status cannot be changed if it is more than 1. It makes sense because if an order is paid (5) or expired (2), nothing else should be able to change the status back. Similarly, if an order is overpaid (4) or underpaid (5), it requires admin supervision and possibly a new order to be created.

{
  new:          0, # no transactions received
  unconfirmed:  1, # transaction has been received doesn't have enough confirmations yet
  paid:         2, # transaction received with enough confirmations and the correct amount
  underpaid:    3, # amount that was received in a transaction was not enough
  overpaid:     4, # amount that was received in a transaction was too large
  expired:      5, # too much time passed since creating an order
  canceled:     6, # user decides to economize
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#old_statusObject (readonly)

Returns the value of attribute old_status.



48
49
50
# File 'lib/straight/order.rb', line 48

def old_status
  @old_status
end

Class Method Details

.included(base) ⇒ Object

Only add getters and setters for those properties in the extended class that don’t already have them. This is very useful with ActiveRecord for example where we don’t want to override AR getters and setters that set attribtues.



23
24
25
26
27
28
29
30
31
32
# File 'lib/straight/order.rb', line 23

def self.included(base)
  base.class_eval do
    [:amount, :amount_paid, :address, :gateway, :keychain_id, :status, :tid, :title, :callback_url, :test_mode].each do |field|
      attr_reader field unless base.method_defined?(field)
      attr_writer field unless base.method_defined?("#{field}=")
    end
    prepend Prependable
    include Includable
  end
end