class NiftyServices::BaseUpdateService

Public Instance Methods

changed_attributes() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 23
def changed_attributes
  return [] if fail?
  @changed_attributes ||= changes(@last_record, @record, changed_attributes_array)
end
execute() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 4
def execute
  execute_action do
    with_before_and_after_callbacks(:update) do
      if can_execute_action?
        duplicate_records_before_update

        @record = with_before_and_after_callbacks(:update_record) { update_record }

        if success_updated?
          success_response
        else
          errors = update_errors
          unprocessable_entity_error!(errors) unless errors.empty?
        end
      end
    end
  end
end

Private Instance Methods

can_execute?() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 55
def can_execute?
  unless valid_record?
    return not_found_error!(invalid_record_error_key)
  end

  return true
end
can_execute_action?() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 67
def can_execute_action?
  unless can_update_record?
    return (valid? ? forbidden_error!(cant_update_error_key) : false)
  end

  return true
end
can_update_record?() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 63
def can_update_record?
  not_implemented_exception(__method__)
end
cant_update_error_key() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 83
def cant_update_error_key
  "#{record_error_key}.cant_update"
end
changed_attributes_array() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 30
def changed_attributes_array
  record_allowed_attributes.keys
end
duplicate_records_before_update() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 75
def duplicate_records_before_update
  @last_record = @record.dup
end
invalid_record_error_key() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 79
def invalid_record_error_key
  "#{record_error_key}.not_found"
end
success_updated?() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 34
def success_updated?
  @record.valid?
end
update_errors() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 38
def update_errors
  @record.errors
end
update_record() click to toggle source
# File lib/nifty_services/base_update_service.rb, line 42
def update_record
  update_method = NiftyServices.configuration.update_record_method

  if update_method.respond_to?(:call)
    update_method.call(@record, record_allowed_attributes)
  else
    @record.public_send(update_method, record_allowed_attributes)
  end

  # initialize @temp_record to be used in after_update_record callback
  @temp_record = @record
end