Class: ImagesController

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

Overview

rubocop:disable ClassLength

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_user, #current_user Used for unit testing, this is normally managed by Devise

Instance Method Summary collapse

Methods inherited from ApplicationController

#clear_search_index

Instance Method Details

#autocomplete_tagsObject



213
214
215
216
217
218
219
220
# File 'app/controllers/images_controller.rb', line 213

def autocomplete_tags
  @tags = ActsAsTaggableOn::Tag.
    where("name LIKE ?", "#{params[:q]}%").
    order(:name)
  respond_to do |format|
    format.json { render json: @tags.map{|t| {id: t.name, name: t.name}}}
  end
end

#createObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/images_controller.rb', line 171

def create
  @image = Image.new(image_params)
  if @image.save
    if request.format.html?
      redirect_to @image, notice: 'Image was successfully created.'
    else
      render :json => @image.to_json
    end
  else
    if request.format.html?
      render :new
    else
      render :json => { :errors => @image.errors.full_messages }
    end
  end
end

#destroyObject



208
209
210
211
# File 'app/controllers/images_controller.rb', line 208

def destroy
  @image.destroy
  redirect_to images_url, notice: 'Image was successfully destroyed.'
end

#editObject

GET /images/1/edit



165
166
# File 'app/controllers/images_controller.rb', line 165

def edit
end

#exportObject



222
223
224
# File 'app/controllers/images_controller.rb', line 222

def export 
  send_data Image.all.to_csv
end

#importObject



226
227
228
229
230
231
232
233
234
# File 'app/controllers/images_controller.rb', line 226

def import
  begin
    Image.import(params[:file])
    redirect_to root_path, {notice: "Images imported."}
  rescue => e
    logger.error e.message
    redirect_to root_path, {alert: "Images failed to import. " + e.message}
  end
end

#indexObject



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/images_controller.rb', line 48

def index
  #the status_ids param is used by JSON endpoint only
  @status_ids = [2]
  @status_ids = params[:status_ids]  if params[:status_ids]
  if params[:canonical_id].present? 
    #for ajax
    @image = Image.find_by(canonical_id: params[:canonical_id])
  else
    #ajax params to ransack params
    if params["updated_at"].present? or params["website_id"].present?
      params["q"] = {} 
      params["q"]["s"] = "updated_at desc"
      params["q"]["website_id_eq"] = params["website_id"].to_i if params["website_id"].present?
      params["q"]["updated_at_gteq"] = Time.parse(params["updated_at"])  if params["updated_at"].present?
    end
    #ajax default sort
    @search_cache_key = params["q"]

    #clean up of html based search queries
    if search_params
      search_params["title_cont_all"] = search_params["title_cont_all"].split(" ") if search_params["title_cont_all"].present?
      search_params["descriptions_text_cont_all"] = search_params["descriptions_text_cont_all"].split(" ") if search_params["descriptions_text_cont_all"].present?
      search_params["tags_name_cont_all"] = search_params["tags_name_cont_all"].split(" ")  if search_params["tags_name_cont_all"].present?
    end

    @q = Image.ransack(search_params)

    #tag filtering does not cooperate with ransack but can paginate
    if params[:tag].present? 
      @images = Image.tagged_with(params[:tag]).page(params[:page]) 
    else
      @images = @q.result(distinct: true).page(params[:page]) 
    end

    #tagcloud
    if request.format.html?
      @tags = Rails.cache.fetch('tags', expires_in: 15.minutes) do
        Image.tag_counts_on(:tags)
      end
    end
  end
end

#newObject

GET /images/new



160
161
162
# File 'app/controllers/images_controller.rb', line 160

def new
  @image = Image.new
end

#showObject



150
151
152
153
154
155
156
157
# File 'app/controllers/images_controller.rb', line 150

def show
  @status_ids = [2]
  @status_ids = params[:status_ids]  if params[:status_ids]
  if request.format.html?
    @previous_image = Image.where("id < ?", @image.id).first
    @next_image = Image.where("id > ?", @image.id).first
  end
end

#titlesObject

returns hash of canonical_ids to titles from MCA NOTE deprecated



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'app/controllers/images_controller.rb', line 238

def titles
  canonical_ids = params["canonical_ids"]

  #TODO try to read each image in cache and if not available, then bulk grab
  ids_titles = Rails.cache.fetch(canonical_ids, expires_in: 1.minute) do
    require 'multi_json'
    require 'open-uri'

    ids_titles = {}

    #prep url
    url = "https://mcachicago.org/api/v1/attachment_images/?"
    canonical_ids.each do |i|
      url += "ids[]=" + i + "&"
    end

    #request
    Rails.logger.info "grabbing images json at #{url}"
    begin
      content = open(url, { "Content-Type" => "application/json", ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE, read_timeout: 10}).read
      #parse
      begin
        images_received = JSON.parse(content)

        #match ids, add titles to image cache, and set titles
        canonical_ids.each do |id|
          i = images_received.find{|i| i["id"].to_s == id.to_s}
          #puts i
          if i
            title = Rails.cache.fetch([id, 'title'].hash, expires_in: 1.minute) do
              i["title"]
            end
            ids_titles[id] = title
          end
        end

      rescue Exception => e
        Rails.logger.error "JSON parsing exception"
        Rails.logger.error e
        length = 0
      end

    rescue OpenURI::HTTPError => error
      response = error.io
      Rails.logger.error response.string
      length = 0
    end
    ids_titles
  end

  render :json => ids_titles.to_json
end

#toggleObject



291
292
293
294
# File 'app/controllers/images_controller.rb', line 291

def toggle
  @image.toggle!(params[:column].to_sym)
  render nothing: true
end

#updateObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/controllers/images_controller.rb', line 190

def update
  if @image.update(image_params)
    if request.format.html?
      redirect_to @image, notice: 'Image was successfully updated.'
    else
      render @image
    end
  else
    if request.format.html?
      render :edit
    else
      render :json => { :errors => @image.errors.full_messages }
    end
  end
end