0

I have this helper that show months and years from Events Model in a select input. How I show only current and future months and not the past months?

def select_month_tag(events)
  html = <<-HTML
  <select id="filtro-mes-ano" class="event-filter selectCustom2 event_filter_select">
    <option value="Filtrar por mês" disabled selected >Filtrar por mês</option>
  HTML

  events.each do | event |
    if not date_and_month(event.month_ref, event.year_ref).blank?
      html += <<-HTML
        <option data-year="#{event.year_ref}"
          data-month="#{event.month_ref}"
          "#{'selected' if is_hash_selected?(event)}">
          date_and_month(event.month_ref, event.year_ref)}
        </option>
      HTML
    end
  end

  html += <<-HTML
  </select>
  HTML

  html.html_safe
end

Thanks.

Kumar
  • 3,116
  • 2
  • 16
  • 24
Felipe Marcon
  • 239
  • 2
  • 17

2 Answers2

0

I guess you'll do something like this for the kind of code you've written. Let me know if it works for you

def select_month_tag(events)
  html = <<-HTML
  <select id="filtro-mes-ano" class="event-filter selectCustom2 event_filter_select">
    <option value="Filtrar por mês" disabled selected >Filtrar por mês</option>
  HTML

  events.each do | event |
    if not date_and_month(event.month_ref, event.year_ref).blank?
      # Get today's date
      today = Date.today

      # check if event year is greater than present date's year
      if event.year_ref >= today.year

        # check if event month is greater than present date's month
        if event.month_ref >= today.month # considering month_ref is a number between 1..12
          html += <<-HTML
            <option data-year="#{event.year_ref}"
              data-month="#{event.month_ref}"
              "#{'selected' if is_hash_selected?(event)}">
              date_and_month(event.month_ref, event.year_ref)}
            </option>
          HTML
        end
      end
    end
  end

  html += <<-HTML
  </select>
  HTML

  html.html_safe
end
Kumar
  • 3,116
  • 2
  • 16
  • 24
0

Why not filter the Event collection, then create options with that? I don't think you want this much logic in a helper. It would be better to make a scope on the model, and use that. Something like

Event.where('date > ?', date_variable_here)

Can you do greater than comparison on a date in a Rails 3 search?

Petercopter
  • 1,218
  • 11
  • 16