Class: LocalAuthority

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/local_authority.rb

Class Method Summary collapse

Class Method Details

.entries_by_term(model, term, query) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/local_authority.rb', line 61

def self.entries_by_term(model, term, query)
  return if query.empty?
  low_query = query.downcase
  hits = []
  # move lc_subject into it's own table since being part of the usual structure caused it to be too slow.
  # When/if we move to having multiple dictionaries for subject we will need to also do a check for the appropriate dictionary.
  if term == 'subject' && model == 'generic_files' # and local_authoritiy = lc_subject
    sql = SubjectLocalAuthorityEntry.where("lowerLabel like ?", "#{low_query}%").select("label, uri").limit(25).to_sql
    SubjectLocalAuthorityEntry.find_by_sql(sql).each do |hit|
      hits << { uri: hit.uri, label: hit.label }
    end
  else
    dterm = DomainTerm.where(model: model, term: term).first
    if dterm
      authorities = dterm.local_authorities.collect(&:id).uniq
      sql = LocalAuthorityEntry.where("local_authority_id in (?)", authorities).where("lower(label) like ?", "#{low_query}%").select("label, uri").limit(25).to_sql
      LocalAuthorityEntry.find_by_sql(sql).each do |hit|
        hits << { uri: hit.uri, label: hit.label }
      end
    end
  end
  hits
end

.harvest_rdf(name, sources, opts = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/local_authority.rb', line 7

def self.harvest_rdf(name, sources, opts = {})
  return unless where(name: name).empty?
  authority = create(name: name)
  format = opts.fetch(:format, :ntriples)
  predicate = opts.fetch(:predicate, ::RDF::SKOS.prefLabel)
  entries = []
  sources.each do |uri|
    ::RDF::Reader.open(uri, format: format) do |reader|
      reader.each_statement do |statement|
        next unless statement.predicate == predicate
        entries << LocalAuthorityEntry.new(local_authority: authority,
                                           label: statement.object.to_s,
                                           uri: statement.subject.to_s)
      end
    end
  end
  if LocalAuthorityEntry.respond_to? :import
    LocalAuthorityEntry.import entries
  else
    entries.each(&:save!)
  end
end

.harvest_tsv(name, sources, opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/local_authority.rb', line 30

def self.harvest_tsv(name, sources, opts = {})
  return unless where(name: name).empty?
  authority = create(name: name)
  prefix = opts.fetch(:prefix, "")
  entries = []
  sources.each do |uri|
    open(uri) do |f|
      f.each_line do |tsv|
        fields = tsv.split(/\t/)
        entries << LocalAuthorityEntry.new(local_authority: authority,
                                           uri: "#{prefix}#{fields[0]}/",
                                           label: fields[2])
      end
    end
  end
  if LocalAuthorityEntry.respond_to? :import
    LocalAuthorityEntry.import entries
  else
    entries.each(&:save!)
  end
end

.register_vocabulary(model, term, name) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/models/local_authority.rb', line 52

def self.register_vocabulary(model, term, name)
  authority = find_by_name(name)
  return if authority.blank?
  model = model.to_s.sub(/RdfDatastream$/, '').underscore.pluralize
  domain_term = DomainTerm.find_or_create_by(model: model, term: term)
  return if domain_term.local_authorities.include? authority
  domain_term.local_authorities << authority
end