How do I control if-then explosions in view files?

I apologize if this does not meet the good recommendations, but I hope this is good in the class with How to manage CSS hacking and gets a similarly useful answer.

I am familiar with some basic adjacency mitigation strategies, such as:

  • Use helpers if necessary
  • Do not repeat yourself
  • Use partial and layouts

Feel free to suggest something if I miss any big ideas in the list above.

However, I still get several dimensions / degrees of freedom, in my opinion, causing a lot of if-then statements, or at least three-dimensional blocks. For example, in something that I am facing right now, I am working on a title bar for a program where a view is called when three "big" variables are called:

  • Admin user
  • Is the user logging in
  • Regardless of whether the page is being viewed by the user or someone else.

In the end, it looks like this mess:

<% content_for :subheader do %>
  <div class="row">
    <% if @user %>
      <% if @user == current_user %>
        <%= link_to 'My programs', user_programs_path(current_user), :class => 'active' %>
      <% else %>
        <%= link_to "#{@user.username} programs", user_programs_path(@user), :class => 'active' %>
      <% end %>
      <%= link_to 'Browse all programs', programs_path %>
    <% else %>
      <% if current_user %>
        <%= link_to 'My programs', user_programs_path(current_user) %>
      <% end %>
      <%= link_to 'Browse all programs', programs_path, :class => 'active' %>
    <% end %>
    <%= link_to 'New Program', new_program_path, :class => 'admin' if current_user.admin? %>
  </div>
  <% if @regions %>
    <div class="row second">
      <%= link_to 'Regional program search', request.fullpath, :class => 'active' %>
    </div>
  <% end %>
<% end %>

Nasty. Readability and easy accessibility, but ugly. Some suggestions?

Between experience and new technologies, such as LESS , I have become very good for losing weight on my CSS files, but I still encounter problems related to the explosion with my MVC views.

+5
2

, :

class User
  def possesive
    self == current_user ? 'My' : "#{username}'s"
  end
end

module ...Helper
  def user_program_link user
    if user
      link_to "#{user.possesive} programs", user_programs_path(user), :class => 'active'
    elsif current_user
      link_to 'My programs', user_programs_path(current_user)
    end
  end
end

if user_program_path:

<%= user_program_link @user %>

:

<% content_for :subheader do %>
  <div class="row">
    <%= user_program_link @user %>
    <% if @user %>
      <%= link_to 'Browse all programs', programs_path %>
    <% else %>
      <%= link_to 'Browse all programs', programs_path, :class => 'active' %>
    <% end %>
    <%= link_to 'New Program', new_program_path, :class => 'admin' if current_user.admin? %>
  </div>
  <% if @regions %>
    <div class="row second">
      <%= link_to 'Regional program search', request.fullpath, :class => 'active' %>
    </div>
  <% end %>
<% end %>

, DRY .

+6

:

<% content_for :subheader do %>
  <div class="row">
    <% if @user ||  current_user %>      
      <%= link_to ((current_user == @user or @user.nil?) ? "My programs" : 
                    "#{@user.username} programs"), 
                  user_programs_path(@user || current_user), 
                  :class => 'active' %>
    <% end %>
    <%= link_to 'Browse all programs', programs_path, 
          :class => (@user ? '' : 'active') %>
    <%= link_to 'New Program', new_program_path, :class => 'admin' if current_user.admin? %>
  </div>
  <% if @regions %>
    <div class="row second">
      <%= link_to 'Regional program search', request.fullpath, :class => 'active' %>
    </div>
  <% end %>
<% end %>
0

All Articles