Class: Landable::Api::PagesController

Inherits:
Landable::ApiController show all
Defined in:
app/controllers/landable/api/pages_controller.rb

Constant Summary

Constants inherited from Landable::ApiController

Landable::ApiController::API_MEDIA_REGEX

Instance Method Summary collapse

Methods inherited from Landable::ApiController

#api_media

Instance Method Details

#createObject

RESTful methods



10
11
12
13
14
15
16
# File 'app/controllers/landable/api/pages_controller.rb', line 10

def create
  page = Page.new page_params
  page.updated_by_author = current_author
  page.save!

  respond_with page, status: :created, location: page_url(page)
end

#destroyObject



18
19
20
21
22
23
# File 'app/controllers/landable/api/pages_controller.rb', line 18

def destroy
  @page.updated_by_author = current_author
  @page.try(:deactivate)

  respond_with @page
end

#indexObject



25
26
27
28
29
# File 'app/controllers/landable/api/pages_controller.rb', line 25

def index
  search = Landable::PageSearchEngine.new search_params.merge(ids: params[:ids]), limit: 100

  respond_with search.results, meta: search.meta
end

#previewObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/landable/api/pages_controller.rb', line 55

def preview
  page = Page.where(page_id: page_params[:id]).first_or_initialize
  page.attributes = page_params

  # run the validators and render
  content = generate_preview_for(page) if page.valid?

  respond_to do |format|
    format.json do
      render json: {
        page: {
          preview: content,
          errors: page.errors
        }
      }
    end
  end
end

#publishObject

custom methods



49
50
51
52
53
# File 'app/controllers/landable/api/pages_controller.rb', line 49

def publish
  @page.publish! author_id: current_author.id, notes: params[:notes], is_minor: !params[:is_minor].nil?

  respond_with @page
end

#reactivateObject



31
32
33
34
35
# File 'app/controllers/landable/api/pages_controller.rb', line 31

def reactivate
  @page.try(:reactivate)

  respond_with @page
end

#screenshotsObject



74
75
76
77
78
79
# File 'app/controllers/landable/api/pages_controller.rb', line 74

def screenshots
  Landable::ScreenshotService.call Page.find(params[:id])

  # '{}' is valid json, which jquery will accept as a successful response. '' is not.
  render json: {}, status: 202
end

#showObject



37
38
39
# File 'app/controllers/landable/api/pages_controller.rb', line 37

def show
  respond_with @page
end

#updateObject



41
42
43
44
45
46
# File 'app/controllers/landable/api/pages_controller.rb', line 41

def update
  @page.updated_by_author = current_author
  @page.update_attributes!(page_params)

  respond_with @page
end