Class: Webhookdb::Postgres::Model

Inherits:
Object
  • Object
show all
Extended by:
ModelUtilities
Includes:
Appydays::Configurable, Appydays::Loggable
Defined in:
lib/webhookdb/postgres/model.rb

Class Method Summary collapse

Methods included from ModelUtilities

extended, find_or_create_or_find

Class Method Details

.add_extensions(*modules) ⇒ Object

Add one or more extension modules to the receiving class. This allows subsystems like Orders, etc. to decorate models outside of their purview without introducing unnecessary dependencies.

Each one of the given modules will be included in the receiving model class, and if it also contains a constant called ClassMethods, the model class will be also be extended with it.

Examples:

Add order methods to Webhookdb::Customer


module Webhookdb::Orders::CustomerExtensions

    # Add some associations for Order models
    def included( model )
        super
        model.one_to_many :orders, Sequel[:app][:orders]
    end

    def first_order
        self.orders.first
    end

end


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/webhookdb/postgres/model.rb', line 63

def self.add_extensions(*modules)
  self.logger.info "Adding extensions to %p: %p" % [self, modules]

  modules.each do |mod|
    include(mod)
    if mod.const_defined?(:ClassMethods)
      submod = mod.const_get(:ClassMethods)
      self.extend(submod)
    end
    if mod.const_defined?(:PrependedMethods)
      submod = mod.const_get(:PrependedMethods)
      prepend(submod)
    end
  end
end