Class: MerbWords::Pages

Inherits:
Application show all
Defined in:
app/controllers/pages.rb

Instance Method Summary collapse

Instance Method Details

#createObject



49
50
51
# File 'app/controllers/pages.rb', line 49

def create
  display(@page = Page.new, :layout => false)
end

#create_postObject



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

def create_post
  params[:page][:categories].collect!{|c| Category[c]} if params[:page][:categories]
  page = Page.new(params[:page])
  if page.save
    session[:mui_message] = url(:merb_ui_message, :title => 'Page created', :tone => 'positive')
  else
    session[:mui_message] = url(:merb_ui_message, :title => 'Unable to create page', :body => mui_list(page.errors), :tone => 'negative')
    session[:mui_window] = slice_url(:create)
  end
  mui_window_redirect
end

#deleteObject



89
90
91
# File 'app/controllers/pages.rb', line 89

def delete
  display(@page = Page.get!(params[:page_id]), :layout => false)
end

#delete_deleteObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/pages.rb', line 93

def delete_delete
  page = Page.get!(params[:page_id])
  page.categories.clear
  page.save
  if page.destroy
    session[:mui_message] = url(:merb_ui_message, :title => 'Page deleted', :tone => 'positive')
  else
    session[:mui_message] = url(:merb_ui_message, :title => 'Unable to delete page', :body => mui_list(page.errors), :tone => 'negative')
    session[:mui_window] = slice_url(:delete)
  end
  redirect(slice_url(:index))
end

#feedObject



44
45
46
47
# File 'app/controllers/pages.rb', line 44

def feed
  only_provides :xml
  display @pages = Page.all(:publish => true) if Page.first
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/pages.rb', line 9

def index
  if Page
    filters = {}
    filters[:publish] = true unless mui_password?
    @order_property = params[:order_property] || MerbWords[:order] || 'created_at'
    if @order_property == 'title'
      filters[:order] = [@order_property.intern.asc]
    else
      filters[:order] = [@order_property.intern.desc]
    end
    if params[:q]
      query = CGI.escape(params[:q])
      if queries = session[:merb_words_queries]
        session[:merb_words_queries] = queries | query.to_a
      else
        session[:merb_words_queries] = query.to_a
      end
      pages_title = Page.all(:title.like => "%#{query}%")
      pages_body = Page.all(:body.like => "%#{query}%")
      @pages = pages_title | pages_body
    elsif @category_id = params[:category_id]
      @pages = Category.get!(@category_id).pages(filters)
    else
      @pages = Page.all(filters)
    end
    @categories = Category.all(:order => [:title.asc])
    @orders = Order.all
    display @pages
  else
    session[:mui_message] = url(:merb_ui_message, :title => 'Create the first page') if mui_password?
    session[:mui_window] = slice_url(:create)
    render
  end
end

#query_deleteObject



106
107
108
109
110
111
112
113
# File 'app/controllers/pages.rb', line 106

def query_delete
  if(session[:merb_words_queries] and params[:query])
    queries = session[:merb_words_queries]
    query = params[:query].to_a
    session[:merb_words_queries] = queries - query
    redirect(slice_url(:index))
  end
end

#readObject



65
66
67
# File 'app/controllers/pages.rb', line 65

def read
  display @page = Page.get!(params[:page_id])
end

#updateObject



69
70
71
72
73
# File 'app/controllers/pages.rb', line 69

def update
  @page = Page.get!(params[:page_id])
  @page.body = escape_xml(@page.body)
  display(@page, :layout => false)
end

#update_putObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/pages.rb', line 75

def update_put
  page = Page.get!(params[:page_id])
  page.categories.clear
  page.save
  params[:page][:categories].collect!{|c| Category[c]} if params[:page][:categories]
  if page.update_attributes(params[:page])
    session[:mui_message] = url(:merb_ui_message, :title => 'Page updated', :tone => 'positive')
  else
    session[:mui_message] = url(:merb_ui_message, :title => 'Unable to update page', :body => mui_list(page.errors), :tone => 'negative')
    session[:mui_window] = slice_url(:create)
  end
  mui_window_redirect
end