Class: Pardner::Base

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Model, ActiveModel::Validations, ActiveModel::Validations::Callbacks
Defined in:
lib/pardner/base.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(decorated_record) ⇒ Base

Returns a new instance of Base.



51
52
53
# File 'lib/pardner/base.rb', line 51

def initialize(decorated_record)
  __setobj__ decorated_record
end

Class Method Details

.howdy_pardner(decorated_class) ⇒ Object

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.



45
46
47
48
49
# 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_nameObject



65
66
67
68
69
70
71
# 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

Instance Method Details

#[](attr) ⇒ Object



73
74
75
# File 'lib/pardner/base.rb', line 73

def [](attr)
  send(attr)
end

#[]=(attr, value) ⇒ Object



77
78
79
# File 'lib/pardner/base.rb', line 77

def []=(attr, value)
  send("#{attr}=", value)
end

#attributes=(attrs = {}) ⇒ Object



81
82
83
84
85
# File 'lib/pardner/base.rb', line 81

def attributes=(attrs = {})
  attrs.each do |attr, value|
    public_send "#{attr}=", value
  end
end

#decorated_recordObject

Returns the decorated record



56
57
58
# File 'lib/pardner/base.rb', line 56

def decorated_record
  __getobj__
end

#decorated_record_deepObject

Returns the decorated record deeply, ignoring any nested decorators.



61
62
63
# File 'lib/pardner/base.rb', line 61

def decorated_record_deep
  __getobj__.is_a?(Pardner::Base) ? __getobj__.decorated_record_deep : __getobj__
end

#destroyObject



104
105
106
107
108
# File 'lib/pardner/base.rb', line 104

def destroy
  ActiveRecord::Base.transaction do
    run_callbacks(:destroy) { super }
  end
end

#errors(*args, &block) ⇒ Object



140
141
142
# File 'lib/pardner/base.rb', line 140

def errors(*args, &block)
  decorated_record.errors(*args, &block)
end

#new_record?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/pardner/base.rb', line 136

def new_record?
  decorated_record.new_record?
end

#persisted?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/pardner/base.rb', line 132

def persisted?
  decorated_record.persisted?
end

#saveObject



87
88
89
90
91
92
93
94
95
96
97
98
# 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!Object



100
101
102
# File 'lib/pardner/base.rb', line 100

def save!
  save or fail InvalidModel, "Validation failed: #{errors.full_messages.join(',')}"
end

#update(attrs = {}) ⇒ Object Also known as: update_attributes



110
111
112
113
# File 'lib/pardner/base.rb', line 110

def update(attrs = {})
  self.attributes = attrs
  save
end

#update!(attrs = {}) ⇒ Object Also known as: update_attributes!



116
117
118
119
# File 'lib/pardner/base.rb', line 116

def update!(attrs = {})
  update attrs or
    fail InvalidModel, "Validation failed: #{errors.full_messages.join(',')}"
end

#valid?Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
# File 'lib/pardner/base.rb', line 122

def valid?
  run_callbacks :validation do
    if super && __getobj__.valid?
      true
    else
      false
    end
  end
end