Class: Api::FoldersController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/api/folders_controller.rb

Overview

Controller to access the Folder model.

Instance Method Summary collapse

Instance Method Details

#createObject

Create a new folder with the title passed in params, and add to it the folder passed in params


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/api/folders_controller.rb', line 70

def create
  @feed = current_user.feeds.find folder_params[:feed_id]
  @folder = current_user.move_feed_to_folder @feed, folder_title: folder_params[:title]
  if @folder.present?
    respond_with @folder
  else
    Rails.logger.error "Could not create folder #{folder_params[:title]} for user #{current_user.id}, returning a 404"
    head 404
  end

rescue => e
  handle_error e
end

#indexObject

Return JSON with the list of folders owned by the current user


15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/api/folders_controller.rb', line 15

def index
  # If folders have not changed, return a 304
  if stale? etag: EtagCalculator.etag(current_user.folders_updated_at),
            last_modified: current_user.folders_updated_at
    @folders = current_user.folders
    respond_with @folders
  end
rescue => e
  handle_error e
end

#showObject

Return a JSON document describing a folder, which must be owned by the current user.

If the requests asks for a folder that does not belong to the current user, the response is a 404 error code (Not Found).


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/api/folders_controller.rb', line 32

def show
  @folder = current_user.folders.find params[:id]

  if @folder.present?
    # If folder has not changed, return a 304
    if stale? etag: EtagCalculator.etag(@folder.updated_at),
              last_modified: @folder.updated_at
      respond_with @folder
    end
  else
    Rails.logger.info "Folder #{params[:id]} not found, returning a 404"
    head 404
  end
rescue => e
  handle_error e
end

#updateObject

Associate a feed with a folder. The current user must own the folder and be subscribed to the feed.


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/api/folders_controller.rb', line 52

def update
  @feed = current_user.feeds.find folder_params[:feed_id]
  if params[:id] != Folder::NO_FOLDER
    @folder = current_user.folders.find params[:id]
  else
    @folder = Folder::NO_FOLDER
  end

  current_user.move_feed_to_folder @feed, folder: @folder
  head :ok
rescue => e
  handle_error e
end