Class: Link

Inherits:
ApplicationRecord show all
Includes:
Problematic
Defined in:
app/models/link.rb

Constant Summary

Constants inherited from ApplicationRecord

ApplicationRecord::SAFE_NAME_LENGTH

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

find_param

Class Method Details

.deserializer_for(url:, for_class: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/link.rb', line 40

def self.deserializer_for(url:, for_class: nil)
  [
    Integrations::Cults3d::CreatorDeserializer,
    Integrations::Cults3d::ModelDeserializer,
    Integrations::MyMiniFactory::CreatorDeserializer,
    Integrations::MyMiniFactory::CollectionDeserializer,
    Integrations::MyMiniFactory::ModelDeserializer,
    Integrations::Thangs::ModelDeserializer,
    Integrations::Thingiverse::CreatorDeserializer,
    Integrations::Thingiverse::CollectionDeserializer,
    Integrations::Thingiverse::ModelDeserializer
  ].map do |klass|
    klass.new(uri: url)
  end.find { it.valid?(for_class: for_class) }
end

.find_duplicated ⇒ Object



28
29
30
31
32
33
34
# File 'app/models/link.rb', line 28

def self.find_duplicated
  Link.select(:linkable_type, :linkable_id, :url) # rubocop:disable Pundit/UsePolicyScope
    .group([:linkable_type, :linkable_id, :url])
    .having("count(*) > 1").map do
    Link.find_by(linkable_type: it.linkable_type, linkable_id: it.linkable_id, url: it.url)
  end
end

Instance Method Details

#deserializer ⇒ Object



36
37
38
# File 'app/models/link.rb', line 36

def deserializer
  self.class.deserializer_for(url: url, for_class: linkable.class)
end

#host ⇒ Object



12
13
14
15
16
# File 'app/models/link.rb', line 12

def host
  URI.parse(url).host || url
rescue URI::InvalidURIError, URI::InvalidComponentError
  url
end

#remove_duplicates! ⇒ Object



24
25
26
# File 'app/models/link.rb', line 24

def remove_duplicates!
  Link.where.not(id: id).where(linkable: linkable, url: url).destroy_all # rubocop:disable Pundit/UsePolicyScope
end

#site ⇒ Object



18
19
20
21
22
# File 'app/models/link.rb', line 18

def site
  PublicSuffix.parse(host).sld
rescue PublicSuffix::DomainInvalid
  host
end


56
57
58
# File 'app/models/link.rb', line 56

def (organize: false, apply_permissions_after_sync: nil)
  UpdateMetadataFromLinkJob.perform_later(link: self, organize: organize, apply_permissions_after_sync: apply_permissions_after_sync)
end