-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from loftwah/dl/improve-user-model
normal urls
- Loading branch information
Showing
4 changed files
with
45 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
class Achievement < ApplicationRecord | ||
belongs_to :user | ||
|
||
has_many :achievement_views | ||
|
||
validates :title, presence: true | ||
validates :date, presence: true | ||
validates :description, presence: true | ||
end | ||
|
||
before_save :normalize_url | ||
|
||
private | ||
|
||
def normalize_url | ||
# Ensure the URL starts with http:// or https:// | ||
unless url.blank? || url =~ /\Ahttps?:\/\// | ||
self.url = "http://#{url}" | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace :normalize do | ||
desc "Normalize URLs for all existing users' links and achievements" | ||
task normalize_urls: :environment do | ||
Link.find_each do |link| | ||
unless link.url.blank? || link.url =~ /\Ahttps?:\/\// | ||
link.update(url: "http://#{link.url}") | ||
puts "Normalized URL for link ID #{link.id}: #{link.url}" | ||
end | ||
end | ||
|
||
Achievement.find_each do |achievement| | ||
unless achievement.url.blank? || achievement.url =~ /\Ahttps?:\/\// | ||
achievement.update(url: "http://#{achievement.url}") | ||
puts "Normalized URL for achievement ID #{achievement.id}: #{achievement.url}" | ||
end | ||
end | ||
|
||
puts "URL normalization completed for all existing links and achievements." | ||
end | ||
end |