Module: SourcesHelper

Defined in:
app/helpers/sources_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_source_to_project_form(source) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/sources_helper.rb', line 130

def add_source_to_project_form(source)
  if !source_in_project?(source)
    form_for(ProjectSource.new(source_id: source.to_param, project_id: sessions_current_project_id), remote: true) do |f|
      f.hidden_field(:source_id) +
        f.hidden_field(:project_id) +
        f.submit('Add to project', data: { 'source-to-project': source.id.to_s }, class: 'button-submit')
    end
  else
    button_to('Remove from project', project_source_path(project_source_for_source(source)), method: :delete, remote: true,  data: { 'source-to-project': source.id.to_s }, class: 'button-delete')
  end
end


76
77
78
79
# File 'app/helpers/sources_helper.rb', line 76

def history_link(source)
  tag.em(' in ') + link_to(tag.span(source_author_year_tag(source), title: source.cached, class: :history__in), send(:nomenclature_by_source_task_path, source_id: source.id) )
  #        return content_tag(:span,  content_tag(:em, ' in ') + b, class: [:history__in])
end

#label_for_source(source) ⇒ Object

This is an exception to the no HTML currently in that it returns curator supplied tags (they are the only allowed



11
12
13
14
# File 'app/helpers/sources_helper.rb', line 11

def label_for_source(source)
  return nil if source.nil?
  source.cached
end

#project_source_for_source(source) ⇒ Object



142
143
144
# File 'app/helpers/sources_helper.rb', line 142

def project_source_for_source(source)
  ProjectSource.find_by(source_id: source.to_param, project_id: sessions_current_project_id)
end

#short_sources_tag(sources) ⇒ String

Returns no HTML.

Returns:

  • (String)

    no HTML



82
83
84
85
# File 'app/helpers/sources_helper.rb', line 82

def short_sources_tag(sources)
  return nil if !sources.load.any?
  sources.collect{|s| source_author_year_tag(s) }.join('; ')
end

#short_sources_year_tag(sources) ⇒ String

Returns no HTML.

Returns:

  • (String)

    no HTML



88
89
90
91
# File 'app/helpers/sources_helper.rb', line 88

def short_sources_year_tag(sources)
  return nil if !sources.load.any?
  sources.collect{|s| s.year }.compact.sort.join('; ')
end

#source_attributes_for(source) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/helpers/sources_helper.rb', line 100

def source_attributes_for(source)
  w = tag.em('ERROR, unkown class of Source, contact developers', class: :warning)
  content_for :attributes do
    case source.class.name
    when 'Source::Bibtex'
      render '/sources/bibtex/attributes'
    when 'Source::Verbatim'
      render '/sources/verbatim/attributes'
    when 'Source::Source'
      w
    else
      w
    end
  end
end

#source_author_year_label(source) ⇒ String

Returns No HTML, no nil.

Returns:

  • (String)

    No HTML, no nil



43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/sources_helper.rb', line 43

def source_author_year_label(source)
  not_provided = 'Author, year not yet provided for source.'
  case source&.type
  when 'Source::Human'
    source.cached
  when 'Source::Bibtex'
    (source.author_year.presence || not_provided)
  else
    not_provided
  end
end

#source_author_year_tag(source) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/sources_helper.rb', line 55

def source_author_year_tag(source)
  not_provided = 'Author, year not yet provided for source.'
  case source&.type
  when 'Source::Human'
    source.cached
  when 'Source::Bibtex'
    (source.author_year.presence || not_provided)
  else
    tag.span(not_provided, class: [:feedback, 'feedback-thin', 'feedback-warning'])
  end
end

#source_citation_totals(sources) ⇒ Object

Returns Hash a vectorized count of totals per source.

Returns:

  • Hash a vectorized count of totals per source



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/helpers/sources_helper.rb', line 187

def source_citation_totals(sources)
  data = {}
  max_total = 0

  Citation.related_klasses.each do |k|
    row = []
    sources.each do |s|
      row.push Citation.where(project_id: sessions_current_project_id, source: s, citation_object_type: k).count 
    end
    if row.sum > 0
      data[k] = row
      max_total = row.sum if max_total < row.sum
    end
  end

  ids_missing_data = []
 
  sources.each_with_index do |s, i| 
    has_data = false
    data.each do |k, v|
      if data[k][i] > 0
        has_data = true
        break
      end
    end
    ids_missing_data.push(s.id) unless has_data
  end

  return {
    data: data,
    metadata: {
      ids_missing_data: ids_missing_data,
      max_total: max_total
    }
  }

end

#source_document_viewer_option_tag(source) ⇒ Object



93
94
95
96
97
98
# File 'app/helpers/sources_helper.rb', line 93

def source_document_viewer_option_tag(source)
  return nil if !source.documents.load.any?
  tag.span(class: 'pdfviewerItem flexbox gap-small') do
    source.documents.collect{|d| tag.a('View', class: 'circle-button', data: { pdfviewer: d.document_file(:original, false), sourceid: source.id}) if d.document_file_content_type == 'application/pdf'}.join.html_safe
  end.html_safe
end

#source_in_other_project?(source) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'app/helpers/sources_helper.rb', line 150

def source_in_other_project?(source)
  source.project_sources.where.not(project_id: sessions_current_project_id).references(:projects_sources).any?
end

#source_in_other_project_tag(object) ⇒ Object



154
155
156
157
158
# File 'app/helpers/sources_helper.rb', line 154

def source_in_other_project_tag(object)
  if source_in_other_project?(object)
    tag.h3('This source is used in another project.', class: :warning)
  end
end

#source_in_project?(source) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'app/helpers/sources_helper.rb', line 146

def source_in_project?(source)
  ProjectSource.exists?(project_id: sessions_current_project_id, source_id: source.to_param)
end


71
72
73
74
# File 'app/helpers/sources_helper.rb', line 71

def source_link(source)
  return nil if source.nil?
  link_to(source_tag(source).html_safe, source.metamorphosize )
end

#source_nomenclature_tag(source, topics) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/helpers/sources_helper.rb', line 160

def source_nomenclature_tag(source, topics)
  t = [tag.span(source_tag(source))]
  t.push [':', topic_list_tag(topics).html_safe] if topics.present?

  (:div, 
    t.flatten.compact.join(' ').html_safe + 
    (:div,
      (:div, 
        radial_annotator(source) + 
        radial_navigation_tag(source), class: 'flex-row gap-small'
      ), class: 'd-inline-block margin-small-left')
    )
end


116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/helpers/sources_helper.rb', line 116

def source_related_attributes(source)
  content_for :related_attributes do
    if source.class.name == 'Source::Bibtex'
      tag.h3( 'Authors') do
        tag.ul do
          source.authors.collect{|a| tag.li(a.last_name)}
        end
      end
    else

    end
  end
end

#source_tag(source) ⇒ Object



3
4
5
6
7
# File 'app/helpers/sources_helper.rb', line 3

def source_tag(source)
  return nil if source.nil?
  # TODO: sanitize should happen at the model validation level, not here!
  source.cached ? sanitize(source.cached, tags: ['i']).html_safe : (source.new_record? ? nil : 'ERROR - Source cache not set, please notify admin.')
end

#sources_autocomplete_tag(source, term) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/sources_helper.rb', line 16

def sources_autocomplete_tag(source, term)
  return nil if source.nil?

  s = mark_tag(source.cached, term, html_safe: false) + ' '

  # In project is the project_if if present in this project see lib/queries/source/autocomplete
  if source.respond_to?(:in_project) && !source.in_project.nil?
    s += ' ' + tag.span('in', class: [:feedback, 'feedback-primary', 'feedback-thin'])
    c = source.use_count
    s += ' ' + ( c > 0 ? tag.span("#{c}&nbsp;#{'citations'.pluralize(c)}".html_safe, class: [:feedback, 'feedback-secondary', 'feedback-thin']) : '' )
    s += ' ' + tag.span('doc/pdf', class: [:feedback, 'feedback-success', 'feedback-thin']) if source.documentation.where(project_id: sessions_current_project_id).any?
  elsif source.is_in_project?(sessions_current_project_id)
    s += ' ' + tag.span('in', class: [:feedback, 'feedback-primary', 'feedback-thin']) 
    c = source.citations.where(project_id: sessions_current_project_id).count
    s += ' ' + ( c > 0 ? tag.span("#{c}&nbsp;#{'citations'.pluralize(c)}".html_safe, class: [:feedback, 'feedback-secondary', 'feedback-thin']) : '' )
    s += ' ' + tag.span('doc/pdf', class: [:feedback, 'feedback-success', 'feedback-thin']) if source.documentation.where(project_id: sessions_current_project_id).any?
  else
    s += ' ' + tag.span('out', class: [:feedback, 'feedback-warning', 'feedback-thin']) 
  end

  s += ' ' + tag.span(label_for_language(source.source_language), class: [:feedback, 'feedback-secondary', 'feedback-thin']) if source.language_id.present?

  s.html_safe
end

#sources_search_formObject



67
68
69
# File 'app/helpers/sources_helper.rb', line 67

def sources_search_form
  render('/sources/quick_search_form')
end