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
79
80
# 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|
        image_title = params[:image][:image_title].presence || auto_title(image.original_filename)
        @images << @image = ::Refinery::Image.create(
          image_params.merge(image_title: image_title, image: image)
        )
      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

#cropObject


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'images/app/controllers/refinery/admin/images_controller.rb', line 115

def crop
  parent_image = ::Refinery::Image.find_by_id(params[:image_id])

  cropped_image = ::Refinery::Image.new(
    parent_id: parent_image.id,
    image: params[:image]
  )

  if cropped_image.valid? && cropped_image.save!
    flash.notice = ::I18n.t('refinery.admin.images.form.crop_success')
    render json: {
      message: ::I18n.t('refinery.admin.images.form.crop_success'),
      crop: render_to_string('/refinery/admin/images/_crop', layout: false, locals: { crop: cropped_image})
    }
  else
    flash.now[:error] = ::I18n.t('refinery.admin.images.form.crop_error')
    render json: { message: ::I18n.t('refinery.admin.images.form.crop_error') }
  end
end

#destroy_cropObject


135
136
137
138
139
140
141
142
143
144
145
146
# File 'images/app/controllers/refinery/admin/images_controller.rb', line 135

def destroy_crop
  @image = Refinery::Image.find_by_id(params[:image_id])
  title = @image.parent.image_name

  if @image.destroy
    flash.notice = t('destroyed', scope: 'refinery.crudify', what: title)

    respond_to do |format|
      format.js { render "/refinery/admin/images/destroy_crop", locals: { image_id: @image.id } }
    end
  end
end

#insertObject

This renders the image insert dialog


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 20

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


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

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

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

#updateObject


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

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_back(fallback_location: { action: 'edit' })
        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