1
votes

My app has several project/task scheduling features. I created a simple demo with the Ice_cube gem first and it works great for scheduling recurring events...but I'm concerned about doing individual day-of-week scopes/actions; so, I created a schedule form with 7 boolean checkboxes.

See this post for an example > weekday events. I couldn't get the solution recommended to work, but my requirements are slightly different.

Instead, I wrote some code that works --- but its very clunky/ugly. And I'm looking for ideas on how to writing a method that allows me: assign calendar dates for each weekday for the user selected date range.

Index view:

<h1>Weekly Project Schedule</h1>
<br />

<h2>Monday: <%= Date.today.monday %></h2>
<% @tasks.each do |task| %>
    <% if task.monday? %>
        <h3><%= link_to task.name, task %></h3>
        <p>Starts: <%= task.starts_on %></p>
        <p>Ends: <%= task.ends_on %></p>
        <p><%= link_to 'Edit', edit_task_path(task) %></p>      
    <% end %>

<% end %>

<h2>Tuesday:  <%= Date.today.monday+1 %></h2>
<% @tasks.each do |task| %>
    <% if task.tuesday? %>
        <h3><%= link_to task.name, task %></h3>
        <p>Starts: <%= task.starts_on %></p>
        <p>Ends: <%= task.ends_on %></p>
        <p><%= link_to 'Edit', edit_task_path(task) %></p>      
    <% end %>

<% end %>

<h2>Wednesday:  <%= Date.today.monday+2 %></h2>
<% @tasks.each do |task| %>
    <% if task.wednesday? %>
        <h3><%= link_to task.name, task %></h3>
        <p>Starts: <%= task.starts_on %></p>
        <p>Ends: <%= task.ends_on %></p>
        <p><%= link_to 'Edit', edit_task_path(task) %></p>      
    <% end %>

<% end %>

<h2>Thursday:  <%= Date.today.monday+3 %></h2>
<% @tasks.each do |task| %>
    <% if task.thursday? %>
        <h3><%= link_to task.name, task %></h3>
        <p>Starts: <%= task.starts_on %></p>
        <p>Ends: <%= task.ends_on %></p>
        <p><%= link_to 'Edit', edit_task_path(task) %></p>      
    <% end %>

<% end %>


Schema:
    ActiveRecord::Schema.define(:version => 20120315004635) do
      create_table "tasks", :force => true do |t|
    t.string   "name"
    t.date     "starts_on"
    t.date     "ends_on"
    t.boolean  "monday"
    t.boolean  "tuesday"
    t.boolean  "wednesday"
    t.boolean  "thursday"
    t.boolean  "friday"
    t.boolean  "saturday"
    t.boolean  "sunday"
    t.boolean  "completed"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Model - no added code.

Any help greatly appreciated!

1

1 Answers

3
votes

You need metaprogramming

For example in view

<%- %w(sunday monday tuesday wednesday thursday friday saturday).each |day| %>
  <h2><%= day.titleize %>: <%= Date.today.send(day) %></h2>
  <% @tasks.each do |task| %>
    <% if task.send("#{day}?") %>
        <h3><%= link_to task.name, task %></h3>
        <p>Starts: <%= task.starts_on %></p>
        <p>Ends: <%= task.ends_on %></p>
        <p><%= link_to 'Edit', edit_task_path(task) %></p>      
    <% end %>

  <% end %>
<% end %>

And so on