Skip to content

Instantly share code, notes, and snippets.

@mcade
Last active August 29, 2015 13:58
Show Gist options
  • Save mcade/9999707 to your computer and use it in GitHub Desktop.
Save mcade/9999707 to your computer and use it in GitHub Desktop.
link_to query
microposts_controller.rb
def index
@micropost = current_user.microposts.build
@microposts = case params["show"]
when "daily"
Kaminari.paginate_array(Micropost.popularToday).page(params[:page]).per(25)
when "weekly"
Kaminari.paginate_array(Micropost.popularWeekly).page(params[:page]).per(25)
when "monthly"
Kaminari.paginate_array(Micropost.popularMonthly).page(params[:page]).per(25)
else
Kaminari.paginate_array(Micropost.popular).page(params[:page]).per(25)
end
end
micropost.rb
def self.popular
reorder('votes desc').order('created_at DESC').find_with_reputation(:votes, :all)
#find_with_reputation(:votes, :all, order: 'votes desc')
end
def self.popularToday
reorder('votes desc').order('created_at DESC').find_with_reputation(:votes, :all, { :conditions => ["DATE(microposts.created_at) = DATE(NOW())"]})
end
def self.popularWeekly
reorder('votes desc').order('created_at DESC').find_with_reputation(:votes, :all, { :conditions => ["EXTRACT(WEEK FROM microposts.created_at) = EXTRACT(WEEK FROM now())"]})
end
def self.popularMonthly
reorder('votes desc').order('created_at DESC').find_with_reputation(:votes, :all, { :conditions => ["EXTRACT(MONTH FROM microposts.created_at) = EXTRACT(MONTH FROM now())"]})
end
index.html.erb for microposts view
<form action="" method="get">
<fieldset>
<button type="submit" name="show" value="daily">Today</button>
<button type="submit" name="show" value="weekly">This week</button>
<button type="submit" name="show" value="monthly">This month</button>
</fieldset>
</form>
<div class="col-md-6 col-md-offset-3">
<%= form_tag microposts_path, method: :get do %>
<%= text_field_tag :query, params[:query], placeholder: 'Search for a micropost' %>
<%= submit_tag "Search", name: nil, class: "btn btn-lrg btn-primary" %>
<% if params[:query].present? %>
<% @microposts = Micropost.search(params[:query]).page params[:page] %>
<% end %>
<% end %>
<%= render 'shared/public_feed' %>
</div>
_micropost.html.erb partial
<li>
<span class="content">
<%= micropost.content %>
<span>
<%= form_for(@micropost) do |f| %>
<%= f.hidden_field :content, :value => micropost.content %>
<%= f.text_field :content1, placeholder: micropost.content1 %>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
<%= pluralize micropost.reputation_for(:votes).to_i, "vote" %>
<% unless micropost.user.id == current_user.id %>
<%= link_to "up", vote_micropost_path(micropost, type: "up"), method: "post" %>
<%= link_to "down", vote_micropost_path(micropost, type: "down"), method: "post" %>
<% end %>
</span>
<span>
<%= link_to "Retweet", retweet_micropost_path(micropost), :method => :post %>
<%= link_to "Comments", micropost %>
</span>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
data: { confirm: "You sure?" },
title: micropost.content %>
<% end %>
</li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment