Class: ActAsDirty::ActiveModel::Dirt
- Inherits:
-
Object
- Object
- ActAsDirty::ActiveModel::Dirt
- Includes:
- Enumerable
- Defined in:
- lib/act_as_dirty/active_model/dirt.rb
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
When passed a symbol or a name of a method, returns an array of messages for the method.
-
#added?(attribute) ⇒ Boolean
def added?(attribute, message = nil, options = {}) message = normalize_message(attribute, message, options) self.include? message end.
-
#as_json(options = nil) ⇒ Object
Returns an ActiveSupport::OrderedHash that can be used as the JSON representation for this object.
-
#clear ⇒ Object
Clear the messages.
-
#count ⇒ Object
Returns the number of dirty messages.
-
#delete(key) ⇒ Object
Delete messages for
key
. -
#each ⇒ Object
Iterates through each error key, value pair in the error messages hash.
-
#empty? ⇒ Boolean
(also: #blank?)
Returns true if no dirty messages are found, false otherwise.
-
#full_messages ⇒ Object
# Returns all the full error messages in an array.
-
#generate_message(attribute, type = :invalid, options = {}) ⇒ Object
Translates a dirty message in its default scope (
activemodel.dirty.messages
). -
#get(key) ⇒ Object
Get messages for
key
. -
#include?(attribute) ⇒ Boolean
(also: #has_key?)
Do the dirty messages include a message for the attribute
attribute
?. -
#initialize(base) ⇒ Dirt
constructor
A new instance of Dirt.
-
#keys ⇒ Object
Returns all message keys.
-
#set(key, value) ⇒ Object
Set messages for
key
tovalue
. -
#to_a ⇒ Object
Returns an array of dirty messages, with the attribute name included.
- #to_hash ⇒ Object
-
#to_xml(options = {}) ⇒ Object
Returns an xml formatted representation of the Dirt hash.
-
#values ⇒ Object
Returns all message values.
Constructor Details
#initialize(base) ⇒ Dirt
Returns a new instance of Dirt.
17 18 19 20 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 17 def initialize(base) @base = base @messages = ActiveSupport::OrderedHash.new end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
15 16 17 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 15 def @messages end |
Instance Method Details
#[](attribute) ⇒ Object
When passed a symbol or a name of a method, returns an array of messages for the method.
p.dirt[:name] # => ["has been updated from Bob to John"]
p.dirt['name'] # => ["has been updated from Bob to John"]
53 54 55 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 53 def [](attribute) get(attribute.to_sym) || set(attribute.to_sym, []) end |
#added?(attribute) ⇒ Boolean
def added?(attribute, message = nil, options = {})
message = normalize_message(attribute, message, options)
self[attribute].include? message
end
179 180 181 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 179 def added?(attribute) keys.include? attribute && [attribute].present? end |
#as_json(options = nil) ⇒ Object
Returns an ActiveSupport::OrderedHash that can be used as the JSON representation for this object.
134 135 136 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 134 def as_json(=nil) to_hash end |
#clear ⇒ Object
Clear the messages
23 24 25 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 23 def clear .clear end |
#count ⇒ Object
Returns the number of dirty messages.
107 108 109 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 107 def count to_a.size end |
#delete(key) ⇒ Object
Delete messages for key
44 45 46 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 44 def delete(key) .delete(key) end |
#each ⇒ Object
Iterates through each error key, value pair in the error messages hash. Yields the attribute and the error for that attribute. If the attribute has more than one error message, yields once for each error message.
p.errors.add(:name, "can't be blank")
p.errors.each do |attribute, errors_array|
# Will yield :name and "can't be blank"
end
p.errors.add(:name, "must be specified")
p.errors.each do |attribute, errors_array|
# Will yield :name and "can't be blank"
# then yield :name and "must be specified"
end
71 72 73 74 75 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 71 def each .each_key do |attribute| yield attribute end end |
#empty? ⇒ Boolean Also known as: blank?
Returns true if no dirty messages are found, false otherwise. If the dirty message is a string it can be empty.
113 114 115 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 113 def empty? values.compact.empty? end |
#full_messages ⇒ Object
# Returns all the full error messages in an array.
#
# class User
# cleans :name, :nickname, :email
# end
#
# company = Company.create(:name => "John Doe", :nickname => "Johnny", :email => "[email protected]")
# company.dirt.full_messages # =>
# ["Added John Doe as a name", "Added Johnny as a nickname", "Added [email protected] as an email"]
192 193 194 195 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 192 def # map { |attribute, message| full_message(attribute, message) } values.flatten end |
#generate_message(attribute, type = :invalid, options = {}) ⇒ Object
Translates a dirty message in its default scope (activemodel.dirty.messages
).
Dirty messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE
, if it’s not there, it’s looked up in models.MODEL.MESSAGE
and if that is not there also, it returns the translation of the default message (e.g. activemodel.errors.messages.MESSAGE
). The translated model name, translated attribute name and the value are available for interpolation.
When using inheritance in your models, it will check all the inherited models too, but only if the model itself hasn’t been found. Say you have class Admin < User; end
and you wanted the translation for the :blank
error message for the title
attribute, it looks for these translations:
-
activemodel.dirty.models.admin.attributes.title.blank
-
activemodel.dirty.models.admin.blank
-
activemodel.dirty.models.user.attributes.title.blank
-
activemodel.dirty.models.user.blank
-
any default you provided through the
options
hash (in theactivemodel.dirty
scope) -
activemodel.dirty.messages.blank
-
dirty.attributes.title.blank
-
dirty.messages.blank
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 236 def (attribute, type = :invalid, = {}) type = .delete(:message) if [:message].is_a?(Symbol) if @base.class.respond_to?(:i18n_scope) defaults = @base.class.lookup_ancestors.map do |klass| [ :"#{@base.class.i18n_scope}.dirty.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}", :"#{@base.class.i18n_scope}.dirty.models.#{klass.model_name.i18n_key}.#{type}" ] end else defaults = [] end defaults << .delete(:message) defaults << :"#{@base.class.i18n_scope}.dirty.messages.#{type}" if @base.class.respond_to?(:i18n_scope) defaults << :"dirty.attributes.#{attribute}.#{type}" defaults << :"dirty.messages.#{type}" defaults.compact! defaults.flatten! key = defaults.shift value = (attribute != :base ? @base.send(:read_attribute_for_cleaning, attribute) : nil) = { :default => defaults, :model => @base.class.model_name.human, :attribute => @base.class.human_attribute_name(attribute), :value => value }.merge() I18n.translate(key, ) end |
#get(key) ⇒ Object
Get messages for key
34 35 36 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 34 def get(key) [key] end |
#include?(attribute) ⇒ Boolean Also known as: has_key?
Do the dirty messages include a message for the attribute attribute
?
28 29 30 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 28 def include?(attribute) (v = [attribute]) && v.any? end |
#keys ⇒ Object
Returns all message keys
93 94 95 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 93 def keys .keys end |
#set(key, value) ⇒ Object
Set messages for key
to value
39 40 41 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 39 def set(key, value) [key] = value end |
#to_a ⇒ Object
Returns an array of dirty messages, with the attribute name included
p.dirt.add(:name, "has been updated from Bob to John")
p.dirt.add(:nickname, "has been updated from Bobby to Johnny")
p.dirt.to_a # => ["Name has been updated from Bob to John", "Nickname has been updated from Bobby to Johnny"]
102 103 104 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 102 def to_a end |
#to_hash ⇒ Object
138 139 140 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 138 def to_hash .dup end |
#to_xml(options = {}) ⇒ Object
Returns an xml formatted representation of the Dirt hash.
p.dirt.add(:name, "has been updated from Bob to John")
p.dirt.add(:nickname, "has been updated from Bobby to Johnny")
p.dirt.to_xml
# =>
# <?xml version=\"1.0\" encoding=\"UTF-8\"?>
# <dirts>
# <dirt>name has been updated from Bob to John</dirt>
# <dirt>name has been updated from Bobby to Johnny</dirt>
# </dirts>
129 130 131 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 129 def to_xml(={}) to_a.to_xml .reverse_merge(:root => "dirts", :skip_types => true) end |
#values ⇒ Object
Returns all message values
88 89 90 |
# File 'lib/act_as_dirty/active_model/dirt.rb', line 88 def values .values end |