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



101
102
103
104
105
# File 'lib/pardner/base.rb', line 101

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

#new_record?Boolean

Returns:

  • (Boolean)


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

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  decorated_record.persisted?
end

#saveObject



87
88
89
90
91
92
93
94
95
# File 'lib/pardner/base.rb', line 87

def save
  valid? or return false

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

  status == true
end

#save!Object



97
98
99
# File 'lib/pardner/base.rb', line 97

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

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



107
108
109
110
# File 'lib/pardner/base.rb', line 107

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

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



113
114
115
116
# File 'lib/pardner/base.rb', line 113

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  run_callbacks :validation do
    if super && __getobj__.valid?
      true
    else
      __getobj__.errors.each do |attr, msg|
        errors.add attr, msg
      end

      false
    end
  end
end