Class: Reference

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ReferenceComparable
Defined in:
app/models/reference_utility.rb,
app/models/reference.rb,
app/models/reference_search.rb,
app/models/reference_has_document.rb

Overview

These are methods used either now or at one time by Rake tasks, not by application code, so could well be dead

Direct Known Subclasses

MissingReference, UnmissingReference

Defined Under Namespace

Classes: BoltonReferenceNotFound, BoltonReferenceNotMatched, DuplicateMatcher

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from ReferenceComparable

#<=>

Class Method Details

+ (Object) citation_year_to_year(citation_year)



76
77
78
79
80
81
82
83
84
# File 'app/models/reference.rb', line 76

def self.citation_year_to_year citation_year
  if citation_year.blank?
    nil
  elsif match = citation_year.match(/\["(\d{4})"\]/)
    match[1]
  else
    citation_year.to_i
  end
end

+ (Object) do_search(options = {})



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/reference_search.rb', line 94

def self.do_search options = {}

  search_options = {}
  if options[:format] != :endnote_import
    search_options[:page] = options[:page] || 1
  end

  case
  when options[:whats_new]
    search_options.merge! :order => :created_at

  when options[:review]
    search_options.merge! :order => :updated_at

  when options[:is_author_search]
    author_names = Parsers::AuthorParser.parse(options[:q])[:names]
    authors = Author.find_by_names author_names
    search_options.merge! :authors => authors

  when options.key?(:q)
    fulltext_string = options[:q].dup || ''

    if match = fulltext_string.match(/\d{5,}/)
      return perform_search :id => match[0].to_i
    end

    start_year, end_year = parse_and_extract_years fulltext_string
    filter = parse_and_extract_filter fulltext_string
    fulltext_string = ActiveSupport::Inflector.transliterate fulltext_string.downcase

    search_options[:fulltext] = fulltext_string if fulltext_string
    search_options[:start_year] = start_year if start_year
    search_options[:end_year] = end_year if end_year
    search_options[:filter] = filter if filter

  end

  perform_search search_options

end

+ (Object) find_by_bolton_key(data)



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/models/reference_search.rb', line 159

def self.find_by_bolton_key data
  year = data[:year] || data[:in] && data[:in][:year]

  return MissingReference.import 'no year', data unless year

  bolton_key = Bolton::ReferenceKey.new(data[:author_names].join(' '), year).to_s :db

  reference = find_by_bolton_key_cache bolton_key
  return reference if reference

  bolton_reference = Bolton::Reference.find_by_key_cache bolton_key
  if !bolton_reference
    reference = MissingReference.import 'no Bolton', data
  else
    reference = bolton_reference.match || MissingReference.import('no Bolton match', data)
  end

  reference.update_attribute :bolton_key_cache, bolton_key

  reference
end

+ (Object) import_hol_document_urls(show_progress = false)



8
9
10
# File 'app/models/reference_utility.rb', line 8

def self.import_hol_document_urls show_progress = false
  Importers::Hol::DocumentUrlImporter.new(show_progress).import
end

+ (Object) parse_and_extract_filter(string)



150
151
152
153
154
155
156
157
# File 'app/models/reference_search.rb', line 150

def self.parse_and_extract_filter string
  question_mark_index = string.index '?'
  if question_mark_index
    string[question_mark_index] = ''
    string.strip!
    return :unknown_references_only
  end
end

+ (Object) parse_and_extract_years(string)



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/reference_search.rb', line 135

def self.parse_and_extract_years string
  start_year = end_year = nil
  if match = string.match(/\b(\d{4})-(\d{4}\b)/)
    start_year = match[1].to_i
    end_year = match[2].to_i
  elsif match = string.match(/(?:^|\s)(\d{4})\b/)
    start_year = match[1].to_i
  end

  return nil, nil unless (1758..(Time.now.year + 1)).include? start_year

  string.gsub! /#{match[0]}/, '' if match
  return start_year, end_year
end

+ (Object) perform_search(options = {})



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/reference_search.rb', line 23

def self.perform_search options = {}
  page = options[:page]
  case
  when options[:fulltext]
    start_year, end_year = options[:start_year], options[:end_year]
    search {
      keywords options[:fulltext]

      if start_year
        if end_year
          with(:year).between(start_year..end_year)
        else
          with(:year).equal_to start_year
        end
      end

      case options[:filter]
      when :unknown_references_only
        with :type, 'UnknownReference'
      when :no_missing_references
        without :type, 'MissingReference'
      end

      paginate :page => page if page

      order_by :author_names_string
      order_by :citation_year
    }.results

  when options[:authors]
    authors = options[:authors]
    query = select('`references`.*').
       joins(:author_names)
      .joins('JOIN authors ON authors.id = author_names.author_id')
      .where('authors.id IN (?)', authors)
      .group('references.id')
      .having("COUNT(`references`.id) = #{authors.length}")
      .order(:author_names_string_cache, :citation_year)

      case options[:filter]
      when :unknown_references_only
        with :type, 'UnknownReference'
      when :no_missing_references
        without :type, 'MissingReference'
      end
    query = query.paginate :page => page if page
    query 

  when options[:id]
    query = where :id => options[:id]
    query = query.paginate :page => page
    query

  else
    if options[:order]
      query = order "#{options[:order]} DESC"
    else
      query = order 'author_names_string_cache, citation_year'
    end
    query = query.paginate :page => page if page
    query
    case options[:filter]
    when :unknown_references_only
      query = query.where 'type == "UnknownReference"'
    when :no_missing_references, nil
      query = query.where 'type != "MissingReference" OR type IS NULL'
    end
    query
  end
end

Instance Method Details

- (Object) author



21
# File 'app/models/reference.rb', line 21

def author; principal_author_last_name; end

- (Object) author_names_string



33
# File 'app/models/reference.rb', line 33

def author_names_string()         author_names_string_cache end

- (Object) author_names_string=(string)



34
# File 'app/models/reference.rb', line 34

def author_names_string=(string)  self.author_names_string_cache = string end

- (Object) authors(reload = false)



32
# File 'app/models/reference.rb', line 32

def authors(reload = false)       author_names(reload).map &:author end

- (Object) check_for_duplicate



69
70
71
72
73
74
75
# File 'app/models/reference.rb', line 69

def check_for_duplicate
  duplicates = DuplicateMatcher.new.match self
  return unless duplicates.present?
  duplicate = Reference.find duplicates.first[:match]
  errors.add :base, "This may be a duplicate of #{Formatters::ReferenceFormatter.format duplicate} #{duplicate.id}.<br>To save, click \"Save Anyway\"".html_safe
  true
end

- (Object) check_not_nested

validation



44
45
46
47
48
# File 'app/models/reference.rb', line 44

def check_not_nested
  nester = NestedReference.find_by_nested_reference_id id
  errors.add :base, "This reference can't be deleted because it's nested in #{nester}" if nester
  nester.nil?
end

- (Object) document_host=(host)



14
15
16
# File 'app/models/reference_has_document.rb', line 14

def document_host= host
  document.host = host if document
end

- (Boolean) downloadable_by?(user)

Returns:

  • (Boolean)


10
11
12
# File 'app/models/reference_has_document.rb', line 10

def downloadable_by? user
  document.try :downloadable_by?, user
end

- (Object) key



31
# File 'app/models/reference.rb', line 31

def key()                         @key ||= ReferenceKey.new(self) end

- (Object) parse_author_names_and_suffix(author_names_string)

utility Called by controller to parse an input string for author names and suffix Returns hash of parse result, or adds to the reference's errors and raises



60
61
62
63
64
65
66
67
68
# File 'app/models/reference.rb', line 60

def parse_author_names_and_suffix author_names_string
  author_names_and_suffix = AuthorName.import_author_names_string author_names_string.dup
  if author_names_and_suffix[:author_names].empty? && author_names_string.present?
    errors.add :author_names_string, "couldn't be parsed. Please post a message on http://groups.google.com/group/antcat/, and we'll fix it!"
    self.author_names_string = author_names_string
    raise ActiveRecord::RecordInvalid.new self
  end
  author_names_and_suffix
end

- (Object) principal_author_last_name



35
# File 'app/models/reference.rb', line 35

def principal_author_last_name()  principal_author_last_name_cache end

- (Object) refresh_author_names_caches

update (including observed updates)



51
52
53
54
55
# File 'app/models/reference.rb', line 51

def refresh_author_names_caches(*)
  string, principal_author_last_name = make_author_names_caches
  update_attribute :author_names_string_cache, string
  update_attribute :principal_author_last_name_cache, principal_author_last_name
end

- (Object) replace_author_name(old_name, new_author_name)



12
13
14
15
16
17
18
19
# File 'app/models/reference_utility.rb', line 12

def replace_author_name old_name, new_author_name
  old_author_name = AuthorName.find_by_name old_name
  reference_author_name = reference_author_names.where(:author_name_id => old_author_name).first
  reference_author_name.author_name = new_author_name
  reference_author_name.save!
  author_names(true)
  refresh_author_names_caches
end

- (Object) short_citation_year



37
38
39
# File 'app/models/reference.rb', line 37

def short_citation_year
  citation_year.gsub %r{ .*$}, ''
end

- (Object) to_s

accessors



30
# File 'app/models/reference.rb', line 30

def to_s()                        "#{author_names_string} #{citation_year}. #{id}." end

- (Object) url



6
7
8
# File 'app/models/reference_has_document.rb', line 6

def url
  document.try :url
end