Sha256: cbc84a399d1d92fc25d1c65dcf1a6d33539bfc3ebb41c043c31b1f1e130caefc

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module ElocalApiSupport
  module Actions
    module Update
      DEFAULT_IGNORED_PARAMETERS = %w(id created_at updated_at).freeze

      def update
        if lookup_object.update_attributes(parameters_available_for_update)
          render json: lookup_object
        else
          Rails.logger.info { "There was an issue updating model #{lookup_object}" }
          Rails.logger.debug { "Error details #{lookup_object.errors.to_xml}" }
          render json: { errors: lookup_object.errors }, status: 422
        end
      end

      private

      def parameters_to_ignore_from_update
        DEFAULT_IGNORED_PARAMETERS
      end

      def updatable_parameter_names
        associated_model.columns.map do |col|
          next if col.name.in?(parameters_to_ignore_from_update)

          col.array ? { col.name => [] } : col.name
        end
      end

      def parameters_available_for_update
        params[associated_model_name].permit(*updatable_parameter_names)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
elocal_api_support-1.2.0 lib/elocal_api_support/actions/update.rb
elocal_api_support-1.1.0 lib/elocal_api_support/actions/update.rb