Skip to content

Commit

Permalink
Refactor description formatting in List model and rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Jan 11, 2024
1 parent fb6a9ec commit a19b533
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions app/models/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/lists.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a19b533

Please sign in to comment.