Class: Thoth::PageController

Inherits:
Controller
  • Object
show all
Defined in:
lib/thoth/controller/page.rb

Instance Method Summary collapse

Methods inherited from Controller

action_missing

Instance Method Details

#delete(id = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/thoth/controller/page.rb', line 44

def delete(id = nil)
  require_auth

  error_404 unless id && @page = Page[id]

  if request.post?
    error_403 unless form_token_valid?

    if request[:confirm] == 'yes'
      @page.destroy
      Ramaze::Cache.action.clear
      flash[:success] = 'Page deleted.'
      redirect(MainController.r())
    else
      redirect(@page.url)
    end
  end

  @title          = "Delete Page: #{@page.title}"
  @show_page_edit = true
end

#edit(id = nil) ⇒ Object



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
91
92
93
94
95
96
97
# File 'lib/thoth/controller/page.rb', line 66

def edit(id = nil)
  require_auth

  unless @page = Page[id]
    flash[:error] = 'Invalid page id.'
    redirect(rs(:new))
  end

  if request.post?
    error_403 unless form_token_valid?

    @page.name  = request[:name]
    @page.title = request[:title]
    @page.body  = request[:body]

    if @page.valid? && request[:action] == 'Post'
      begin
        raise unless @page.save
      rescue => e
        @page_error = "There was an error saving your page: #{e}"
      else
        Ramaze::Cache.action.clear
        flash[:success] = 'Page saved.'
        redirect(rs(@page.name))
      end
    end
  end

  @title          = "Edit page - #{@page.title}"
  @form_action    = rs(:edit, id)
  @show_page_edit = true
end

#index(name = nil) ⇒ Object



37
38
39
40
41
42
# File 'lib/thoth/controller/page.rb', line 37

def index(name = nil)
  error_404 unless name && @page = Page[:name => name.strip.downcase]

  @title          = @page.title
  @show_page_edit = true
end

#list(page = 1) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/thoth/controller/page.rb', line 99

def list(page = 1)
  require_auth

  # If this is a POST request, set page display positions.
  if request.post? && !request[:position].nil? &&
      request[:position].is_a?(Hash)

    error_403 unless form_token_valid?

    Page.normalize_positions

    Page.order(:position).all do |p|
      unless request[:position][p.id.to_s].nil? ||
         request[:position][p.id.to_s].to_i == p.position
        Page.set_position(p, request[:position][p.id.to_s].to_i)
      end
    end

    Page.normalize_positions
  end

  page = page.to_i

  @columns  = [:name, :title, :created_at, :updated_at, :position]
  @order    = (request[:order] || :asc).to_sym
  @sort     = (request[:sort]  || :display_order).to_sym
  @sort     = :position unless @columns.include?(@sort)
  @sort_url = rs(:list, page)

  @pages = Page.order(@order == :desc ? Sequel.desc(@sort) : @sort).paginate(page, 20)

  @title        = "Pages (page #{page} of #{[@pages.page_count, 1].max})"
  @pager        = pager(@pages, rs(:list, '__page__', :sort => @sort, :order => @order))
  @form_action  = rs(:list)
end

#newObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/thoth/controller/page.rb', line 135

def new
  require_auth

  @title       = "New page - Untitled"
  @form_action = rs(:new)

  if request.post?
    error_403 unless form_token_valid?

    @page = Page.new do |p|
      p.name     = request[:name]
      p.title    = request[:title]
      p.body     = request[:body]
      p.position = Page.dataset.max(:position).to_i + 1
    end

    if @page.valid? && request[:action] == 'Post'
      begin
        raise unless @page.save
      rescue => e
        @page_error = "There was an error saving your page: #{e}"
      else
        Ramaze::Cache.action.clear
        flash[:success] = 'Page created.'
        redirect(rs(@page.name))
      end
    end

    @title = "New page - #{@page.title}"
  end
end