module CoreExtensions::HerSaveOnlyChangedAttrs

WARNING: monkey patch (github.com/remi/her/blob/master/lib/her/model/relation.rb#L34)

Public Instance Methods

save() click to toggle source

Validate record before save and after the request put the errors in the right places.

@example A subscription without a customer

@subscription = Vindi::Subscription.new.tap do |s|
  s.plan_id = plan.id
  s.payment_method_code = "credit_card"
  s.save
end

@subscription.errors.full_messages # ["Customer can't be blank"]

@example A subscription with invalid plan

@subscription = Vindi::Subscription.new.tap do |s|
  s.customer_id = customer.id
  s.plan_id = 1
  s.payment_method_code = "credit_card"
  s.save
end

@subscription.errors.full_messages # ["Plan nao encontrado"]
Calls superclass method
# File lib/vindi/core_extensions/her_save_only_changed_attrs.rb, line 30
def save
  if new?
    super
  else
    save_current_changes
  end

  response_errors.any? && errors.clear && response_errors.each do |re|
    errors.add re[:attribute], re[:type], message: re[:message]
  end

  return false if errors.any?

  self
end

Private Instance Methods

filtered_changed_attributes() click to toggle source
# File lib/vindi/core_extensions/her_save_only_changed_attrs.rb, line 67
def filtered_changed_attributes
  changes.transform_values(&:last)
end
save_current_changes() click to toggle source
# File lib/vindi/core_extensions/her_save_only_changed_attrs.rb, line 48
def save_current_changes
  return self unless changed.any?

  callback = new? ? :create : :update
  method = self.class.method_for(callback)

  run_callbacks :save do
    run_callbacks callback do
      self.class.request(filtered_changed_attributes.merge(_method: method, _path: request_path)) do |parsed_data, response|
        load_from_parsed_data(parsed_data)
        return false if !response.success? || @response_errors.any?
        changes_applied
      end
    end
  end

  self
end