From a19b53341162a3758a07238722a564cf9578a2be Mon Sep 17 00:00:00 2001 From: Andrew Nesbitt Date: Thu, 11 Jan 2024 11:00:38 +0000 Subject: [PATCH] Refactor description formatting in List model and rake task --- app/models/list.rb | 17 +++++++++++++++++ lib/tasks/lists.rake | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/models/list.rb b/app/models/list.rb index 609f3276..3340d4a6 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -54,6 +54,23 @@ def description end end + def awesome_description + return if description.blank? + # add a period if there isn't one + d = description.dup + # remove whitespace from start and end + d.strip! + # add a period if there isn't one + d = description[-1] == '.' ? description : "#{description}." + # start with a capital letter + d[0] = d[0].capitalize + # Should not repeat "." or "!" more than once + d.gsub!(/([.!?])\1+/, '\1') + # remove extra urls (e.g. http://example.com) + d.gsub!(/https?:\/\/\S+/, '') + d + end + def last_updated_at return updated_at unless repository.present? return updated_at unless repository['updated_at'].present? diff --git a/lib/tasks/lists.rake b/lib/tasks/lists.rake index 569fcdd2..505d687b 100644 --- a/lib/tasks/lists.rake +++ b/lib/tasks/lists.rake @@ -16,7 +16,7 @@ namespace :lists do List.displayable.order(Arel.sql("(repository ->> 'stargazers_count')::text::integer").desc.nulls_last).all.each do |list| next if list.description.blank? next if list.name.include?('?') - puts "- [#{list.name}](#{list.url}) - #{list.description}#{list.description[-1] == '.' ? '' : '.'}" + puts "- [#{list.name}](#{list.url}) - #{list.awesome_description}" end end end \ No newline at end of file