todo.rb
def sort_lists(lists, &block)
incomplete_lists = {}
complete_lists = {}
lists.each_with_index do |list, index|
if list_complete?(list)
complete_lists[list] = index
else
incomplete_lists[list] = index
end
end
incomplete_lists.each(&block)
complete_lists.each(&block)
end
So here, this is under the helpers block. Code in the helpers block is accessible in erb view template and the main ruby files. "sort_lists" takes a lists object and a block. A lists object is a array that contains the hash object {name: "work" , todos: [] }. So now our objective is to break the lists into those that is incomplete list first, then completed second.
so we iterate through the lists's array, one by one, which yielded the list hash. Then we pass the list hash into the list_complete? method. Which select list hash who has at least one item in the todos array and also those list who has all completed list. Then , we store the list object in a complete_list's hash with the list hash as a key and the index as a value. If not completed, then those are stored in the incomplete_list hash.
Then we yield the incomplete_list hash first to the block , then we yield the second complete_list to the block.
lists.erb
<ul id="lists">
<% sort_lists(@lists) do |list, index| %>
<li class="<%= list_class(list) %>">
<a href="/lists/<%= index %>">
<h2><%= list[:name] %></h2>
<p>
<%= todos_remaining_count(list) %> / <%= todos_count(list) %>
</p>
</a>
</li>
<% end %>
</ul>
The sort_list will pass those list in the incomplete_list hash first to the block , then only the completed list hash into the block.
the sort_list will yield the list one by one. The list_class method will return the string "complete" if the list_complete? returns true. list_complete? will return true if todos array count is more than 0 and todos_remaining_count is zero.
def list_class(list)
"complete" if list_complete?(list)
end
def sort_lists(lists, &block)
incomplete_lists = {}
complete_lists = {}
lists.each_with_index do |list, index|
if list_complete?(list)
complete_lists[list] = index
else
incomplete_lists[list] = index
end
end
incomplete_lists.each(&block)
complete_lists.each(&block)
end
So here, this is under the helpers block. Code in the helpers block is accessible in erb view template and the main ruby files. "sort_lists" takes a lists object and a block. A lists object is a array that contains the hash object {name: "work" , todos: [] }. So now our objective is to break the lists into those that is incomplete list first, then completed second.
so we iterate through the lists's array, one by one, which yielded the list hash. Then we pass the list hash into the list_complete? method. Which select list hash who has at least one item in the todos array and also those list who has all completed list. Then , we store the list object in a complete_list's hash with the list hash as a key and the index as a value. If not completed, then those are stored in the incomplete_list hash.
Then we yield the incomplete_list hash first to the block , then we yield the second complete_list to the block.
lists.erb
<ul id="lists">
<% sort_lists(@lists) do |list, index| %>
<li class="<%= list_class(list) %>">
<a href="/lists/<%= index %>">
<h2><%= list[:name] %></h2>
<p>
<%= todos_remaining_count(list) %> / <%= todos_count(list) %>
</p>
</a>
</li>
<% end %>
</ul>
The sort_list will pass those list in the incomplete_list hash first to the block , then only the completed list hash into the block.
the sort_list will yield the list one by one. The list_class method will return the string "complete" if the list_complete? returns true. list_complete? will return true if todos array count is more than 0 and todos_remaining_count is zero.
def list_class(list)
"complete" if list_complete?(list)
end
def todos_remaining_count(list)
list[:todos].select { |todo| !todo[:completed] }.size
end
todos_remaining_count will take a list hash, then it will access the todos array in the list hash , then select those todo that has a completed = true value. A todo object look like this {name: "work1" , completed: true }
Comments
Post a Comment