Class: Admin::RestaurantTagGroupsController

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

Constant Summary

Constants inherited from BaseController

BaseController::INTERNAL_SERVER_ERROR_MESSAGE

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 Method Details

#createObject



33
34
35
36
37
38
39
40
41
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 33

def create
  @tag = RestaurantTagGroup.new(tag_params)

  if @tag.save
    redirect_to admin_restaurant_tag_group_path(@tag)
  else
    render action: 'edit'
  end
end

#destroyObject



55
56
57
58
59
60
61
62
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 55

def destroy
  if @tag.soft_destroy
    redirect_to admin_restaurant_tag_groups_path
  else
    redirect_back fallback_location: back_fallback_location,
                  alert: "Unable to delete, #{@tag.errors.full_messages.to_sentence}. you might need to change the rank to another number"
  end
end

#editObject



45
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 45

def edit; end

#indexObject



20
21
22
23
24
25
26
27
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 20

def index
  set_meta_tags title: 'Restaurant Tag Group'
  @grid = RestaurantTagGroupsGrid.new(params[:admin_restaurant_tag_groups_grid]) do |scope|
    scope.order('restaurant_tag_groups.id desc').page(params[:page]).per(50)
  end
  etag = CityHash.hash32([self.class.to_s, action_name, @grid.assets.cache_key, params])
  nil unless stale?(etag: etag)
end

#newObject



29
30
31
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 29

def new
  @tag = RestaurantTagGroup.new
end

#set_ranksObject



8
9
10
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 8

def set_ranks
  @tags = RestaurantTagGroup.exclude_deleted.includes(:translations).order_by_rank
end

#showObject



43
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 43

def show; end

#tag_group_translation_statusObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 120

def tag_group_translation_status
  translation_id = params[:translation_id]

  if translation_id.blank?
    render json: { success: false, message: 'Translation ID is required.' },
           status: :unprocessable_entity
    return
  end

  begin
    redis_key = "tag_group_translation:#{translation_id}"
    result_json = $persistent_redis.with { |redis| redis.get(redis_key) }

    if result_json.nil?
      # Job is still processing or ID is invalid
      render json: {
        success: true,
        status: 'processing',
        message: 'Translation is still in progress...',
      }, status: :ok
    else
      result = JSON.parse(result_json)

      if result['status'] == 'completed'
        render json: {
          success: true,
          status: 'completed',
          translations: result['translations'],
          message: result['message'],
          completed_at: result['completed_at'],
        }, status: :ok
      elsif result['status'] == 'failed'
        render json: {
          success: false,
          status: 'failed',
          error: result['error'],
          failed_at: result['failed_at'],
        }, status: :unprocessable_entity
      end
    end
  rescue StandardError => e
    APMErrorHandler.report(e, context: { translation_id: translation_id })
    render json: {
      success: false,
      message: "Failed to check translation status: #{e.message}",
    }, status: :internal_server_error
  end
end

#translate_by_aiObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 64

def translate_by_ai
  tag_group = RestaurantTagGroup.find_by(id: params[:id])
  unless tag_group
    render json: { success: false, message: 'Restaurant tag group not found.' }, status: :not_found
    return
  end

  source_language = params[:source_language] || 'en'
  target_languages = params[:target_languages] || []
  target_fields = params[:target_fields] || []
  only_blank_languages = params[:only_blank_languages] == true || params[:only_blank_languages] == 'true'
  only_blank_fields = params[:only_blank_fields] == true || params[:only_blank_fields] == 'true'

  # Only validate selections if blank field mode is NOT enabled
  if !only_blank_languages && target_languages.blank?
    render json: { success: false, message: 'Please select target languages or enable blank field detection.' },
           status: :unprocessable_entity
    return
  end

  if !only_blank_fields && target_fields.blank?
    render json: { success: false, message: 'Please select target fields or enable blank field detection.' },
           status: :unprocessable_entity
    return
  end

  begin
    # Generate unique translation ID for async processing
    translation_id = SecureRandom.uuid

    # Enqueue background job for async translation
    RestaurantTagGroups::AiTranslationWorker.perform_async(
      translation_id,
      tag_group.id,
      source_language,
      target_languages,
      target_fields,
      only_blank_languages,
      only_blank_fields,
    )

    # Return immediately with translation ID for status polling
    render json: {
      success: true,
      translation_id: translation_id,
      message: 'Translation job started. Use the translation_id to check status.',
    }, status: :accepted
  rescue StandardError => e
    APMErrorHandler.report(e, context: { tag_group_id: tag_group.id })
    render json: {
      success: false,
      message: "Failed to start translation: #{e.message}",
    }, status: :internal_server_error
  end
end

#updateObject



47
48
49
50
51
52
53
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 47

def update
  if @tag.update_attributes(tag_params)
    redirect_to admin_restaurant_tag_group_path(@tag)
  else
    render action: 'edit'
  end
end

#update_ranksObject



12
13
14
15
16
17
18
# File 'app/controllers/admin/restaurant_tag_groups_controller.rb', line 12

def update_ranks
  if (sort = RestaurantTagGroup.exclude_deleted.sort_rank(params[:from], params[:to]))
    render json: { success: true, sorted_ranks: sort }
  else
    render json: { success: false }, status: :unprocessable_entity
  end
end