Permalink
Browse files

Refactoring, page sorting links work without js

  • Loading branch information...
1 parent fae9f60 commit 5f4c8f6768ec1f441344824d4671de63bc702425 @markoa committed Jan 13, 2009
@@ -9,7 +9,11 @@ def index
@pages.sort! { |a, b| b.last_updated_at <=> a.last_updated_at }
respond_to do |format|
- format.html # index.html.erb
+ format.html do
+ render :partial => 'list',
+ :locals => { :sort => 'date' },
+ :layout => true
+ end
format.xml { render :xml => @pages }
end
end
@@ -96,12 +100,15 @@ def destroy
end
end
- def sorted_by_date
+ # GET /pages/by_date
+ # GET /pages/by_date.xml
+ # GET /pages/by_date.js
+ def by_date
@pages = Page.find(:all)
@pages.sort! { |a, b| b.last_updated_at <=> a.last_updated_at }
respond_to do |format|
- format.html { redirect_to :action => 'index' } #TODO: actually render smt
+ format.html { redirect_to :action => 'index' }
format.xml { render :xml => @pages }
format.js do
render :update do |page|
@@ -111,16 +118,25 @@ def sorted_by_date
end
end
- def sorted_by_name
+ # GET /pages/by_name
+ # GET /pages/by_name.xml
+ # GET /pages/by_name.js
+ def by_name
@pages = Page.find(:all)
@pages.sort! { |a, b| a.name <=> b.name }
respond_to do |format|
- format.html { redirect_to :action => 'index' } #TODO: actually render smt
+ format.html do
+ render :partial => 'list',
+ :locals => { :sort => 'name' },
+ :layout => true
+ end
+
format.xml { render :xml => @pages }
+
format.js do
render :update do |page|
- page.replace_html 'pageList', :partial => 'list_dump'
+ page.replace_html 'pageList', :partial => 'dump'
end
end
end
@@ -0,0 +1,26 @@
+<% # local: sort [date, name] -%>
+
+<script type='text/javascript'>
+ document.observe('dom:loaded', function() {
+ remote_get_link_to(<%= "'#{by_date_pages_path}'" %>, 'sortDate');
+ remote_get_link_to(<%= "'#{by_name_pages_path}'" %>, 'sortName');
+ });
+</script>
+
+<h1>Listing pages</h1>
+
+<div id="pageList">
+ <% if sort == 'date' -%>
+ <%= render :partial => 'date_separated' %>
+ <% else -%>
+ <%= render :partial => 'dump' %>
+ <% end -%>
+</div>
+
+<br />
+
+<%= link_to 'New page', new_page_path %> |
+Sort by
+<%= link_to 'Date', by_date_pages_path, :id => 'sortDate' %> or
+<%= link_to 'Name', by_name_pages_path, :id => 'sortName' %> |
+<%= link_to 'Home', root_path %>
@@ -1,23 +0,0 @@
-<script type='text/javascript'>
- document.observe('dom:loaded', function() {
- $('sortDate').href = '#';
- $('sortName').href = '#';
-
- remote_get_link_to(<%= "'#{sorted_by_date_pages_url}'" %>, 'sortDate');
- remote_get_link_to(<%= "'#{sorted_by_name_pages_url}'" %>, 'sortName');
- });
-</script>
-
-<h1>Listing pages</h1>
-
-<div id="pageList">
- <%= render :partial => 'date_separated' %>
-</div>
-
-<br />
-
-<%= link_to 'New page', new_page_path %> |
-Sort by
-<%= link_to 'Date', sorted_by_date_pages_path, :id => 'sortDate', :class => 'remote' %> or
-<%= link_to 'Name', sorted_by_name_pages_path, :id => 'sortName', :class => 'remote' %> |
-<%= link_to 'Home', root_path %>
@@ -2,6 +2,7 @@
<%= wikify @page.content %>
+<small> Last updated at <%= @page.last_updated_at.to_s(:long) %> </small> <br />
<%= link_to 'Edit', edit_page_path(@page) %> |
<%= link_to "#{suffixed_number(@page.revisions.size)} revision", page_revisions_path(@page) %> |
<%= link_to 'Delete', @page, :confirm => 'Are you sure? This will delete all revisions.', :method => :delete %> |
View
@@ -10,8 +10,8 @@
map.resources :registration_codes
map.resources :pages, :has_many => :revisions, :collection => {
- :sorted_by_date => :get,
- :sorted_by_name => :get
+ :by_date => :get,
+ :by_name => :get
}
# The priority is based upon order of creation: first created -> highest priority.
@@ -1,16 +1,17 @@
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
-function authToken() {
+// gets authenticity token which is expected to be embedded in the page layout
+function getAuthToken() {
return 'authenticity_token=' + encodeURIComponent(AUTH_TOKEN);
}
function remote_get_link_to(ajaxPath, linkId) {
+ $(linkId).href = '#';
Event.observe(linkId, 'click', function() {
- var link = new Element(linkId);
new Ajax.Request(ajaxPath,
{ asynchronous: true, evalScripts: true, method: 'get',
- parameters: authToken()
+ parameters: getAuthToken()
});
return false;
});

0 comments on commit 5f4c8f6

Please sign in to comment.