class AttributeChanger::Performer

Attributes

attrib[R]
attrib_change[R]
error[R]
obj[R]
result[R]

Public Class Methods

new(attribs) click to toggle source
# File lib/attribute_changer/performer.rb, line 4
def initialize(attribs)
  @obj = attribs[:obj]
  @attrib = attribs[:attrib]
  @value = attribs[:value]
end

Public Instance Methods

save() click to toggle source
# File lib/attribute_changer/performer.rb, line 10
def save
  valid
  return false if result && !result.success
  create_attribute_change
  @result = AttributeChanger::Utils::Result.new true, nil
  true
end

Private Instance Methods

assign_error() click to toggle source
# File lib/attribute_changer/performer.rb, line 64
def assign_error
  @error = @obj.errors[@attrib].first
end
attrib_exists?() click to toggle source
# File lib/attribute_changer/performer.rb, line 21
def attrib_exists?
  (@obj.attributes.keys - %w(id created_at updated_at)).include? @attrib.to_s
end
attribute_allowed?() click to toggle source
# File lib/attribute_changer/performer.rb, line 25
def attribute_allowed?
  @obj.class.attributes_allowed_for_step_change.include? @attrib
end
create_attribute_change() click to toggle source

END validation

# File lib/attribute_changer/performer.rb, line 74
def create_attribute_change
  @attrib_change = AttributeChanger::AttributeChange.new do |o|
    o.obj = @obj
    o.attrib = @attrib
    o.value = @value
  end
  @attrib_change.save!
end
reset_obj() click to toggle source
# File lib/attribute_changer/performer.rb, line 68
def reset_obj
  @obj.errors.clear
  @obj.reload
end
same_value?() click to toggle source
# File lib/attribute_changer/performer.rb, line 60
def same_value?
  @obj.send(@attrib) == @value
end
valid() click to toggle source

BEGIN validation

# File lib/attribute_changer/performer.rb, line 30
def valid
  unless attribute_allowed?
    @result = AttributeChanger::Utils::Result.new false, :not_allowed
    return
  end

  unless attrib_exists?
    @result = AttributeChanger::Utils::Result.new false, :missing_attribute
    return
  end

  if same_value?
    @result = AttributeChanger::Utils::Result.new false, :same
    return
  end

  unless valid_attrib?
    assign_error
    reset_obj
    @result = AttributeChanger::Utils::Result.new false, :invalid
    return
  end
end
valid_attrib?() click to toggle source
# File lib/attribute_changer/performer.rb, line 54
def valid_attrib?
  @obj.send "#{@attrib}=", @value
  @obj.valid?
  @obj.errors[@attrib].empty?
end