Class: Pardner::Base
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Pardner::Base
- 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:
-
create a subclass of Pardner::Base
-
call .pardner_config to configure things
-
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
-
.howdy_pardner(decorated_class) ⇒ Object
Configure the decorator by calling this.
- .model_name ⇒ Object
Instance Method Summary collapse
- #[](attr) ⇒ Object
- #[]=(attr, value) ⇒ Object
- #attributes=(attrs = {}) ⇒ Object
-
#decorated_record ⇒ Object
Returns the decorated record.
-
#decorated_record_deep ⇒ Object
Returns the decorated record deeply, ignoring any nested decorators.
- #destroy ⇒ Object
- #errors(*args, &block) ⇒ Object
-
#initialize(decorated_record) ⇒ Base
constructor
A new instance of Base.
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #save! ⇒ Object
- #update(attrs = {}) ⇒ Object (also: #update_attributes)
- #update!(attrs = {}) ⇒ Object (also: #update_attributes!)
- #valid? ⇒ Boolean
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_name ⇒ Object
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_record ⇒ Object
Returns the decorated record
56 57 58 |
# File 'lib/pardner/base.rb', line 56 def decorated_record __getobj__ end |
#decorated_record_deep ⇒ Object
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 |
#destroy ⇒ Object
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
136 137 138 |
# File 'lib/pardner/base.rb', line 136 def new_record? decorated_record.new_record? end |
#persisted? ⇒ Boolean
132 133 134 |
# File 'lib/pardner/base.rb', line 132 def persisted? decorated_record.persisted? end |
#save ⇒ Object
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
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 |