<%= link_to 'Search New York Times articles', root_path %>

<%= form_tag search_path, method: 'get', role: 'search' do %>
<%= text_field_tag :q, params[:q], class: 'form-control', placeholder: 'Search...' %>
<% params.slice(:a, :c, :s).each do |name, value| %> <%= hidden_field_tag name, value %> <% end %>

Displaying <%= (params[:page] || 1).to_i.ordinalize %> page with <%= @articles.size %> articles of total <%= @articles.total %>

<% end %>
<% if @articles.size < 1 && @articles.response.suggestions.present? %>

No documents have been found. <% if @articles.response.suggestions.terms.present? %> Maybe you mean <%= @articles.response.suggestions.terms.map do |term| link_to term, search_path(params.except(:controller, :action).merge q: term) end.to_sentence(last_word_connector: ' or ').html_safe %>? <% end %>

<% end %>
<% unless @articles.size < 1 %>

<%= link_to 'All Sections →'.html_safe, search_path(params.except(:controller, :action).merge(c: nil))%>

<% @articles.response.response['aggregations']['categories']['categories']['buckets'].each do |c| %> <%= link_to search_path(params.except(:controller, :action).merge(c: c['key'])), class: "list-group-item#{' active' if params[:c] == c['key']}" do c['key'].titleize.html_safe + content_tag(:small, c['doc_count'], class: 'badge').html_safe end %> <% end %>

<%= link_to 'All Authors →'.html_safe, search_path(params.except(:controller, :action).merge(a: nil))%>

<% @articles.response.response['aggregations']['authors']['authors']['buckets'].each do |a| %> <%= link_to search_path(params.except(:controller, :action).merge(a: a['key'])), class: "list-group-item#{' active' if params[:a] == a['key']}" do a['key'].titleize.html_safe + content_tag(:small, a['doc_count'], class: 'badge').html_safe end %> <% end %>

<%= link_to 'Any Date →'.html_safe, search_path(params.except(:controller, :action).merge(w: nil))%>

<% @articles.response.response['aggregations']['published']['published']['buckets'].each do |w| %> <%= __start = Time.at(w['key']/1000) __end = __start.end_of_week __date = __start.to_date.to_s(:iso) link_to search_path(params.except(:controller, :action).merge(w: __date)), class: "list-group-item#{' active' if params[:w] == __date}" do "#{__start.to_date.to_s(:short)} — #{__end.to_date.to_s(:short)}".html_safe + \ content_tag(:small, w['doc_count'], class: 'badge').html_safe end %> <% end %>
<% end %>
<% @articles.each do |article| %>

<%= (article.try(:highlight).try(:title) ? article.highlight.title.join.html_safe : article.title) %> <%= article.categories.to_sentence %>

<% if article.try(:highlight).try(:abstract) %> <%= article.highlight.abstract.join.html_safe %> <% else %> <%= article.try(:highlight).try(:content) ? article.highlight.content.join('…').html_safe : article.abstract %> <% end %>

<% if comments = article.try(:highlight) && article.highlight['comments.body'] %>

Comments: <%= comments.join('…').html_safe %>

<% end %>

Authors: <%= article.authors.map(&:full_name).to_sentence %> | Published: <%= article.published_on %> | Score: <%= article._score %>

<% end %>