class AttributeChanger::Committer

Attributes

error[R]
invalid_attribs[R]
result[R]

Public Class Methods

new(attribute_change) click to toggle source
# File lib/attribute_changer/committer.rb, line 4
def initialize(attribute_change)
  @result = AttributeChanger::Utils::Result.new nil, nil
  @attribute_change = attribute_change
end

Public Instance Methods

accept() click to toggle source
# File lib/attribute_changer/committer.rb, line 11
def accept
  if update_obj
    @result.success = true
  else
    @result.success = false
    false
  end
end
attrib_change() click to toggle source
# File lib/attribute_changer/committer.rb, line 9
def attrib_change; @attribute_change end
reject() click to toggle source
# File lib/attribute_changer/committer.rb, line 20
def reject
  update_status :rejected
end

Private Instance Methods

assign_error() click to toggle source
# File lib/attribute_changer/committer.rb, line 49
def assign_error
  @error = @obj.class.human_attribute_name(@obj.errors.first.first) + ' ' + @obj.errors.first.last
end
assign_invalid_attribs() click to toggle source
# File lib/attribute_changer/committer.rb, line 53
def assign_invalid_attribs
  @invalid_attribs = @obj.errors.messages.map{ |attr, errors| attr if errors.any? }.compact
end
take_into_consideration_waiting_changes() click to toggle source
# File lib/attribute_changer/committer.rb, line 57
def take_into_consideration_waiting_changes
  attrib_changes = AttributeChanger::AttributeChange.from_obj(@obj).waiting.all
  attrib_changes.each{ |ac| @obj.send "#{ac.attrib}=", ac.value }
  saved = @obj.save
  if saved
    attrib_changes.each do |ac| 
      ac.status = 'accepted'
      ac.save!
    end
  end
  saved
end
update_obj() click to toggle source
# File lib/attribute_changer/committer.rb, line 27
def update_obj
  @obj = @attribute_change.obj
  @obj.send "#{@attribute_change.attrib}=", @attribute_change.value
  return update_status(:accepted) if @obj.save
  assign_error
  assign_invalid_attribs
  if @obj.errors[@attribute_change.attrib].any?
    return update_status(:accepted) if take_into_consideration_waiting_changes
    @result.message = :invalid_attrib
  else
    return update_status(:accepted) if take_into_consideration_waiting_changes
    @result.message = :invalid_obj
    update_status :waiting
  end
  false
end
update_status(status) click to toggle source
# File lib/attribute_changer/committer.rb, line 44
def update_status(status)
  @attribute_change.status = status.to_s
  @attribute_change.save!
end