amritha
amritha

Reputation: 75

Creating a method for styling purposes in rails

I have a pretty basic blog app on rails, and I need help creating a method.

For each post, the user has to specify a neighborhood, and for styling purposes, I want to find out if that neighborhood falls in a certain bucket so I can color that post accordingly. For instance, the neighborhood "SOMA" is a part of the bucket "Downtown."

I imagined that what I would do is pass the neighborhood into a method that determines which bucket it falls in and I can somehow output the bucket to correspond to a div class in the index. Open to other suggestions.

Does anyone know where a method like this would live?

Update:

I added a barebones method in PostsHelper to even see if it works, then i can flesh out the logic

module PostsHelper

def bucket(neighbor)
    case neighbor
        when "SOMA"
        puts "Downtown"

        when "Mission"
        puts "Dolores"

        else
        puts "foo"
    end

end

end

Then I tried calling the method in the post index and nothing happens. This may be the dumbest question ever, but what am I missing? Is this even the right way to go about it?:

<ul id="post-list">
<% @posts.each do |post| %>

<li><%= post.content %></li>
<li><%= post.attribution %> in <%= post.neighborhood %> 
<span class="post-date"><%=time_ago_in_words(post.created_at) %> ago</span></li>
<li> <%= bucket(yield(post.neighborhood)) %> </li> 

<% end %>
</ul>

Upvotes: 0

Views: 157

Answers (2)

apneadiving
apneadiving

Reputation: 115511

This kind of methods should live in a decorator.

See this railscast: http://railscasts.com/episodes/286-draper


Edit after your details

def bucket(neighbor)
  case neighbor
  when "SOMA"    then "Downtown"
  when "Mission" then "Dolores"
  else
    "foo"
  end
end

In your view:

<li> <%= bucket(post.neighborhood) %> </li>

But you should really consider using decorators.

Upvotes: 1

anxiety
anxiety

Reputation: 1709

What you could do is create a resource (table + model, Bucket) that keeps track of the buckets you care about - where perhaps the Bucket name attribute holds the name of the greater geographical area you mentioned. Then you could have Post belong to Bucket.

In your view, you could populate the class of the post's container with the post.bucket.name, and then create styles for the different bucket names with your preset colors and whatnot.

If you want to be query-efficient:

If you take this approach, consider caching the Bucket name attribute as an attribute of Post (so extra queries don't have to be made each time a view is rendered). The purpose in still having the Bucket class (instead of just a bucket_name attribute in the Post model) is so that you can normalize the Bucket names, and potentially update them yourself. To make sure the cached bucket_name attributes get updated on your Post records, you could create an after_update callback in your Bucket model that selects all Posts belonging to that Bucket, and updates their bucket_name attributes.

Upvotes: 0

Related Questions