Class: Refinery::Admin::ImagesController

Inherits:
Refinery::AdminController show all
Defined in:
images/app/controllers/refinery/admin/images_controller.rb

Instance Method Summary collapse

Methods included from BaseController

#admin?, included, #refinery_admin_root_path, #searching?

Methods included from Refinery::ApplicationController

#admin?, #current_refinery_user, #error_404, #from_dialog?, #home_page?, included, #local_request?

Instance Method Details

#createObject



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
# File 'images/app/controllers/refinery/admin/images_controller.rb', line 40

def create
  @images = []
  begin
    if params[:image].present? && params[:image][:image].is_a?(Array)
      params[:image][:image].each do |image|
        params[:image][:image_title] = params[:image][:image_title].presence || auto_title(image.original_filename)
        @images << (@image = ::Refinery::Image.create({image: image}.merge(image_params.except(:image).to_h)))
      end
    else
      @images << (@image = ::Refinery::Image.create(image_params))
    end
  rescue NotImplementedError
    logger.warn($!.message)
    @image = ::Refinery::Image.new
  end

  if params[:insert]
    # if all uploaded images are ok redirect page back to dialog, else show current page with error
    if @images.all?(&:valid?)
      @image_id = @image.id if @image.persisted?
      @image = nil
    end

    self.insert
  else
    if @images.all?(&:valid?)
      flash.notice = t('created', scope: 'refinery.crudify', what: "'#{@images.map(&:image_title).join("', '")}'")
      if from_dialog?
        @dialog_successful = true
        render '/refinery/admin/dialog_success', layout: true
      else
        redirect_to refinery.admin_images_path
      end
    else
      self.new # important for dialogs
      render 'new'
    end
  end
end

#insertObject

This renders the image insert dialog



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'images/app/controllers/refinery/admin/images_controller.rb', line 19

def insert
  self.new if @image.nil?

  @url_override = refinery.admin_images_path(request.query_parameters.merge(insert: true))

  if params[:conditions].present?
    extra_condition = params[:conditions].split(',')

    extra_condition[1] = true if extra_condition[1] == "true"
    extra_condition[1] = false if extra_condition[1] == "false"
    extra_condition[1] = nil if extra_condition[1] == "nil"
  end

  find_all_images(({extra_condition[0] => extra_condition[1]} if extra_condition.present?))
  search_all_images if searching?

  paginate_images

  render 'insert'
end

#newObject



12
13
14
15
16
# File 'images/app/controllers/refinery/admin/images_controller.rb', line 12

def new
  @image = ::Refinery::Image.new if @image.nil?

  @url_override = refinery.admin_images_path(dialog: from_dialog?)
end

#updateObject



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
# File 'images/app/controllers/refinery/admin/images_controller.rb', line 80

def update
  @image.attributes = image_params
  if @image.valid? && @image.save
    flash.notice = t('refinery.crudify.updated', what: "'#{@image.title}'")

    if from_dialog?
      self.index
      @dialog_successful = true
      render :index
    else
      if params[:continue_editing] =~ /true|on|1/
        if request.xhr?
          render partial: '/refinery/message'
        else
          redirect_to :back
        end
      else
        redirect_back_or_default refinery.admin_images_path
      end
    end
  else
    @thumbnail = Image.find params[:id]
    if request.xhr?
      render partial: '/refinery/admin/error_messages', locals: {
               object: @image,
               include_object_name: true
             }
    else
      render 'edit'
    end
  end
end