Module: CoreExtensions::HerSaveOnlyChangedAttrs

Defined in:
lib/vindi/core_extensions/her_save_only_changed_attrs.rb

Overview

Instance Method Summary collapse

Instance Method Details

#saveObject

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

Examples:

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"]

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"]


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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