Created
March 13, 2018 11:08
-
-
Save gmmcal/ebb5489dfcebd3d54bce3720d82263e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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