Skip to content

Instantly share code, notes, and snippets.

@gmmcal
Created March 13, 2018 11:08
Show Gist options
  • Save gmmcal/ebb5489dfcebd3d54bce3720d82263e3 to your computer and use it in GitHub Desktop.
Save gmmcal/ebb5489dfcebd3d54bce3720d82263e3 to your computer and use it in GitHub Desktop.
# fetch all date_pub from active Post already ordered
dates = Post.where(active: true).order(date_pub: :desc).pluck(:date_pub)
# normalize all data into an array and remove duplication
data = dates.map { |date| { year: date.year, month: date.strftime('%B') } }.uniq
# group all data by year
year_groups = data.group_by { |group| group[:year] }
# normalize data to be more organized
# group[0] is year, group[1] is a group_by result. something like [{:year=>2018, :month=>"March"}, {:year=>2018, :month=>"January"}]
# final data will be similar to [{2018=>["March", "January"]}, {2017=>["April", "May", "December"]}]
groups = year_groups.map {|ygroup| { ygroup[0] => ygroup[1].map {|month| month[:month] } } }
# print data
groups.each do |group|
group.each do |year, months|
p year
months.each do |month|
p "- #{month}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment