More work on todo app

Today I worked some more on the to do list app. I wanted to add a default value to the :done attribute so that any time a new task is created it is flagged as not yet completed. I liked the logic of doing this rather than leaving it as nil, but also I wanted to use the value to use in my model.

class AddDefaultToDoneAttribute < ActiveRecord::Migration
  def change
    change_column_default(:tasks, :done, false)
  end
end

With this migration set, each new record created will return :done => false. After this, I added a pending? method to the model to check if there exist any created tasks that have :done set to false.

class Task < ActiveRecord::Base
  validates :title, presence: true

  def self.pending?
    self.where(:done => false).any?
  end
end

Now, if i utilize this, I can get a boolean response to the question if there are any pending tasks. Finally, I utilized this method in order to display a table pending tasks or a message informing the user that there are no pending tasks.

<% if @tasks.pending? %>
  <table>
    <tr>
      <th>Task</th>
      <th>Notes</th>
      <th class = "donebtn" >Done?</th>
    </tr>
    <% @tasks.each do |t| %>
        <% if t.done == false %>
            <tr>
              <td class = "tasknote"><%= t.title %></td>
              <td class = "tasknote"><%= t.note %></td>
              <td class = "donebtn">
                <%= link_to "Done", complete_tasks_path(:id => t.id), class: "link" %>
              </td>
            </tr>
        <% end %>
    <% end %>
  </table>
<% else %>
  <p> There are no pending tasks! </p>
<% end %>

I have much more to do but I like where this is going so far. Iā€™m going to add a user model next along with more refactoring where necessary.

Written on December 16, 2014