Class: Admin::SearchSuggestionSettingsController

Inherits:
BaseController show all
Defined in:
app/controllers/admin/search_suggestion_settings_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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
# File 'app/controllers/admin/search_suggestion_settings_controller.rb', line 25

def create
  @suggestion_tag = SuggestionTag.new(suggestion_tag_params)
  respond_to do |format|
    if params[:suggestion_tag][:suggestion_type] == 'outlet'
      if save_outlet?(params[:suggestion_tag][:outlet_tag_id])
        format.json do
          render json: { success: true, message: 'Data has successfully created' }
        end
      else
        format.json do
          render json: { success: false, message: 'Restaurant tag doesnt have restaurant listed' }
        end
      end
    elsif params[:suggestion_tag][:suggestion_type] == 'interesting'
      check_existing = SuggestionTag.where(suggestion_type: 'interesting').
        where('name_en = ? OR name_th = ?', params[:suggestion_tag][:name_en], params[:suggestion_tag][:name_th]).first

      if check_existing.present?
        format.json do
          render json: { success: false,
                         message: "#{params[:suggestion_tag][:name_th]} or #{params[:suggestion_tag][:name_en]} already exists" }
        end
      elsif params[:suggestion_tag][:name_en].length > 190 ||
          params[:suggestion_tag][:name_th].length > 190 ||
          params[:suggestion_tag][:name_cn].length > 190
        format.json do
          name_param = if params[:suggestion_tag][:name_en].length > 190
                         params[:suggestion_tag][:name_en]
                       elsif params[:suggestion_tag][:name_th].length > 190
                         params[:suggestion_tag][:name_th]
                       elsif params[:suggestion_tag][:name_cn].length > 190
                         params[:suggestion_tag][:name_cn]
                       end

          render json: { success: false,
                         message: "#{name_param} Failed, Keyword's Characters are too long. It should be 190 at the max." }
        end
      elsif @suggestion_tag.valid? && @suggestion_tag.save
        format.json do
          render json: { success: true, message: 'Data has successfully created' }
        end
      else
        format.json do
          render json: {
            success: false,
            errors: @suggestion_tag.errors.full_messages,
          }, status: :unprocessable_entity
        end
      end
    elsif @suggestion_tag.valid? && @suggestion_tag.save
      format.json do
        render json: { success: true, message: 'Data has successfully created' }
      end
    else
      set_data
      format.html { render :index }
      format.json do
        render json: {
          success: false,
          errors: @suggestion_tag.errors.full_messages,
        }, status: :unprocessable_entity
      end
    end
  end
end

#destroyObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/controllers/admin/search_suggestion_settings_controller.rb', line 161

def destroy
  respond_to do |format|
    if @suggestion_tag.destroy
      format.html do
        redirect_to admin_search_suggestion_settings_path, notice: 'Data destroyed successfully'
      end
      format.json do
        render json: { success: true, message: 'Data destroyed successfully' }
      end
    else
      format.html do
        redirect_back fallback_location: back_fallback_location,
                      notice: @suggestion_tag.errors.full_messages.to_sentence
      end
      format.json do
        render json: {
          success: false,
          errors: @suggestion_tag.errors.full_messages,
        }, status: :unprocessable_entity
      end
    end
  end
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/admin/search_suggestion_settings_controller.rb', line 6

def index
  respond_to do |format|
    format.html do
      set_meta_tags title: 'Search Suggestion Setting'
      render
    end
    format.json do
      render json: {
               search_suggestion_settings: @search_suggestion_settings,
               outlet_tags: @restaurant_tags[:outlet_tags],
               promotion_tags: @hashtags[:promotion_tags],
               interesting_tags: @tags[:interesting_tags],
               outlet_selected_tag: @outlet_selected_tag,
             },
             adapter: :json
    end
  end
end

#save_outlet?(_outlet_tag_id) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/admin/search_suggestion_settings_controller.rb', line 91

def save_outlet?(_outlet_tag_id)
  ActiveRecord::Base.transaction do
    check_old_outlet = SuggestionTag.where(suggestion_type: 'outlet')
    if check_old_outlet.present?
      check_old_outlet.destroy_all
    end
    rtr = RestaurantTagsRestaurant.where(restaurant_tag_id: params[:suggestion_tag][:outlet_tag_id])
    if rtr.present?
      rtr.each_with_index do |r, index|
        SuggestionTag.create(suggestion_type: 'outlet', restaurant_id: r.restaurant_id,
                             outlet_tag_id: params[:suggestion_tag][:outlet_tag_id], rank: index + 1)
      end
      true
    else
      false
    end
  end
end

#updateObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/admin/search_suggestion_settings_controller.rb', line 110

def update
  respond_to do |format|
    if @suggestion_tag.update(suggestion_tag_params)
      format.json do
        render json: { success: true, message: 'Data updated successfully' }
      end
    else
      format.json do
        render json: {
          success: false,
          errors: @suggestion_tag.errors.full_messages,
        }, status: :unprocessable_entity
      end
    end
  end
end

#update_rankObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/admin/search_suggestion_settings_controller.rb', line 127

def update_rank
  ranks = SuggestionTag.where(restaurant_id: params[:update_rank].pluck(:restaurant_id))
  ranks.each do |rank|
    params[:update_rank].each do |update_rank|
      if rank.restaurant_id == update_rank[:restaurant_id]
        rank.rank = update_rank[:rank]
        rank.save
      end
    end
  end
  respond_to do |format|
    format.json do
      render json: { success: true, message: 'Rank updated successfully' }
    end
  end
end

#update_rank_keywordObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/admin/search_suggestion_settings_controller.rb', line 144

def update_rank_keyword
  ranks = SuggestionTag.where(suggestion_type: 'interesting')
  ranks.each do |rank|
    params[:update_rank_keyword].each do |update_rank_keyword|
      if rank.id == update_rank_keyword[:id]
        rank.rank = update_rank_keyword[:rank]
        rank.save
      end
    end
  end
  respond_to do |format|
    format.json do
      render json: { success: true, message: 'Rank Keyword updated successfully' }
    end
  end
end