Class: Redactor3Rails::ImagesController

Inherits:
Object
  • Object
show all
Defined in:
app/controller/redactor3_rails/images_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controller/redactor3_rails/images_controller.rb', line 5

def create
  json = {}
  error = nil
  params[:file].each_with_index do |file, i|
    @image = Redactor3Rails.image_model.new
    @image.data = Redactor3Rails::Http.normalize_param(file, request)
    if @image.has_attribute?(:"#{Redactor3Rails.devise_user_key}")
      @image.send("#{Redactor3Rails.devise_user}=", redactor3_current_user)
      @image.assetable = redactor3_current_user
    end
    if @image.save
      json["file-#{i}"] = { id: @image.id, url: @image.url(:content) }
    else
      error = @image.errors
    end
  end
  if json.empty?
    json = { error: true, message: error.full_messages.to_sentence }
  end
  render json: json
end

#indexObject



27
28
29
30
31
32
33
34
35
36
# File 'app/controller/redactor3_rails/images_controller.rb', line 27

def index
  json = Redactor3Rails.image_model.where(user_id: redactor3_current_user.id).order('id DESC').map do |image|
    {
      thumb: image.data.thumb.url,
      url: image.url(:content),
      id: image.id
    }
  end
  render json: json
end