Class: Admin::MenusController

Inherits:
BaseController show all
Defined in:
app/controllers/admin/menus_controller.rb

Overview

Responsible to manage package's menus

Constant Summary

Constants inherited from BaseController

BaseController::INTERNAL_SERVER_ERROR_MESSAGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from AdminHelper

#dynamic_pricings_formatter, #link_to_admin_reservations_path_by_id, #link_to_admin_restaurants_path_by_id, #link_to_log, #optional_locales, #optional_locales_with_labels, #staff_signed_in?

Methods included from UpdateLocaleConcern

#setup_locale

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Attribute Details

#add_onObject (readonly)

Returns the value of attribute add_on.



10
11
12
# File 'app/controllers/admin/menus_controller.rb', line 10

def add_on
  @add_on
end

Instance Method Details

#createObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/admin/menus_controller.rb', line 33

def create
  permitted_params = params.require(:add_ons_menu).permit(:add_on_id, :add_on_type, image: [])

  payloads = permitted_params.fetch(:image, []).map do |p|
    {
      add_on_id: permitted_params[:add_on_id],
      add_on_type: permitted_params[:add_on_type],
      image: p,
    }
  end
  operation = AddOns::Menu.create payloads
  if operation.first&.id&.present?
    flash[:notice] = 'Success'
  else
    flash[:alert] = operation.first&.errors&.full_messages&.to_sentence || INTERNAL_SERVER_ERROR_MESSAGE
  end
  redirect_back fallback_location: back_fallback_location
end

#destroyObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/admin/menus_controller.rb', line 60

def destroy
  if params[:id] == 'all'
    add_on.add_on_menus.destroy_all
  else
    add_on_menu = AddOns::Menu.find_by(id: params.require(:id))
    if add_on_menu.blank?
      return render json: { message: 'Menu not found' }, status: :not_found
    end

    if add_on_menu.destroy
      render json: { message: 'Menu deleted successfully' }
    else
      render json: { message: add_on_menu.errors.full_messages.to_sentence }, status: :unprocessable_entity
    end
  end
end

#indexObject



12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/admin/menus_controller.rb', line 12

def index
  respond_to do |format|
    format.html do
      @add_on_menu = add_on.add_on_menus.build
    end
    format.json do
      render json: add_on.add_on_menus.order_by_rank
    end
  end
end

#updateObject



52
53
54
55
56
57
58
# File 'app/controllers/admin/menus_controller.rb', line 52

def update
  if add_on_menu.update params.require(:add_on_menu).permit(:index_number)
    render json: add_on_menu
  else
    render json: { message: add_on_menu.errors.full_messages.to_sentence }, status: :unprocessable_entity
  end
end

#update_imageObject



23
24
25
26
27
28
29
30
31
# File 'app/controllers/admin/menus_controller.rb', line 23

def update_image
  operation = AddOns::Menu.find(params.require(:menu_id))
  operation.image = params.require(:file)
  if operation.valid? && operation.save
    render json: { success: true }
  else
    render json: { success: false, message: operation.errors.full_messages.to_sentence }
  end
end