mirror of
https://github.com/manyfold3d/manyfold.git
synced 2026-10-22 03:32:18 -05:00
If we do need to link between models, we will do it with proper relationships.
15 lines
502 B
Ruby
15 lines
502 B
Ruby
module Linkable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
has_many :links, as: :linkable, dependent: :destroy
|
|
accepts_nested_attributes_for :links, reject_if: :link_not_valid?, allow_destroy: true
|
|
|
|
scope :linked_to, ->(url) { includes(:links).where("links.url": url) }
|
|
|
|
def link_not_valid?(attributes)
|
|
attributes[:url].blank? || links.map(&:url).include?(attributes[:url]) || [PublicUrl.hostname, "localhost"].include?(URI.parse(attributes[:url]).hostname)
|
|
end
|
|
end
|
|
end
|