Links, mainly...
This commit is contained in:
parent
fd42a3f173
commit
21ab02d647
69 changed files with 2258 additions and 155 deletions
|
|
@ -7,4 +7,27 @@ class Element < ApplicationRecord
|
|||
has_many :success_criteria, dependent: :destroy
|
||||
|
||||
validates :path, :title, presence: true
|
||||
|
||||
# Calculate actual conformity level:
|
||||
# - if a success_criterion has result :failed -> the confirmity_level
|
||||
# of that success_criterion is not reached.
|
||||
# - the resulting level is the highest readched level
|
||||
# abeying rule above.
|
||||
def level
|
||||
return nil
|
||||
return nil unless success_criteria.all(&:result)
|
||||
|
||||
min_failed = success_criteria.select(&:failed?).map(&:level).min
|
||||
possible_levels = success_criteria.select(&:passed?).map(&:level).uniq
|
||||
|
||||
return nil if possible_levels.empty?
|
||||
|
||||
puts possible_levels.inspect
|
||||
puts min_failed
|
||||
possible_levels[possible_levels.find_index(min_failed) - 1]
|
||||
end
|
||||
|
||||
def max_level
|
||||
@max_level ||= success_criteria.reject(&:not_applicable?).max(&:level)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
51
app/models/link.rb
Normal file
51
app/models/link.rb
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
class Link < ApplicationRecord
|
||||
belongs_to :link_category
|
||||
has_rich_text :description_html
|
||||
|
||||
validates :url, :text, presence: true
|
||||
validate :check_url
|
||||
validate :valid_url
|
||||
|
||||
before_validation :ensure_absolute_url
|
||||
|
||||
def ok?
|
||||
fail_count == 0
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
|
||||
|
||||
def check_url(input = url, add_error = true)
|
||||
response = begin
|
||||
Net::HTTP.get_response(URI.parse(input), "User-Agent": USER_AGENT)
|
||||
rescue Socket::ResolutionError
|
||||
errors.add(:url, :invalid) if add_error
|
||||
return false
|
||||
end
|
||||
|
||||
case response
|
||||
when Net::HTTPSuccess
|
||||
self.last_check_at = Time.zone.now
|
||||
self.fail_count = 0
|
||||
|
||||
true
|
||||
when Net::HTTPRedirection
|
||||
check_url(response[:location])
|
||||
else
|
||||
self.last_check_at = Time.zone.now
|
||||
self.fail_count += 1
|
||||
errors.add(:url, :invalid) if add_error
|
||||
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def ensure_absolute_url
|
||||
self.url = "https://#{self.url}" unless self.url.match?(%r{https{0,1}://.*})
|
||||
end
|
||||
|
||||
def valid_url
|
||||
errors.add(:url, :invalid) unless url.match /\A#{URI::regexp(['http', 'https'])}\z/
|
||||
end
|
||||
end
|
||||
5
app/models/link_category.rb
Normal file
5
app/models/link_category.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class LinkCategory < ApplicationRecord
|
||||
has_many :links
|
||||
|
||||
has_rich_text :description_html
|
||||
end
|
||||
|
|
@ -6,4 +6,10 @@ class SuccessCriterion < ApplicationRecord
|
|||
has_rich_text :description_html
|
||||
|
||||
belongs_to :element, touch: true
|
||||
|
||||
def level_value
|
||||
return nil unless level
|
||||
|
||||
self.class.levels.values.index_of(level)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue