refactoring

This commit is contained in:
albert
2011-06-12 16:41:23 -04:00
parent 033f0fc266
commit d6e4283cc7
41 changed files with 197 additions and 429 deletions

View File

@@ -37,9 +37,6 @@
placeholder: "ui-state-placeholder"
});
$("ul#sortable").disableSelection();
$("ul#sortable span.delete").click(function(e) {
$(e.target).parent().remove();
});
$("div.pools div.edit form#ordering-form").submit(function(e) {
$.ajax({

View File

@@ -419,10 +419,12 @@ div.dtext {
}
/*** Pools Posts ***/
/*** Pool Elements ***/
div#c-pools-posts {
div#c-pool-elements {
div#a-new {
font-size: 0.8em;
form {
margin-bottom: 1em;
}
@@ -431,6 +433,10 @@ div#c-pools-posts {
margin-left: 1em;
cursor: pointer;
}
h1 {
font-size: $h3_size;
}
}
}
@@ -439,30 +445,41 @@ div#c-pools-posts {
/*** Pools ***/
div#c-pools {
h1 {
font-size: $h2_size;
}
p {
width: 30em;
}
textarea {
height: 10em;
}
}
/*** Pool Orders ***/
div#c-pool-orders {
h1 {
font-size: $h2_size;
}
div#a-edit {
p {
width: 30em;
}
ul.ui-sortable {
list-style-type: none;
span {
margin: 0;
float: right;
cursor: pointer;
}
li {
padding: 0.5em;
}
li.ui-state-default {
margin-bottom: 20px;
width: 180px;
background: none;
}
li.ui-state-placeholder {
margin-bottom: 20px;
width: 180px;
@@ -473,7 +490,6 @@ div#c-pools {
}
}
/*** Comments ***/
div.comments-for-post {
@@ -572,7 +588,7 @@ article.post-preview {
div#c-posts {
div.notice {
font-size: 80%;
font-size: 0.8em;
padding: 1em;
margin-bottom: 1em;
@@ -687,18 +703,6 @@ div#sessions {
width: 30em;
float: left;
}
aside {
width: 20em;
float: left;
li {
display: list-item;
margin-bottom: 0.5em;
list-style-type: square;
margin-left: 1em;
}
}
h1 {
font-size: $h2_size;
@@ -752,7 +756,7 @@ div#c-users {
}
li {
font-size: 0.9em;
font-size: 0.8em;
line-height: 1.5em;
}
@@ -940,7 +944,7 @@ div#note-container {
}
div.note-edit-dialog {
font-size: 70%;
font-size: 0.8em;
}

View File

@@ -1,5 +1,6 @@
class ApplicationController < ActionController::Base
protect_from_forgery
helper :pagination
before_filter :set_current_user
after_filter :reset_current_user
before_filter :initialize_cookies

View File

@@ -1,11 +1,11 @@
class PoolsPostsController < ApplicationController
class PoolElementsController < ApplicationController
respond_to :html, :xml, :json, :js
before_filter :member_only
def create
@pool = Pool.find_by_name(params[:pool_name]) || Pool.find(params[:pool_id])
@post = Post.find(params[:post_id])
@pool.add_post!(@post)
@pool.add!(@post)
append_pool_to_session(@pool)
respond_with(@pool, :location => pool_path(@pool))
end
@@ -13,7 +13,7 @@ class PoolsPostsController < ApplicationController
def destroy
@pool = Pool.find(params[:pool_id])
@post = Post.find(params[:post_id])
@pool.remove_post!(@post)
@pool.remove!(@post)
respond_with(@pool, :location => pool_path(@pool))
end

View File

@@ -0,0 +1,12 @@
class PoolOrdersController < ApplicationController
respond_to :html, :xml, :json, :js
before_filter :member_only
def edit
@pool = Pool.find(params[:pool_id])
respond_with(@pool)
end
def update
end
end

View File

@@ -25,7 +25,9 @@ class PoolsController < ApplicationController
def show
@pool = Pool.find(params[:id])
@post_set = PostSets::Pool.new(@pool, :page => params[:page])
@post_set = PostSets::Base.new(:id => @pool, :page => params[:page])
@post_set.extend(PostSets::Numbered)
@post_set.extend(PostSets::Pool)
respond_with(@pool)
end

View File

@@ -4,7 +4,8 @@ class PostsController < ApplicationController
respond_to :html, :xml, :json
def index
@post_set = PostSets::Post.new(params[:tags], params)
@post_set = PostSets::Base.new(params)
extend_post_set(@post_set)
respond_with(@post_set)
end
@@ -29,6 +30,24 @@ class PostsController < ApplicationController
end
private
def extend_post_set(post_set)
@post_set.extend(PostSets::Post)
if use_sequential_paginator?
@post_set.extend(PostSets::Sequential)
else
@post_set.extend(PostSets::Numbered)
end
end
def use_sequential_paginator?
if params[:page].to_i > 1000
true
else
false
end
end
def save_recent_tags
if params[:tags] || (params[:post] && params[:post][:tags])
tags = Tag.scan_tags(params[:tags] || params[:post][:tags])

View File

@@ -1,8 +1,10 @@
module PaginationHelper
def smart_paginator(set, &block)
if set.page && set.page > 1000
if params[:page] && set.page > 1000
set.extend(PostSets::Sequential)
sequential_paginator(set)
else
set.extend(PostSets::Numbered)
numbered_paginator(set, &block)
end
end
@@ -10,11 +12,11 @@ module PaginationHelper
def sequential_paginator(set)
html = "<menu>"
unless set.first_page?
unless set.is_first_page?
html << '<li>' + link_to("&laquo; Previous", params.merge(:after_id => set.first_id)) + '</li>'
end
unless set.last_page?
unless set.is_last_page?
html << '<li>' + link_to("Next &raquo;", params.merge(:before_id => set.last_id)) + '</li>'
end

View File

@@ -36,6 +36,14 @@ module PostSets
@page ||= params[:page] ? params[:page].to_i : 1
end
def total_pages
(count / limit.to_f).ceil
end
def current_page
page
end
def offset
((page < 1) ? 0 : (page - 1)) * limit
end

View File

@@ -41,6 +41,10 @@ module PostSets
@tag_array ||= ::Tag.scan_query(tag_string)
end
def tags
tag_array
end
def validate
super
validate_query_count

View File

@@ -18,6 +18,22 @@ module PostSets
end
end
def first_id
if posts.any?
posts.first.id
else
nil
end
end
def last_id
if posts.any?
posts.last.id
else
nil
end
end
def pagination_options
{:before_id => before_id, :after_id => after_id}
end

View File

@@ -1,3 +1,5 @@
require 'ostruct'
class Pool < ActiveRecord::Base
validates_uniqueness_of :name
validates_format_of :name, :with => /\A[^\s;,]+\Z/, :on => :create, :message => "cannot have whitespace, commas, or semicolons"

View File

@@ -1,78 +0,0 @@
module Paginators
class Base < Presenter
def sequential_pagination_html(template)
html = "<menu>"
prev_url = template.request.env["HTTP_REFERER"]
next_url = sequential_link(template)
html << %{<li><a href="#{prev_url}">&laquo; Previous</a></li>}
if post_set.posts.any?
html << %{<li><a href="#{next_url}">Next &raquo;</a></li>}
end
html << "</menu>"
html.html_safe
end
def numbered_pagination_html(template)
html = "<menu>"
window = 3
if total_pages <= (window * 2) + 5
1.upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
elsif current_page <= window + 2
1.upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
elsif current_page >= total_pages - (window + 1)
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
else
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
end
html << "</menu>"
html.html_safe
end
protected
def numbered_pagination_item(template, page, current_page)
html = "<li>"
if page == "..."
html << "..."
elsif page == current_page
html << page.to_s
else
html << paginated_link(template, page)
end
html << "</li>"
html.html_safe
end
def total_pages
raise NotImplementedError
end
def current_page
raise NotImplementedError
end
def sequential_link(template)
raise NotImplementedError
end
def paginated_link(template, page)
raise NotImplementedError
end
end
end

View File

@@ -1,22 +0,0 @@
module Paginators
class ForumPost < Base
attr_accessor :forum_posts
def initialize(forum_posts)
@forum_posts = forum_posts
end
protected
def total_pages
forum_posts.total_entries
end
def current_page
forum_posts.current_page
end
def paginated_link(template, page)
template.link_to(page, template.forum_posts_path(:search => template.params[:search], :page => page))
end
end
end

View File

@@ -1,23 +0,0 @@
module Paginators
class ForumTopic < Base
attr_accessor :forum_topic, :forum_posts
def initialize(forum_topic, forum_posts)
@forum_topic = forum_topic
@forum_posts = forum_posts
end
protected
def total_pages
forum_posts.total_pages
end
def current_page
forum_posts.current_page
end
def paginated_link(template, page)
template.link_to(page, template.forum_topic_path(forum_topic, :page => page))
end
end
end

View File

@@ -1,58 +0,0 @@
module Paginators
class Numbered
attr_reader :template, :source
delegate :url, :total_pages, :current_page, :to => :source
def initialize(template, source)
@template = template
@source = source
end
def pagination_html
html = "<menu>"
window = 3
if total_pages <= (window * 2) + 5
1.upto(total_pages) do |page|
html << pagination_item(page, current_page)
end
elsif current_page <= window + 2
1.upto(current_page + window) do |page|
html << pagination_item(page, current_page)
end
html << pagination_item("...", current_page)
html << pagination_item(total_pages, current_page)
elsif current_page >= total_pages - (window + 1)
html << pagination_item(1, current_page)
html << pagination_item("...", current_page)
(current_page - window).upto(total_pages) do |page|
html << pagination_item(page, current_page)
end
else
html << pagination_item(1, current_page)
html << pagination_item("...", current_page)
(current_page - window).upto(current_page + window) do |page|
html << pagination_item(page, current_page)
end
html << pagination_item("...", current_page)
html << pagination_item(total_pages, current_page)
end
html << "</menu>"
html.html_safe
end
protected
def pagination_item(page, current_page)
html = "<li>"
if page == "..."
html << "..."
elsif page == current_page
html << page.to_s
else
html << template.link_to(page, url(template, :page => page))
end
html << "</li>"
html.html_safe
end
end
end

View File

@@ -1,22 +0,0 @@
module Paginators
class Pool < Base
attr_accessor :post_set
def initialize(post_set)
@post_set = post_set
end
protected
def total_pages
(post_set.count.to_f / post_set.limit.to_f).ceil
end
def current_page
[1, post_set.page].max
end
def paginated_link(template, page)
template.link_to(page, template.pool_path(post_set.pool, :page => page))
end
end
end

View File

@@ -1,27 +0,0 @@
module Paginators
class Post < Base
attr_accessor :post_set
def initialize(post_set)
@post_set = post_set
end
protected
def total_pages
(post_set.count.to_f / post_set.limit.to_f).ceil
end
def current_page
[1, post_set.page].max
end
# TODO: this is not compatible with paginating favorites
def sequential_link(template)
template.posts_path(:tags => template.params[:tags], before_id => post_set.posts[-1].id, :page => nil)
end
def paginated_link(template, page)
template.link_to(page, template.posts_path(:tags => template.params[:tags], :page => page))
end
end
end

View File

@@ -1,18 +0,0 @@
module Paginators
class PostVersion < Base
attr_accessor :post_set
def initialize(post_set)
@post_set = post_set
end
def numbered_pagination_html(template)
raise NotImplementedError
end
protected
def sequential_link(template)
template.post_versions_path(:before_time => post_set.posts[-1].last_commented_at, :page => nil)
end
end
end

View File

@@ -1,29 +0,0 @@
module Paginators
class Sequential
attr_reader :template, :source
delegate :url, :to => :source
def initialize(template, source)
@template = template
@source = source
end
def pagination_html
html = "<menu>"
html << '<li>' + template.link_to("&laquo; Previous", prev_url) + '</li>'
if next_url
html << '<li>' + template.link_to("Next &raquo;", next_url) + '</li>'
end
html << "</menu>"
html.html_safe
end
def prev_url
template.request.env["HTTP_REFERER"]
end
def next_url
@next_url ||= url(template)
end
end
end

View File

@@ -38,14 +38,6 @@ class PostSetPresenter < Presenter
end
end
def pagination_html(template)
if post_set.use_sequential_paginator?
Paginators::Post.new(post_set).sequential_pagination_html(template)
else
Paginators::Post.new(post_set).numbered_pagination_html(template)
end
end
def post_previews_html
html = ""

View File

@@ -7,7 +7,6 @@
class TagSetPresenter < Presenter
def initialize(tags)
@tags = tags
fetch_categories
end
def tag_list_html(template, options = {})
@@ -21,17 +20,13 @@ class TagSetPresenter < Presenter
end
private
def fetch_categories
@category_cache ||= Tag.categories_for(@tags)
end
def category_for(tag)
@category_cache[tag]
def categories
@categories ||= Tag.categories_for(@tags)
end
def build_list_item(tag, template, options)
html = ""
html << %{<li data-tag-type="#{category_for(tag)}" data-tag-name="#{u(tag)}">}
html << %{<li data-tag-type="#{categories[tag]}" data-tag-name="#{u(tag)}">}
if CurrentUser.user.is_privileged?
html << %{<a href="/wiki_pages?title=#{u(tag)}">?</a> }

View File

@@ -1,6 +1,6 @@
<div id="c-pools-posts">
<div id="c-pool-elements">
<div id="a-new">
<%= form_tag(pool_post_path) do %>
<%= form_tag(pool_element_path) do %>
<%= hidden_field_tag "post_id", @post.id %>
<%= text_field_tag "pool_name", "", :size => 20 %>
<%= submit_tag "Select" %>

View File

@@ -0,0 +1,20 @@
<div id="c-pool-orders">
<div id="a-edit">
<h1>Order Pool: <%= @pool.name %></h1>
<p>Drag and drop the list below to determine ordering.</p>
<ul id="sortable">
<% @pool.posts(:limit => 1_000).each do |post| %>
<li class="ui-state-default" id="pool[post_id_array]_<%= post.id %>">
<%= link_to(image_tag(post.preview_file_url), post_path(post)) %>
</li>
<% end %>
</ul>
<%= simple_form_for(@pool, :format => :js, :html => {:id => "ordering-form"}) do |f| %>
<%= submit_tag "Save" %>
<% end %>
</div>
</div>
<%= render :partial => "pools/secondary_links" %>

View File

@@ -1,6 +0,0 @@
<%= simple_form_for(@pool) do |f| %>
<h2>Edit Pool: <%= @pool.name %></h2>
<p>If you know the precise ordering of posts that you want, you can just enter them here instead of sorting each post manually. Enter a list of post ids separated by spaces.</p>
<%= f.input :post_ids, :label => "Posts" %>
<%= f.button :submit %>
<% end %>

View File

@@ -1,5 +0,0 @@
<%= simple_form_for(pool) do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.button :submit %>
<% end %>

View File

@@ -7,8 +7,8 @@
<li>|</li>
<li><%= link_to "Show", pool_path(@pool) %></li>
<li><%= link_to "Posts", posts_path(:tags => "pool:#{@pool.id}") %></li>
<li><%= link_to "Simple Edit", edit_pool_path(@pool) %></li>
<li><%= link_to "Advanced Edit", edit_pool_path(@pool, :advanced => true) %></li>
<li><%= link_to "Edit", edit_pool_path(@pool) %></li>
<li><%= link_to "Order", edit_pool_order_path(@pool) %></li>
<% end %>
</menu>
<% end %>

View File

@@ -1,17 +0,0 @@
<h2>Edit Pool: <%= @pool.name %></h2>
<p>Drag and drop the list below to determine ordering.</p>
<ul id="sortable">
<% posts.each do |post| %>
<li class="ui-state-default" id="pool[post_id_array]_<%= post.id %>">
<%= link_to(image_tag(post.preview_file_url), post_path(post)) %>
<span class="ui-icon ui-icon-closethick delete" title="Remove post"></span>
</li>
<% end %>
</ul>
<%= simple_form_for(@pool, :format => :js, :html => {:id => "ordering-form"}) do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= submit_tag "Save" %>
<% end %>

View File

@@ -1,10 +1,12 @@
<div id="c-pools">
<div id="a-edit">
<% if params[:advanced] %>
<%= render "advanced_edit" %>
<% else %>
<%= render :partial => "simple_edit", :locals => {:posts => @pool.posts} %>
<% end %>
<%= simple_form_for(@pool) do |f| %>
<h1>Edit Pool: <%= @pool.name %></h1>
<%= f.input :name %>
<%= f.input :description %>
<%= f.input :post_ids, :label => "Posts" %>
<%= f.button :submit %>
<% end %>
</div>
</div>

View File

@@ -1,9 +1,10 @@
<div id="c-pools">
<div id="a-show">
<aside id="sidebar">
<h2>Pool: <%= @pool.name %></h2>
<p><%= format_text(@pool.description) %></p>
</aside>
<h1>Pool: <%= @pool.name %></h1>
<div id="description">
<%= format_text(@pool.description) %>
</div>
<section id="content">
<%= @post_set.presenter.post_previews_html %>
@@ -11,7 +12,9 @@
<div class="clearfix"></div>
<div class="paginator">
<%= @post_set.presenter.pagination_html(self) %>
<%= numbered_paginator(@post_set) do |page| %>
<%= link_to(page, pool_path(@pool, :page => page)) %>
<% end %>
</div>
</section>
</div>

View File

@@ -22,8 +22,6 @@
</ul>
</section>
<%= render :partial => "posts/partials/index/explore", :locals => {:post_set => @post_set} %>
<section id="tag-box">
<h1>Tags</h1>
<%= @post_set.presenter.tag_list_html(self) %>

View File

@@ -7,7 +7,7 @@
<li>Subscriptions</li>
<li><%= link_to "Changes", post_versions_path %></li>
<li>Approvals</li>
<li><%= link_to "Moderate", post_moderation_moderate_path %></li>
<li><%= link_to "Moderate", moderation_post_dashboard_path %></li>
<li>Help</li>
</menu>
<% end %>

View File

@@ -1,10 +0,0 @@
<% if post_set.date_tag %>
<section>
<h1>Explore</h1>
<ul>
<li>&laquo; Day &raquo;</li>
<li>&laquo; Week &raquo;</li>
<li>&laquo; Month &raquo;</li>
</ul>
</section>
<% end %>

View File

@@ -6,12 +6,12 @@
<li><%= link_to "Flag", new_post_flag_path(:post_id => post.id), :id => "flag" %></li>
<li><%= link_to "Appeal", new_post_appeal_path(:post_id => post.id), :id => "appeal" %></li>
<% if CurrentUser.is_janitor? %>
<li><%= link_to "Approve", post_moderation_approve_path(:post_id => post.id), :remote => true, :method => :put, :id => "approve" %></li>
<li><%= link_to "Disapprove", post_moderation_disapprove_path(:post_id => post.id), :remote => true, :method => :put, :id => "disapprove" %></li>
<li><%= link_to "Approve", moderation_post_approval_path(:post_id => post.id), :remote => true, :method => :post, :id => "approve" %></li>
<li><%= link_to "Disapprove", moderation_post_approval_path(:post_id => post.id), :remote => true, :method => :destroy, :id => "disapprove" %></li>
<% end %>
<% if CurrentUser.is_moderator? %>
<li><%= link_to "Undelete", post_moderation_undelete_path(:post_id => post.id), :remote => true, :method => :post, :id => "undelete" %></li>
<li><%= link_to "Delete", post_moderation_delete_path(:post_id => post.id), :remote => true, :method => :post, :id => "delete" %></li>
<li><%= link_to "Undelete", moderation_post_deletion_path(:post_id => post.id), :remote => true, :method => :destroy, :id => "undelete" %></li>
<li><%= link_to "Delete", moderation_post_deletion_path(:post_id => post.id), :remote => true, :method => :post, :id => "delete" %></li>
<% end %>
<li><%= link_to "Pool", "#", :id => "pool" %></li>
</ul>

View File

@@ -1,13 +1,13 @@
<ul>
<% post.pools.each do |pool| %>
<li>
<% if pool.neighbor_posts(post)[:previous] %>
<%= link_to "&laquo;".html_safe, post_path(pool.neighbor_posts(post)[:previous]) %>
<% if pool.neighbors(post).previous %>
<%= link_to "&laquo;".html_safe, post_path(pool.neighbors(post).previous) %>
<% else %>
&laquo;
<% end %>
<% if pool.neighbor_posts(post)[:next] %>
<%= link_to "&raquo;".html_safe, post_path(pool.neighbor_posts(post)[:next]) %>
<% if pool.neighbors(post).next %>
<%= link_to "&raquo;".html_safe, post_path(pool.neighbors(post).next) %>
<% else %>
&raquo;
<% end %>

View File

@@ -57,6 +57,10 @@
</section>
<section id="notes">
<% if @post.notes.active.empty? %>
<p>There are no notes.</p>
<% end %>
<%= render :partial => "notes/note", :collection => @post.notes.active %>
</section>
@@ -75,7 +79,7 @@
</div>
<div id="add-to-pool-dialog" title="Add to Pool">
<%= render :template => "pools_posts/new" %>
<%= render :template => "pool_elements/new" %>
</div>
</div>

View File

@@ -19,19 +19,18 @@
<%= submit_tag "Submit" %>
</div>
<% end %>
</section>
<aside>
<h1>Help</h1>
<ul>
<li><%= link_to "I don't have an account", new_user_path %></li>
<li><%= link_to "I forgot my password", reset_password_path %></li>
<li><%= link_to "I forgot my user name", login_reminder_path %></li>
</ul>
</aside>
</section>
</div>
</div>
<% content_for(:page_title) do %>
/login
<% end %>
<% content_for(:secondary_links) do %>
<menu>
<li><%= link_to "Signup", new_user_path %></li>
<li><%= link_to "Reset Password", new_maintenance_user_password_reset_path %></li>
<li><%= link_to "Login Reminder", new_maintenance_user_login_reminder_path %></li>
</menu>
<% end %>

View File

@@ -1 +1,3 @@
<h1>Namasté <%= CurrentUser.pretty_name %>!</h1>
<%= render :partial => "secondary_links" %>