Sha256: 325868f01ac1417ad56f7a8b8d15352ed6631a2e981b3c19145566340adf6388

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

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

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

  def create
    model = get_model.build(model_params)

    if model.save
      response=Mks::Common::MethodResponse.new(true, "#{get_humanized_name} saved successfully!", nil, nil)
    else
      errors = Mks::Common::Util.error_messages(model, "#{get_humanized_name}")
      response = Mks::Common::MethodResponse.new(false, nil, nil, errors, 0)
    end
    render json: response
  end

  def update
    if @model.update(model_params)
      response = Mks::Common::MethodResponse.new(true, "#{get_humanized_name} updated successfully!", @model, nil, nil)
    else
      errors = Mks::Common::Util.error_messages(@model, "#{get_humanized_name}")
      response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
    end
    render json: response
  end
  

  private

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

  def get_model
    get_model_name.constantize
  end

  def get_humanized_name
    get_model_name.rpartition('::').last.underscore.humanize
  end

  def model_params
    nil
  end

  def set_model
    @model = get_model.find(params[:id])
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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