Module: SourcesHelper

Defined in:
app/helpers/sources_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_source_to_project_form(source) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'app/helpers/sources_helper.rb', line 127

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


80
81
82
83
# File 'app/helpers/sources_helper.rb', line 80

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 <i> 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



139
140
141
# File 'app/helpers/sources_helper.rb', line 139

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) ⇒ Object



85
86
87
88
# File 'app/helpers/sources_helper.rb', line 85

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

#source_attributes_for(source) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/sources_helper.rb', line 97

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



47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/sources_helper.rb', line 47

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.present? ? source.author_year : not_provided
  else
    not_provided
  end
end

#source_author_year_tag(source) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/sources_helper.rb', line 59

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.present? ? source.author_year : 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



178
179
180
181
182
183
184
185
186
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
# File 'app/helpers/sources_helper.rb', line 178

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



90
91
92
93
94
95
# File 'app/helpers/sources_helper.rb', line 90

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})}.join.html_safe
  end.html_safe
end

#source_in_other_project?(source) ⇒ Boolean

Returns:

  • (Boolean)


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

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



151
152
153
154
155
# File 'app/helpers/sources_helper.rb', line 151

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)


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

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


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

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



157
158
159
160
161
162
163
# File 'app/helpers/sources_helper.rb', line 157

def source_nomenclature_tag(source, topics)
  t = [tag.span(source_tag(source))]
  t.push [':', topic_list_tag(topics).html_safe] if !topics.blank?
  t.push radial_annotator(source)
  t.push radial_navigation_tag(source)
  t.flatten.compact.join(' ').html_safe
end


113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/sources_helper.rb', line 113

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
40
41
42
43
# File 'app/helpers/sources_helper.rb', line 16

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

  if term
    s = regex_mark_tag(source.cached, term) + ' '
  else
    s = source.cached + ' '
  end

  # 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.to_s}&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.to_s}&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



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

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