Class: Thesis::ThesisController

Inherits:
ApplicationController
  • Object
show all
Includes:
ControllerHelpers
Defined in:
lib/thesis/controllers/thesis_controller.rb

Instance Method Summary collapse

Methods included from ControllerHelpers

#current_page, #current_slug, #root_pages, #thesis_editor

Instance Method Details

#create_pageObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/thesis/controllers/thesis_controller.rb', line 17

def create_page
  page = Page.new
  return head :forbidden unless page_is_editable?(page)

  update_page_attributes page
  if params[:parent_slug].present?
    parent_slug = params[:parent_slug].to_s.sub(/(\/)+$/,'')
    parent = Page.where(slug: parent_slug).first
    page.parent = parent
  end

  resp = { page: page }

  page.update_slug
  if page.save
    resp[:page] = page
  else
    resp[:message] = page.errors.messages.first
  end

  render json: resp, status: page.valid? ? :ok : :not_acceptable
end

#delete_pageObject



40
41
42
43
44
45
46
# File 'lib/thesis/controllers/thesis_controller.rb', line 40

def delete_page
  slug = params[:slug].to_s.sub(/(\/)+$/,'')
  page = Page.where(slug: slug).first
  return head :forbidden unless page && page_is_editable?(page)

  head page.destroy ? :ok : :not_acceptable
end

#page_attributesObject



57
58
59
# File 'lib/thesis/controllers/thesis_controller.rb', line 57

def page_attributes
  [ :name, :title, :description, :parent_id ]
end

#page_is_editable?(page) ⇒ Boolean

The ApplicationController should implement this.

Returns:

  • (Boolean)

Raises:



93
94
95
96
# File 'lib/thesis/controllers/thesis_controller.rb', line 93

def page_is_editable?(page)
  raise RequiredMethodNotImplemented.new("Add a `page_is_editable?(page)` method to your controller that returns true or false.") unless defined?(super)
  super
end

#showObject

Raises:

  • (ActionController::RoutingError)


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/thesis/controllers/thesis_controller.rb', line 5

def show
  raise ActionController::RoutingError.new('Not Found') unless current_page

  if current_page.template && template_exists?("page_templates/#{current_page.template}")
    render "page_templates/#{current_page.template}", layout: false
  elsif template_exists?("page_templates/default")
    render "page_templates/default"
  else
    raise PageRequiresTemplate.new("No default template found in page_templates. Create page_templates/default.html.(erb|haml|slim).")
  end
end

#update_pageObject



48
49
50
51
52
53
54
55
# File 'lib/thesis/controllers/thesis_controller.rb', line 48

def update_page
  page = current_page
  return head :forbidden unless page_is_editable?(page)

  update_page_attributes page

  head page.save ? :ok : :not_acceptable
end

#update_page_attributes(page) ⇒ Object



61
62
63
64
# File 'lib/thesis/controllers/thesis_controller.rb', line 61

def update_page_attributes(page)
  page_attributes.each { |a| page.send("#{a}=", params[a].to_s) if params[a] }
  page
end

#update_page_contentObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/thesis/controllers/thesis_controller.rb', line 66

def update_page_content
  errors = false
  error_message = "Unknown error."

  page_contents = PageContent.where(id: params.keys).includes(:page).all
  if page_contents.length.zero?
    error_message = "That page doesn't exist anymore."
    errors = true
  else
    page_contents.each do |pc|
      if page_is_editable? pc.page
        pc.content = params[pc.id.to_s].to_s.presence || " ".html_safe
        pc.save
      else
        errors = true
        error_message = "You don't have permission to update this page."
      end
    end
  end

  resp = {}
  resp[:message] = error_message if errors

  render json: resp, status: errors ? :not_acceptable : :ok
end