Sha256: c463180738eec8a6ec1b4aa57145193295b30f60bc5c0e62487cb16db8abcaee

Contents?: true

Size: 1.59 KB

Versions: 9

Compression:

Stored size: 1.59 KB

Contents

require 'mks/common/methodresponse'
require 'mks/common/util'
module LookupCommon
  extend ActiveSupport::Concern

  included do
    before_action :set_lookup, only: [:update]
  end

  def index
    p = params[:type]
    clazz = get_model(p)
    response = Mks::Common::MethodResponse.new(true, nil, clazz.all, nil, nil)
    render json: response
  end

  def create
    clazz = get_model
    obj = clazz.new(lookup_params)
    class_name = get_model_name.split('::').last.underscore.humanize
    if obj.save
      response = Mks::Common::MethodResponse.new(true, "#{class_name} saved successfully!",obj, nil, nil)
    else
      errors = Mks::Common::Util.error_messages obj, class_name
      response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
    end
    render json: response
  end

  def update
    class_name = get_model_name.split('::').last.underscore.humanize
    if @lookup.update(lookup_params)
      response = Mks::Common::MethodResponse.new(true, "#{class_name} updated successfully!", @lookup, nil, nil)
    else
      errors = Mks::Common::Util.error_messages @lookup, class_name
      response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
    end
    render json: response
  end

  def get_model(type = nil)
    if type
      return type.constantize
    end
    get_model_name.constantize
  end

  def get_model_name
    name = self.class.to_s
    name.slice!('Controller')
    name.singularize
  end

  private

  def set_lookup
    clazz = get_model
    @lookup = clazz.find(params[:id])
  end

  def lookup_params
    params.require(:lookup).permit(:code, :name)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
logistics_core-21.11.2 app/controllers/concerns/lookup_common.rb
logistics_core-21.11.1 app/controllers/concerns/lookup_common.rb
logistics_core-21.08.1 app/controllers/concerns/lookup_common.rb
logistics_core-21.03.1 app/controllers/concerns/lookup_common.rb
logistics_core-20.10.3 app/controllers/concerns/lookup_common.rb
logistics_core-20.10.2 app/controllers/concerns/lookup_common.rb
logistics_core-20.10.1 app/controllers/concerns/lookup_common.rb
logistics_core-20.9.1 app/controllers/concerns/lookup_common.rb
logistics_core-20.8.1 app/controllers/concerns/lookup_common.rb