Class: ActAsDirty::ActiveModel::Dirt

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/act_as_dirty/active_model/dirt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#messagesObject (readonly)

Returns the value of attribute messages.



15
16
17
# File 'lib/act_as_dirty/active_model/dirt.rb', line 15

def messages
  @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

Returns:

  • (Boolean)


179
180
181
# File 'lib/act_as_dirty/active_model/dirt.rb', line 179

def added?(attribute)
  keys.include? attribute && messages[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(options=nil)
  to_hash
end

#clearObject

Clear the messages



23
24
25
# File 'lib/act_as_dirty/active_model/dirt.rb', line 23

def clear
  messages.clear
end

#countObject

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)
  messages.delete(key)
end

#eachObject

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
  messages.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.

Returns:

  • (Boolean)


113
114
115
# File 'lib/act_as_dirty/active_model/dirt.rb', line 113

def empty?
  values.compact.empty?
end

#full_messagesObject

# 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 full_messages
#        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 the activemodel.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 generate_message(attribute, type = :invalid, options = {})
  type = options.delete(:message) if options[: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 << options.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)

  options = {
    :default => defaults,
    :model => @base.class.model_name.human,
    :attribute => @base.class.human_attribute_name(attribute),
    :value => value
  }.merge(options)

  I18n.translate(key, options)
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)
  messages[key]
end

#include?(attribute) ⇒ Boolean Also known as: has_key?

Do the dirty messages include a message for the attribute attribute?

Returns:

  • (Boolean)


28
29
30
# File 'lib/act_as_dirty/active_model/dirt.rb', line 28

def include?(attribute)
  (v = messages[attribute]) && v.any?
end

#keysObject

Returns all message keys



93
94
95
# File 'lib/act_as_dirty/active_model/dirt.rb', line 93

def keys
  messages.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)
  messages[key] = value
end

#to_aObject

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
  full_messages
end

#to_hashObject



138
139
140
# File 'lib/act_as_dirty/active_model/dirt.rb', line 138

def to_hash
  messages.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(options={})
  to_a.to_xml options.reverse_merge(:root => "dirts", :skip_types => true)
end

#valuesObject

Returns all message values



88
89
90
# File 'lib/act_as_dirty/active_model/dirt.rb', line 88

def values
  messages.values
end