Skip to content

Commit

Permalink
Merge pull request #152 from loftwah/dl/improve-user-model
Browse files Browse the repository at this point in the history
normal urls
  • Loading branch information
loftwah authored Sep 14, 2024
2 parents d08aef5 + 941b8e8 commit 175a3dd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
14 changes: 12 additions & 2 deletions app/models/achievement.rb
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
8 changes: 8 additions & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Link < ApplicationRecord
scope :hidden, -> { where(hidden: true) }

before_save :ensure_unique_position
before_save :normalize_url

private

Expand All @@ -17,4 +18,11 @@ def ensure_unique_position
self.position = user.links.maximum(:position).to_i + 1
end
end

def normalize_url
# Ensure the URL starts with http:// or https://
unless url.blank? || url =~ /\Ahttps?:\/\//
self.url = "http://#{url}"
end
end
end
11 changes: 5 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ class User < ApplicationRecord

has_many :links, dependent: :destroy
has_many :achievements, dependent: :destroy

has_many :daily_metrics
has_many :page_views
has_many :link_clicks
has_many :achievement_views
has_many :daily_metrics, dependent: :destroy
has_many :page_views, dependent: :destroy
has_many :link_clicks, dependent: :destroy
has_many :achievement_views, dependent: :destroy

validates :username, uniqueness: true, allow_blank: true
validates :full_name, presence: true
Expand Down Expand Up @@ -81,4 +80,4 @@ def ensure_username_presence
self.username = email.present? ? email.split('@').first : "user#{SecureRandom.hex(4)}"
end
end
end
end
20 changes: 20 additions & 0 deletions lib/tasks/normalize_urls.rake
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

0 comments on commit 175a3dd

Please sign in to comment.