Newbie question regarding where to put logic for UI elements

Hello folks.

I am building a site that let’s users track their progress on 30 day
challenges. To record their progress, users make a Post for each day
of the challenge. The Post object has a “complete” attribute to record
if they were successful on that particular day. When users view a
challenge, I build a UI element that both let’s users navigate to
daily Posts, and get a snapshot of the challenger’s progress. It
looks a little like this …

[10/11] [10/12] [10/13] … * 30 days, with different colored boxes

There are five types of buttons, with factors such as today’s date,
the presence of posts, the success of posts, etc.

  1. This day has a post, and the user was successful. This button is
    GREEN, and has a link to view the post.
  2. This day has a post, and the user was unsuccessful. This button is
    RED, and has a link to view the post
  3. This day is after today, so no posting is possible. This button is
    GREY, and has NO link.
  4. This day is post-able, but there is no post. This button is WHITE.
  5. This day is post-able, and the logged in user owns the challenge.
    This button is YELLOW, and has a link to create a new post.

Tests with users have shown that this is one of their favorite parts
of the app, so it needs to stay.

I think I am approaching this incorrectly. Right now, I am building an
array of arrays in the controller … 1 item for each of the 30 days
in the nav. Each item is an array with data for the view …
. I pass this to the view using an
instance variable @thirtydays, and then use more logic on the View end
(e.g. “if boxtype == 3”) to decide how to display the button.

Something seems fishy about this to me but I am not skilled enough to
know why. What is the ‘correct’ way to supply data for UI elements
with lots of logic? Should I keep this all in the View? Should I
create a method under the Challenger class that takes a number (1-30),
and spits information for that particular day, and iterate from 1-30
in the View to create the buttons?

Thanks in advanced.

Confused Newbie

John

On Jul 25, 8:50 pm, Astorian [email protected] wrote:

create a method under the Challenger class that takes a number (1-30),
and spits information for that particular day, and iterate from 1-30
in the View to create the buttons?

I think you’re close… Instead of an array of arrays, I would replace
each ‘inner array’ with a full-fledged object.

Add a file called (for example) day_indicator.rb in your /lib folder.
As long as you name the class correctly, Rails will find it
automatically for you:

class DayIndicator

attr_accessor :date, :type, :post id, :border_type

end

Now in your controller, just create an instance of each of these as
needed and add it to your array. In your view, each item in the array
is a DayIndicator object so you can easily access the attributes.

Plus, you can continue to add logic to DayIndicator as needed, and it
will be encapsulated away from the view.

How does that sound?

Jeff
www.essentialrails.com - 2-Day workshop on Rails for beginners.
Chicago. Sept 21-22, 2007.
www.softiesonrails.com - blog