Permalink
Browse files

Refactored todo list view. todo lists, public todo lists and wachted …

…lists render from inherited template
  • Loading branch information...
1 parent 58bcfc0 commit 269387addad0e67c76724b90dd76c184dc650e38 @rondy committed Dec 2, 2011
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -18,10 +18,11 @@ div.todo_list {
a {
background-repeat: no-repeat;
- padding-left: 1.4em;
+ padding-left: 1.6em;
- &.edit { background-image: url('pencil.png'); }
- &.destroy { background-image: url('trash.gif'); }
+ &.edit { background-image: url("pencil.png"); }
+ &.destroy { background-image: url("trash.gif"); }
+ &.watching { background-image: url("watching.png"); }
}
}
}
@@ -1,4 +1,4 @@
-class PublicTodoListsController < ApplicationController
+class PublicTodoListsController < TodoListsController
def index
@public_todo_lists = TodoList.public_visibility
@@ -1,4 +1,4 @@
-class WatchedListsController < ApplicationController
+class WatchedListsController < PublicTodoListsController
def index
@watched_lists = current_user.watched_lists
@@ -1,2 +0,0 @@
-module PublicTodoListsHelper
-end
@@ -8,4 +8,60 @@ def setup_task_fields(todo_list)
todo_list.new_record? ? todo_list.tasks_with_empty_items : todo_list.tasks
end
+ def render_action_links_for(todo_list)
+ if current_user.owns?(todo_list)
+ links_to_manage_todo_list(todo_list)
+ else
+ link_to_toggle_watching(todo_list)
+ end
+ end
+
+ def links_to_manage_todo_list(todo_list)
+ [link_to_edit_todo_list(todo_list), link_to_destroy_todo_list(todo_list)].join(" | ").html_safe
+ end
+
+ def link_to_edit_todo_list(todo_list)
+ link_to "(Edit)",
+ edit_todo_list_path(todo_list),
+ :id => "edit_todo_list_#{todo_list.id}",
+ :class => "edit"
+ end
+
+ def link_to_destroy_todo_list(todo_list)
+ link_to "(Remove)",
+ todo_list_path(todo_list),
+ :method => :delete,
+ :confirm => "Are you sure?",
+ :id => "remove_todo_list_#{todo_list.id}",
+ :class => "destroy"
+ end
+
+ def link_to_toggle_watching(todo_list)
+ content_tag :span, :class => "toggle_watching" do
+ if current_user.can_watch? todo_list
+ if current_user.watching? todo_list
+ link_to_unwatch todo_list
+ else
+ link_to_watch todo_list
+ end
+ end
+ end
+ end
+
+ def link_to_unwatch(todo_list)
+ link_to "Unwatch",
+ watched_list_path(todo_list),
+ :method => :delete,
+ :id => "unwatch_todo_list_#{todo_list.id}",
+ :class => "watching"
+ end
+
+ def link_to_watch(todo_list)
+ link_to "Watch",
+ watched_lists_path(:todo_list_id => todo_list),
+ :method => :post,
+ :id => "watch_todo_list_#{todo_list.id}",
+ :class => "watching"
+ end
+
end
@@ -1,23 +0,0 @@
-module WatchedListsHelper
-
- def toggle_watching(todo_list)
- content_tag :span, :class => "toggle_watching" do
- if current_user.can_watch? todo_list
- if current_user.watching? todo_list
- link_to_unwatch todo_list
- else
- link_to_watch todo_list
- end
- end
- end
- end
-
- def link_to_unwatch(todo_list)
- link_to "Unwatch", watched_list_path(todo_list), :method => :delete, :id => "unwatch_todo_list_#{todo_list.id}"
- end
-
- def link_to_watch(todo_list)
- link_to "Watch", watched_lists_path(:todo_list_id => todo_list), :method => :post, :id => "watch_todo_list_#{todo_list.id}"
- end
-
-end
@@ -7,7 +7,11 @@ def watch!(todo_list)
end
def can_watch?(todo_list)
- todo_list.public? && !todo_lists.include?(todo_list)
+ todo_list.public? && !owns?(todo_list)
+ end
+
+ def owns?(todo_list)
+ self.id == todo_list.user_id
end
def unwatch!(todo_list)
@@ -1,15 +0,0 @@
-<div id="<%= dom_id(public_todo_list) %>" class="todo_list">
- <div class="name">
- <%= public_todo_list.name %> <%= toggle_watching public_todo_list %>
- </div>
-
- <% unless public_todo_list.tasks.empty? %>
- <ul class="tasks">
- <% public_todo_list.tasks.each do |task| %>
- <li><%= task.name %></li>
- <% end %>
- </ul>
- <% end %>
-</div>
-
-<hr/>
@@ -1,5 +1,5 @@
<% title "Public todo lists", :show => false %>
<div id="todo_lists">
- <%= render :partial => "public_todo_list", :collection => @public_todo_lists %>
+ <%= render :partial => "todo_list", :collection => @public_todo_lists %>
</div>
@@ -13,8 +13,7 @@
<h3><%= todo_list.name %></h3>
<div class="actions">
<span class="timestamp"><%= time_ago_in_words(todo_list.created_at) %></span>
- <%= link_to "(Edit)", edit_todo_list_path(todo_list), :id => "edit_todo_list_#{todo_list.id}", :class => "edit", %> |
- <%= link_to "(Remove)", todo_list_path(todo_list), :method => :delete, :confirm => "Are you sure?", :id => "remove_todo_list_#{todo_list.id}", :class => "destroy" %>
+ <%= render_action_links_for(todo_list) %>
</div>
</div>
</div>
@@ -1,15 +0,0 @@
-<div class="todo_list">
- <div>
- <%= watched_list.name %> <%= toggle_watching watched_list %>
- </div>
-
- <% unless watched_list.tasks.empty? %>
- <ul class="tasks">
- <% watched_list.tasks.each do |task| %>
- <li><%= task.name %></li>
- <% end %>
- </ul>
- <% end %>
-</div>
-
-<hr/>
@@ -1,5 +1,5 @@
<% title "Watched lists", :show => false %>
<div id="todo_lists">
- <%= render :partial => "watched_list", :collection => @watched_lists %>
+ <%= render :partial => "todo_list", :collection => @watched_lists %>
</div>
@@ -2,26 +2,133 @@
describe TodoListsHelper do
- describe "visibility" do
+ let(:current_user) { Factory(:user) }
+ let(:todo_list) { Factory.create(:todo_list) }
+
+ before { helper.stub :current_user => current_user }
+
+ describe "#visibility(todo_list)" do
subject { helper.visibility(todo_list) }
context "when todo list is private" do
- let(:todo_list) { stub(:todo_list, :private? => true) }
+ before { todo_list.stub(:private? => true) }
it { should eq "<em>(private)</em>" }
end
context "when todo_list is not private" do
- let(:todo_list) { stub(:todo_list, :private? => false) }
+ before { todo_list.stub(:private? => false) }
it { should be_nil }
end
end
+ describe "#link_to_edit_todo_list(todo_list)" do
+
+ it "renders link to edit a todo list" do
+ helper.link_to_edit_todo_list(todo_list).should eq %{<a href="/todo_lists/#{todo_list.id}/edit" class="edit" id="edit_todo_list_#{todo_list.id}">(Edit)</a>}
+ end
+
+ end
+
+ describe "#link_to_destroy_todo_list(todo_list)" do
+
+ it "renders link to destroy a todo list" do
+ helper.link_to_destroy_todo_list(todo_list).should eq %{<a href="/todo_lists/#{todo_list.id}" class="destroy" data-confirm="Are you sure?" data-method="delete" id="remove_todo_list_#{todo_list.id}" rel="nofollow">(Remove)</a>}
+ end
+
+ end
+
+ describe "#links_manage_todo_list(todo_list)" do
+
+ before do
+ helper.stub(:link_to_edit_todo_list).with(todo_list).and_return("link to edit list")
+ helper.stub(:link_to_destroy_todo_list).with(todo_list).and_return("link to destroy list")
+ end
+
+ it do
+ helper.links_to_manage_todo_list(todo_list).should eq "link to edit list | link to destroy list"
+ end
+
+ end
+
+ describe "#render_action_links_for(todo_list)" do
+
+ context "when user owns todo list" do
+
+ before { current_user.should_receive(:owns?).with(todo_list).and_return(true) }
+
+ it "renders links to manage todo list" do
+ helper.should_receive(:links_to_manage_todo_list).with(todo_list)
+ helper.render_action_links_for(todo_list)
+ end
+
+ end
+
+ context "when user doesn't own todo list" do
+
+ before { current_user.should_receive(:owns?).with(todo_list).and_return(false) }
+
+ it "renders link to toggle watching" do
+ helper.should_receive(:link_to_toggle_watching).with(todo_list)
+ helper.render_action_links_for(todo_list)
+ end
+
+ end
+
+ end
+
+ describe "#link_to_unwatch(todo_list)" do
+
+ it "renders link to unwatch a todo list" do
+ helper.link_to_unwatch(todo_list).should eq %{<a href="/watched_lists/#{todo_list.to_param}" class="watching" data-method="delete" id="unwatch_todo_list_#{todo_list.id}" rel="nofollow">Unwatch</a>}
+ end
+
+ end
+
+ describe "#link_to_watch(todo_list)" do
+
+ it "renders link to watch a todo list" do
+ helper.link_to_watch(todo_list).should eq %{<a href="/watched_lists?todo_list_id=#{todo_list.to_param}" class="watching" data-method="post" id="watch_todo_list_#{todo_list.id}" rel="nofollow">Watch</a>}
+ end
+
+ end
+
+ describe "#link_to_toggle_watching(todo_list)" do
+
+ before do
+ current_user.stub(:can_watch?).with(todo_list).and_return(true)
+ current_user.stub(:watching?).with(todo_list).and_return(is_user_watching_todo_list)
+ end
+
+ context "when user isn't watching todo list" do
+
+ let(:is_user_watching_todo_list) { true }
+
+ it "renders link to unwatch a todo list" do
+ helper.should_receive(:link_to_unwatch).with(todo_list)
+ helper.link_to_toggle_watching(todo_list)
+ end
+
+ end
+
+ context "when user is watching todo list" do
+
+ let(:is_user_watching_todo_list) { false }
+
+ it "renders link to watch a todo list" do
+ helper.should_receive(:link_to_watch).with(todo_list)
+ helper.link_to_toggle_watching(todo_list)
+ end
+
+ end
+
+ end
+
end
@@ -1,49 +0,0 @@
-require "spec_helper"
-
-describe WatchedListsHelper do
-
- let(:user) { Factory(:user) }
- let(:todo_list) { Factory.create(:todo_list) }
-
- before { helper.stub :current_user => user }
-
- it "renders link to unwatch a todo list" do
- helper.link_to_unwatch(todo_list).should eq %{<a href="/watched_lists/#{todo_list.to_param}" data-method="delete" id="unwatch_todo_list_#{todo_list.id}" rel="nofollow">Unwatch</a>}
- end
-
- it "renders link to watch a todo list" do
- helper.link_to_watch(todo_list).should eq %{<a href="/watched_lists?todo_list_id=#{todo_list.to_param}" data-method="post" id="watch_todo_list_#{todo_list.id}" rel="nofollow">Watch</a>}
- end
-
- describe "toggle watching links" do
-
- before do
- user.stub(:can_watch?).with(todo_list).and_return(true)
- user.stub(:watching?).with(todo_list).and_return(is_user_watching_todo_list)
- end
-
- context "when user isn't watching todo list" do
-
- let(:is_user_watching_todo_list) { true }
-
- it "renders link to unwatch a todo list" do
- helper.should_receive(:link_to_unwatch).with(todo_list)
- helper.toggle_watching(todo_list)
- end
-
- end
-
- context "when user is watching todo list" do
-
- let(:is_user_watching_todo_list) { false }
-
- it "renders link to watch a todo list" do
- helper.should_receive(:link_to_watch).with(todo_list)
- helper.toggle_watching(todo_list)
- end
-
- end
-
- end
-
-end
Oops, something went wrong. Retry.

0 comments on commit 269387a

Please sign in to comment.