Class: Pod4::Alert
- Inherits:
-
Object
- Object
- Pod4::Alert
- Defined in:
- lib/pod4/alert.rb
Overview
An Alert is an error, warning or note which might be raised in validation in the model.
They are, however, designed to follow all the way through the controller to the view; you should use them whenever you want to display a message on the page.
Constant Summary collapse
- ALERTTYPES =
Valid values for @type: :error, :warning, :info or :success
[:error, :warning, :info, :success]
Instance Attribute Summary collapse
-
#exception ⇒ Object
readonly
The exception attached to the alert, or nil if there isn’t one.
-
#field ⇒ Object
The field name associated with the alert, or nil.
-
#message ⇒ Object
The alert message.
-
#type ⇒ Object
readonly
The alert type.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
An array of Alert is automatically sorted into descending order of seriousness.
-
#initialize(type, field = nil, message) ⇒ Alert
constructor
A new alert must have a type (error warning info or success); there should be a message to display, obviously.
-
#log(file = '') ⇒ Object
Write self to the log.
Constructor Details
#initialize(type, field = nil, message) ⇒ Alert
A new alert must have a type (error warning info or success); there should be a message to display, obviously. Note that you can pass an exception in place of a message, in which case You may optionally specify the name of the field to be highlighted. Models will give validation alerts a field that corresponds to the model attribute; but this is not enforced here, and your controller will have to sort things out if the model is expecting different field names.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pod4/alert.rb', line 38 def initialize(type, field=nil, ) raise ArgumentError, "unknown alert type" unless ALERTTYPES.include? type.to_s.to_sym @type = type.to_s.to_sym @field = field ? field.to_sym : nil @exception = nil if .kind_of?(Exception) @exception = .dup = @exception. # SwingShift validation exceptions hold the field name @field ||= @exception.field if @exception.respond_to?(:field) else = end end |
Instance Attribute Details
#exception ⇒ Object (readonly)
The exception attached to the alert, or nil if there isn’t one
19 20 21 |
# File 'lib/pod4/alert.rb', line 19 def exception @exception end |
#field ⇒ Object
The field name associated with the alert, or nil
22 23 24 |
# File 'lib/pod4/alert.rb', line 22 def field @field end |
#message ⇒ Object
The alert message
25 26 27 |
# File 'lib/pod4/alert.rb', line 25 def end |
#type ⇒ Object (readonly)
The alert type
16 17 18 |
# File 'lib/pod4/alert.rb', line 16 def type @type end |
Instance Method Details
#<=>(other) ⇒ Object
An array of Alert is automatically sorted into descending order of seriousness
62 63 64 |
# File 'lib/pod4/alert.rb', line 62 def <=>(other) ALERTTYPES.index(self.type) <=> ALERTTYPES.index(other.type) end |
#log(file = '') ⇒ Object
Write self to the log
70 71 72 73 74 75 76 77 78 |
# File 'lib/pod4/alert.rb', line 70 def log(file='') case self.type when :error then Pod4.logger.error(file) { self. } when :warning then Pod4.logger.warn(file) { self. } else Pod4.logger.info(file) { self. } end self end |