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, #root_pages, #thesis_editor

Instance Method Details

#create_pageObject



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

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 = {}

  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



37
38
39
40
41
42
43
# File 'lib/thesis/controllers/thesis_controller.rb', line 37

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



54
55
56
# File 'lib/thesis/controllers/thesis_controller.rb', line 54

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

#page_is_editable?(page) ⇒ Boolean

The ApplicationController should implement this.

Returns:

  • (Boolean)

Raises:



90
91
92
93
# File 'lib/thesis/controllers/thesis_controller.rb', line 90

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
# 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
  else
    raise PageRequiresTemplate.new("Page requires a template but none was specified.")
  end
end

#update_pageObject



45
46
47
48
49
50
51
52
# File 'lib/thesis/controllers/thesis_controller.rb', line 45

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



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

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

#update_page_contentObject



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

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]
        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