Class: ImageManagersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/image_managers_controller.rb

Instance Method Summary collapse

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 LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Method Details

#newObject



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

def new
  @restaurant = Restaurant.includes(pictures: %i[
                                      translations
                                      taggings
                                    ]).find(current_owner.restaurant.id)
end

#sortingObject



59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/image_managers_controller.rb', line 59

def sorting
  unless Restaurants::Picture::TAGS.keys.map(&:to_s).include?(params[:tag])
    redirect_to(owner_dashboards_path, alert: 'Invalid tag') && return
  end

  @restaurant = current_owner.restaurant

  @restaurant.pictures.build

  @pictures = Restaurants::Picture.includes(:translations).where(restaurant_id: current_owner.restaurant.id).tagged_with(params[:tag]).order(:priority)
end

#taggingObject



89
90
91
# File 'app/controllers/image_managers_controller.rb', line 89

def tagging
  @images = current_owner.restaurant.pictures
end

#tagsObject



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
119
120
121
122
123
124
125
126
127
# File 'app/controllers/image_managers_controller.rb', line 93

def tags
  if request.env['REQUEST_METHOD'] == 'POST'
    pict = Restaurants::Picture.find(params[:image_manager_id])
    pict.tag_list = params[:tag_name]
    pict.priority = nil
    if pict.save
      render(json: {
               status: :success
             })
    else
      render(json: {
               status: :error,
               message: pict.errors.full_messages.join(' ')
             }, status: :unprocessable_entity)
    end

  elsif request.env['REQUEST_METHOD'] == 'DELETE'
    pict = Restaurants::Picture.find(params[:image_manager_id])
    pict.tag_list.remove(params[:tag_name])
    if pict.save
      render(json: {
               status: :success
             })
    else
      render(json: {
               status: :error
             }, status: :unprocessable_entity)
    end

  else
    render(json: {
             status: :error
           }, status: :unprocessable_entity)
  end
end

#updateObject



20
21
22
23
24
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
# File 'app/controllers/image_managers_controller.rb', line 20

def update
  @restaurant = current_owner.restaurant

  anchor = nil
  ActiveRecord::Base.transaction do
    params.require(:restaurant)[:pictures_attributes].each do |_, param|
      caption_param = {}

      MyLocaleManager.available_locales.each do |locale|
        value = param["caption_#{locale}"]
        caption_param.merge!("caption_#{locale}": value) if value.present?
      end

      if param['id'].present?
        if param['_destroy'].to_s.strip == '1'
          Restaurants::Picture.find_by(id: param['id'])&.destroy!
        elsif !caption_param.empty?
          Restaurants::Picture.find(param['id']).update!(caption_param)
        end
      elsif param['item'].present?
        param['item'].each do |item|
          picture = Restaurants::Picture.new({
            restaurant_id: @restaurant.id,
            item: File.open(item.tempfile)
          }.merge(caption_param))
          picture.tag_list.add(params[:tag])
          picture.save!

          anchor = "pict-#{picture.id}"
        end
      end
    end
  end

  redirect_to(sorting_image_managers_path(tag: params[:tag], anchor: anchor), notice: 'Success')
rescue ActiveRecord::RecordInvalid => e
  redirect_to(new_image_manager_path(tag: params[:tag]), alert: e.message)
end

#update_prioritiesObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/image_managers_controller.rb', line 71

def update_priorities
  ids = JSON.parse(params[:ids])

  ids.each_with_index do |image_id, index|
    Restaurants::Picture.find(image_id).update_column(:priority, index + 1)
  end

  render(json: {
           status: :success
         })
rescue ActiveRecord::RecordNotSaved, ActiveRecord::RecordInvalid => e
  APMErrorHandler.report(e)
  render(json: {
           status: :error,
           message: 'Something bad happened :( . Please try again later.'
         }, status: :unprocessable_entity)
end