Permalink
Browse files

Refactored empty task fields when creating a todo list

  • Loading branch information...
rondy committed Dec 1, 2011
1 parent 5fde939 commit 1daddda325e94269a0f2d4b925e3eece1fa64ae2
@@ -8,15 +8,13 @@ def index
def new
@todo_list = current_user.todo_lists.build
- @todo_list.build_empty_tasks
end
def create
@todo_list = current_user.todo_lists.build(params[:todo_list])
if @todo_list.save
redirect_to todo_lists_url, :notice => "Todo list created"
else
- @todo_list.build_empty_tasks
render "new"
end
end
@@ -4,4 +4,8 @@ def visibility(todo_list)
content_tag :em, "(private)" if todo_list.private?
end
+ def setup_task_fields(todo_list)
+ todo_list.new_record? ? todo_list.tasks_with_empty_items : todo_list.tasks
+ end
+
end
View
@@ -10,12 +10,15 @@ class TodoList < ActiveRecord::Base
scope :public_visibility, where(:private => false)
- def build_empty_tasks(n=4)
- n.times { tasks.build } if tasks.empty?
+ NUMBER_OF_TASK_FIELDS = 4
+
+ def tasks_with_empty_items
+ (NUMBER_OF_TASK_FIELDS - tasks.size).times { tasks.build }
+ tasks
end
def public?
!private?
end
-end
+end
@@ -6,7 +6,7 @@
<div id="tasks">
<h3>Tasks</h3>
- <%= f.fields_for :tasks, @todo_list.tasks do |tasks_form| %>
+ <%= f.fields_for :tasks, setup_task_fields(@todo_list) do |tasks_form| %>
<%= render :partial => "task", :locals => { :f => tasks_form } %>
<% end %>
@@ -2,21 +2,17 @@
describe TodoList do
- it "builds empty tasks (4 by default)" do
+ it "builds empty tasks (#{TodoList::NUMBER_OF_TASK_FIELDS} by default)" do
todo_list = TodoList.new
- todo_list.tasks.should be_empty
- todo_list.build_empty_tasks
- todo_list.tasks.should have(4).tasks
+ todo_list.tasks_with_empty_items.should have(TodoList::NUMBER_OF_TASK_FIELDS).items
todo_list = TodoList.new
- todo_list.tasks.should be_empty
- todo_list.build_empty_tasks(2)
- todo_list.tasks.should have(2).tasks
+ todo_list.tasks.build(:name => "Just one item for while")
+ todo_list.tasks_with_empty_items.should have(TodoList::NUMBER_OF_TASK_FIELDS).items
todo_list = TodoList.new
- todo_list.tasks.build(:name => "only one item for while")
- todo_list.build_empty_tasks
- todo_list.tasks.should have(1).item
+ 10.times { |n| todo_list.tasks.build(:name => "Task #{n}") }
+ todo_list.tasks_with_empty_items.should have(10).items
end
end
@@ -12,15 +12,15 @@
page.should_not have_content todo_list_2.name
end
- scenario "show 4 empty text fields for tasks while user doesn't fill at least one and submit" do
+ scenario "show at least #{TodoList::NUMBER_OF_TASK_FIELDS} text fields for tasks when creating a todo list" do
visit new_todo_list_path
- all("div#tasks > div.task_field input[type=text]").should have(4).items
+ all("div#tasks > div.task_field input[type=text]").should have(TodoList::NUMBER_OF_TASK_FIELDS).items
fill_in "Name", :with => ""
click_button "Create Todo list"
- all("div#tasks > div.task_field input[type=text]").should have(4).items
+ all("div#tasks > div.task_field input[type=text]").should have(TodoList::NUMBER_OF_TASK_FIELDS).items
fill_in(find("div#tasks > div.task_field:nth-child(1) input")[:id], :with => "Can't find a good name to my todo list")
click_button "Create Todo list"
- all("div#tasks > div.task_field input[type=text]").should have(1).items
+ all("div#tasks > div.task_field input[type=text]").should have(TodoList::NUMBER_OF_TASK_FIELDS).items
end
scenario "can create a todo list with valid attributes" do

0 comments on commit 1daddda

Please sign in to comment.