Skip to content

Instantly share code, notes, and snippets.

@s0ber
Last active December 12, 2015 00:08
Show Gist options
  • Save s0ber/4681364 to your computer and use it in GitHub Desktop.
Save s0ber/4681364 to your computer and use it in GitHub Desktop.
Posts list markup (HAML)
- # in controller
- @posts_most_popular = Post.where('likes_count > ?', 100)
- @posts_most_commented = Post.where('comments_count > ?', 20)
- @posts_draft = Post.where(status: :draft)
- # in view
- if @posts_most_popular.any?
.posts_list.for-most_popular
.posts_list__title Most popular
- @posts_most_popular.each_with_index do |post, i|
- # three most popular posts have modifier
.posts_list__item{class: ('is-top' if i < 3)}= link_to post.title, post_path(post)
- if @posts_most_commented.any?
.posts_list.for-most_commented
.posts_list__title Most commented
- @posts_most_commented.each do |post|
- # for example, we are in post page and we want to mark current post in a list
- # we are using modifier for this
- if @post.id == post.id
.posts_list__item.is-active= post.title
- else
.posts_list__item= link_to post.title, post_path(post)
- if current_user.role.is_a?(Admin) && @posts_draft.any?
.posts_list.for-drafts
.posts_list__title Drafts
- @posts_draft.each do |post|
.posts_list__item= link_to post.title, post_path(post)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment