Sha256: 398e97259111cd624768b7d1de1f303182927082d72429256d2dfe04976bc80b

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module ForestLiana
  class ResourceUpdater
    attr_accessor :record
    attr_accessor :errors

    def initialize(resource, params)
      @resource = resource
      @params = params
      @errors = nil
    end

    def perform
      begin
        @record = @resource.find(@params[:id])

        if has_strong_parameter
          @record.update_attributes(resource_params)
        else
          @record.update_attributes(resource_params, without_protection: true)
        end
      rescue ActiveRecord::StatementInvalid => exception
        # NOTICE: SQL request cannot be executed properly
        @errors = [{ detail: exception.cause.error }]
      rescue ForestLiana::Errors::SerializeAttributeBadFormat => exception
        @errors = [{ detail: exception.message }]
      rescue => exception
        @errors = [{
            detail: 'Cannot update the record due to an unexpected error.'
          }]
      end
    end

    def resource_params
      ResourceDeserializer.new(@resource, @params, false).perform
    end

    def has_strong_parameter
      @resource.instance_method(:update_attributes!).arity == 1
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
forest_liana-1.6.13 app/services/forest_liana/resource_updater.rb
forest_liana-1.6.12 app/services/forest_liana/resource_updater.rb