From 7e3c2d5a6c3010b1f161b6d8cea8efa68fc741b1 Mon Sep 17 00:00:00 2001 From: Lorin Werthen Date: Thu, 9 Feb 2017 02:31:56 +0100 Subject: [PATCH] some code cleanup --- lib/helpers/archives.rb | 20 ++++++-------------- lib/helpers/blog.rb | 2 +- lib/helpers/preprocess.rb | 9 +++++---- lib/helpers/time.rb | 14 +++++++------- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/helpers/archives.rb b/lib/helpers/archives.rb index 6646ac1..b4362d6 100644 --- a/lib/helpers/archives.rb +++ b/lib/helpers/archives.rb @@ -1,26 +1,18 @@ module ArchiveHelper def academic_years - academic_years = Set.new - - items.find_all('/blog/*/*').each do |i| - academic_year = %r{/(\d\d)-\d\d/}.match(i.identifier).captures[0] - academic_years << academic_year.to_i - end - - academic_years + Set.new(items.find_all('/blog/*/*').map { |i| i.identifier.to_s[/\d\d-\d\d/] }).to_a end def academic_years_items - academic_years.to_a.reverse.map { |y| [y, items["/blog/#{y}-#{y + 1}.html"]] }.to_h + academic_years.reverse.map { |y| [y, items["/blog/#{y}.html"]] } end def pretty_year(year) - "'#{year} - '#{year + 1}" + year = year.scan(/\d\d/) + "'#{year[0]} - '#{year[1]}" end - def posts_in_year(academic_year) - items.find_all('/blog/*/*').sort_by { |x| Date.parse x[:created_at] }.select do |post| - post[:academic_year] == academic_year - end.reverse + def posts_in_year(y) + items.find_all("/blog/#{y}/*").sort_by { |x| x[:created_at] }.reverse end end diff --git a/lib/helpers/blog.rb b/lib/helpers/blog.rb index 7b7a768..14cd657 100644 --- a/lib/helpers/blog.rb +++ b/lib/helpers/blog.rb @@ -3,7 +3,7 @@ require 'words_counted' module BlogHelper def reading_time(blogpost) human_wpm = 200.0 - words = WordsCounted.count(blogpost.reps[:text].compiled_content).token_count + words = WordsCounted.count(blogpost.compiled_content(rep: :text)).token_count minutes = (words / human_wpm).ceil diff --git a/lib/helpers/preprocess.rb b/lib/helpers/preprocess.rb index 0dbfcdf..5d2882c 100644 --- a/lib/helpers/preprocess.rb +++ b/lib/helpers/preprocess.rb @@ -8,12 +8,13 @@ module PreprocessHelper def update_blog_attributes @items.find_all('/blog/**/*').each do |i| - year_str = %r{/(\d\d)-\d\d/}.match(i.identifier).captures[0] + year_str = %r{/(\d\d-\d\d)/}.match(i.identifier).captures[0] attr_hash = { # Tag all posts with article (for Blogging helper) kind: 'article', - academic_year: year_str.to_i + academic_year: year_str, + created_at: Date.parse(i[:created_at]) } i.update_attributes(attr_hash) @@ -26,11 +27,11 @@ module PreprocessHelper @items.create( '', { academic_year: year, title: 'Blog' }, - "/blog/#{year}-#{year + 1}.html" + "/blog/#{year}.html" ) end - academic_years_items[academic_years.max].update_attributes( + academic_years_items[0][1].update_attributes( navigable: true, order: 10 ) diff --git a/lib/helpers/time.rb b/lib/helpers/time.rb index 51c970b..685d7aa 100644 --- a/lib/helpers/time.rb +++ b/lib/helpers/time.rb @@ -1,20 +1,20 @@ module TimeHelper def christmastime? - timehelper([[Time.new(Time.now.year,12,6),Time.new(Time.now.year,12,31)]]) + timehelper([[Time.new(Time.now.year, 12, 6), Time.new(Time.now.year, 12, 31)]]) end def studytime? year = Time.now.year timehelper([ - [Time.new(year,12,14), Time.new(year,12,31)], - [Time.new(year,1,1), Time.new(year,2,7)], - [Time.new(year,5,15), Time.new(year,6,30)], - [Time.new(year,8,5), Time.new(year,9,10)], - ]) + [Time.new(year, 12, 14), Time.new(year, 12, 31)], + [Time.new(year, 1, 1), Time.new(year, 2, 7)], + [Time.new(year, 5, 15), Time.new(year, 6, 30)], + [Time.new(year, 8, 5), Time.new(year, 9, 10)] + ]) end def timehelper(ranges) - ranges.any? {|range| periodhelper(*range)} + ranges.any? { |range| periodhelper(*range) } end def periodhelper(startdate, enddate)