class Pardner::Base

A base class for creating an active model that decorate an active record model to add behavior. Common active record persistence methods are available and delegated to the decorated active record instance.

To use:

  1. create a subclass of Pardner::Base

  2. call .pardner_config to configure things

  3. override methods, add validations, add callbacks etc to get custom behavior

For example:

class BookDecorator < Pardner::Base
  pardner_config Book

  after_save :send_email

  private

  def send_email
    BookMailer.saved_book(self).deliver_now
  end
end

Public Class Methods

howdy_pardner(decorated_class) click to toggle source

Configure the decorator by calling this. Pass in one or many classes that will be delegated to. Define a block which’ll be passed a configuration object to configure more things.

# File lib/pardner/base.rb, line 45
def self.howdy_pardner(decorated_class)
  self.pardner_config = pardner_config ? pardner_config.deep_dup : Config.new
  pardner_config.decorated_class = decorated_class
  nil
end
model_name() click to toggle source
Calls superclass method
# File lib/pardner/base.rb, line 65
def self.model_name
  if pardner_config.try(:decorated_class)
    pardner_config.decorated_class.model_name
  else
    super
  end
end
new(decorated_record) click to toggle source
# File lib/pardner/base.rb, line 51
def initialize(decorated_record)
  __setobj__ decorated_record
end

Public Instance Methods

[](attr) click to toggle source
# File lib/pardner/base.rb, line 73
def [](attr)
  send(attr)
end
[]=(attr, value) click to toggle source
# File lib/pardner/base.rb, line 77
def []=(attr, value)
  send("#{attr}=", value)
end
attributes=(attrs = {}) click to toggle source
# File lib/pardner/base.rb, line 81
def attributes=(attrs = {})
  attrs.each do |attr, value|
    public_send "#{attr}=", value
  end
end
decorated_record() click to toggle source

Returns the decorated record

# File lib/pardner/base.rb, line 56
def decorated_record
  __getobj__
end
decorated_record_deep() click to toggle source

Returns the decorated record deeply, ignoring any nested decorators.

# File lib/pardner/base.rb, line 61
def decorated_record_deep
  __getobj__.is_a?(Pardner::Base) ? __getobj__.decorated_record_deep : __getobj__
end
destroy() click to toggle source
Calls superclass method
# File lib/pardner/base.rb, line 104
def destroy
  ActiveRecord::Base.transaction do
    run_callbacks(:destroy) { super }
  end
end
errors(*args, &block) click to toggle source
# File lib/pardner/base.rb, line 140
def errors(*args, &block)
  decorated_record.errors(*args, &block)
end
new_record?() click to toggle source
# File lib/pardner/base.rb, line 136
def new_record?
  decorated_record.new_record?
end
persisted?() click to toggle source
# File lib/pardner/base.rb, line 132
def persisted?
  decorated_record.persisted?
end
save() click to toggle source
Calls superclass method
# File lib/pardner/base.rb, line 87
def save
  valid? or return false

  status = nil

  ActiveRecord::Base.transaction do
    status = run_callbacks(:save) { super }
    status or raise ActiveRecord::Rollback
  end

  status == true
end
save!() click to toggle source
# File lib/pardner/base.rb, line 100
def save!
  save or fail InvalidModel, "Validation failed: #{errors.full_messages.join(',')}"
end
update(attrs = {}) click to toggle source
# File lib/pardner/base.rb, line 110
def update(attrs = {})
  self.attributes = attrs
  save
end
Also aliased as: update_attributes
update!(attrs = {}) click to toggle source
# File lib/pardner/base.rb, line 116
def update!(attrs = {})
  update attrs or
    fail InvalidModel, "Validation failed: #{errors.full_messages.join(',')}"
end
Also aliased as: update_attributes!
update_attributes(attrs = {})
Alias for: update
update_attributes!(attrs = {})
Alias for: update!
valid?() click to toggle source
Calls superclass method
# File lib/pardner/base.rb, line 122
def valid?
  run_callbacks :validation do
    if super && __getobj__.valid?
      true
    else
      false
    end
  end
end