Skip to content

Commit

Permalink
* Added root url route generator for pages
Browse files Browse the repository at this point in the history
* Overrode page_path to use root page urls
* Added page callbacks for route reloading
  • Loading branch information
hulihanapplications committed May 24, 2012
1 parent 1ce24a3 commit b0a8dbb
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 27 deletions.
6 changes: 4 additions & 2 deletions app/assets/stylesheets/menu.css.scss
Expand Up @@ -54,7 +54,9 @@ div#side ul.menu li span.count, div#side div.menu, div#side ul.menu li a.selecte
div#side div.menu {
div.footer {
border-top:1px solid #aaa;
padding-top:5px;margin-top:5px;
padding-top:5px;
margin-top:5px;
text-align:center;
}
}
div#side ul.menu li.selected > ul, div#side ul.menu li.selected > div.menu ul {display:block;}
Expand Down Expand Up @@ -115,7 +117,7 @@ div#side ul.menu {

div#side > div.menu > ul.menu {
> li {
border-bottom:1px solid #ccc;
border-bottom:1px solid #ddd;
border-top:1px solid #fff;
&:first-child { border-top: none;}
&:last-child { border-bottom: none;}
Expand Down
4 changes: 3 additions & 1 deletion app/assets/stylesheets/pages.css.scss
@@ -1,4 +1,7 @@
/* Pages */
.pages {
a.not-displayed-in-menu {font-style:italic}
}
.page {
margin-bottom:10px;
.content {
Expand All @@ -7,7 +10,6 @@
}
div#side > div.page {background:0;border:0;}


/* Blog & Blog Posts */
div#blog-header {display:none}
div.blog {}
Expand Down
15 changes: 13 additions & 2 deletions app/helpers/pages_helper.rb
@@ -1,7 +1,18 @@
module PagesHelper
def link_to_page(page, options = {})
url ||= page_path(page)
raw link_to(page.title, url, :class => options[:class], :title => page.description.present? ? page.description : page.title)
options[:url] ||= page_path(page)
raw link_to(page.title, options[:url], :class => options[:class], :title => page.description.present? ? page.description : page.title)
end

# override default REST helper for page_path
def page_path(page)
page.is_public_page? ? page_root_path(page) : url_for(:action => :show, :controller => :pages, :id => page)
end

# Generate special root path for a page
# example.com/my-page
def page_root_path(page)
"/#{page.slug}"
end

def show_page(page) # prints out page content
Expand Down
13 changes: 9 additions & 4 deletions app/models/page.rb
Expand Up @@ -19,8 +19,10 @@ class Page < ActiveRecord::Base
before_validation(:on => :create) do
self.assign_order_number
end

after_destroy :destroy_everything

after_save :reload_routes
after_destroy :destroy_subpages
after_destroy :reload_routes

attr_protected :user_id
serialize :group_ids, Array
Expand All @@ -43,7 +45,11 @@ def to_s
title
end

def destroy_everything
def reload_routes
Opal::Application.reload_routes!
end

def destroy_subpages
for subpage in self.pages # delete all subpages
if !subpage.deletable # if the subpage is not deletable, move to root
subpage.update_attribute(:page_id, 0)
Expand All @@ -53,7 +59,6 @@ def destroy_everything
end
end


def validate_redirection_url
if self.redirect
validates_format_of :redirect_url, :with => Cregexp.url
Expand Down
5 changes: 2 additions & 3 deletions app/views/application/_main_menu.html.erb
Expand Up @@ -12,13 +12,12 @@
path = {:action => "index", :controller => "items", :id => nil}
selected = params[:controller] == "items"
when "blog"
path = {:action => "index", :controller => "blog"}
path = blog_path
selected = params[:controller] == "blog"
else
path = page_path(page)
path = page_root_path(page)
selected = params[:controller] == "pages" && params[:id] == page.to_param
end

%>
<% subpages = page.pages.published.display_in_menu.in_order %>
<%= content_tag :li, :class => "#{subpages.empty? ? nil : "with-subpages"} #{selected ? "selected" : nil}", :id => "main-menu-item-#{page.title.dasherized}" do -%>
Expand Down
6 changes: 4 additions & 2 deletions app/views/blog/_menu.html.erb
Expand Up @@ -9,8 +9,10 @@
<%= content_tag :ul, :class => :menu do %>
<% @post_archives.each do |date, posts| %>
<%= content_tag :li do %>
<%= link_to("#{date.strftime("%B %Y")} ", blog_archive_path(:year => date.strftime("%Y"), :month => date.strftime("%m"))) %>
<%= content_tag(:span, posts.size, :class => "count") %>
<%= link_to(blog_archive_path(:year => date.strftime("%Y"), :month => date.strftime("%m"))) do %>
<%= date.strftime("%B %Y") %>
<%= content_tag(:span, posts.size, :class => "count") %>
<% end %>
<% end %>
<% end %>
<% end %>
Expand Down
10 changes: 8 additions & 2 deletions app/views/pages/_form.html.erb
Expand Up @@ -127,9 +127,15 @@
<%= icon("redirect") %>
<%= f.label :redirect %>
</div>
<%= content_tag(:div, Page.human_attribute_name(:redirect_description), :class => "description") %>
<%= content_tag(:div, text_field("page", "redirect_url"), :class => "description") %>
<%= content_tag(:div, Page.human_attribute_name(:redirect_description), :class => "description") %>
<%= content_tag(:div, text_field("page", "redirect_url"), :class => "description") %>
</div>
<%= content_tag :div, :class => :field do %>
<div class="label">
<%= f.label :slug %>
</div>
<%= f.text_field :slug %>
<% end if false %>
<% end %>
</td>
</tr>
Expand Down
12 changes: 6 additions & 6 deletions app/views/pages/_menu_for_page.html.erb
@@ -1,16 +1,16 @@
<%
subpages ||= page.pages.published.display_in_menu.in_order
%>
<%= content_tag :div, :class => "section", :id => "page-menu" do %>
<h2 class="title">
<%= link_to_page page %>
</h2>
<div class="menu">
<%= content_for :side do %>
<%= content_tag :div, :class => "menu", :id => "page-menu" do %>
<h2 class="title">
<%= link_to_page page %>
</h2>
<%= content_tag :ul, :class => :menu do %>
<% for subpage in subpages %>
<%= content_tag(:li, link_to_page(subpage)) %>
<%#= descend_page(subpage) %>
<% end %>
<% end %>
</div>
<% end %>
<% end unless subpages.empty? %>
4 changes: 1 addition & 3 deletions app/views/pages/_show.html.erb
Expand Up @@ -30,9 +30,7 @@
<% if options[:checkbox] %>
<input type="checkbox" name=<%= options[:input_name] %>" value="<%=page.id %>" <%= checked_value %> <%= disabled_value %>>
<% end %>
<%= raw "<i>" if !page.display_in_menu %>
<%= link_to(truncate(page.title, :length => options[:truncate_length]), {:action => "page", :controller => "pages", :id => page}, :class => "page_link", :title => page.description) %>
<%= raw "</i>" if !page.display_in_menu %>
<%= link_to(truncate(page.title, :length => options[:truncate_length]), page_path(page), :class => "page_link #{page.display_in_menu ? "displayed-in-menu" : "not-displayed-in-menu"}", :title => page.description) %>
</td>
<td align=right>
<%= friendly_date page.created_at if (options[:show_date] || page.is_blog_post?) %>
Expand Down
15 changes: 13 additions & 2 deletions config/routes.rb
Expand Up @@ -23,6 +23,7 @@
# match "/#{Russian.translit(Item.model_name.human(:count => :other)).downcase}(/:action(/:id(.:format))", :controller => "items" # Russian variant uses transliteration to avoid encoding troubles
# end

match "blog" => "blog#index"
match '/blog/:year(/:month(/:day))',
:controller => 'blog',
:action => 'archive',
Expand Down Expand Up @@ -70,6 +71,10 @@
get "view", :on => :member
end

match "cheese" => "admin#index"
match "cheese" => "browse#index"


resources :settings do
collection do
get "test_email"
Expand All @@ -84,11 +89,17 @@
end
end

# Create cms-like page urls.
# example.com/my-page
# This takes low priority in the case that a page title conflicts with a controller route.
# You can generate this url with the `page_root_path` helper method.
Page.public.all.each do |page|
match page.slug, :controller => 'pages', :action => 'show', :id => page.slug
end

# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
match ':controller(/:action(/:id(.:format)))'


end


0 comments on commit b0a8dbb

Please sign in to comment.