Module: SourcesHelper

Defined in:
app/helpers/sources_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_source_to_project_form(source) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/sources_helper.rb', line 93

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

#project_source_for_source(source) ⇒ Object



105
106
107
# File 'app/helpers/sources_helper.rb', line 105

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



51
52
53
54
# File 'app/helpers/sources_helper.rb', line 51

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/sources_helper.rb', line 63

def source_attributes_for(source)
  w = (: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_tag(source) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/sources_helper.rb', line 29

def source_author_year_tag(source)
  res = (:span, 'Author, year not yet provided for source.', class: [:feedback, 'feedback-thin', 'feedback-warning'])

  case source&.type
  when 'Source::Human'
    res = source.cached
  when 'Source::Bibtex'
    res = source.author_year if source.author_year.present?
  end

  res
end

#source_document_viewer_option_tag(source) ⇒ Object



56
57
58
59
60
61
# File 'app/helpers/sources_helper.rb', line 56

def source_document_viewer_option_tag(source)
  return nil if !source.documents.load.any?
  (:span, class: 'pdfviewerItem') do
    source.documents.collect{|d| (: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)


113
114
115
# File 'app/helpers/sources_helper.rb', line 113

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



117
118
119
120
121
# File 'app/helpers/sources_helper.rb', line 117

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

#source_in_project?(source) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/helpers/sources_helper.rb', line 109

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


46
47
48
49
# File 'app/helpers/sources_helper.rb', line 46

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



123
124
125
126
127
128
# File 'app/helpers/sources_helper.rb', line 123

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


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/sources_helper.rb', line 79

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

    end
  end
end

#source_tag(source) ⇒ Object



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

def source_tag(source)
  return nil if source.nil?
  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



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

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

  if term
    s = source.cached.gsub(/#{Regexp.escape(term)}/i, "<mark>#{term}</mark>") + ' ' # weee bit simpler
  else
    s = source.cached + ' '
  end

  if source.is_in_project?(sessions_current_project_id)
    s += ' ' + (:span, 'in', class: [:feedback, 'feedback-primary', 'feedback-thin']) 
    c = source.citations.where(project_id: sessions_current_project_id).count
    s += ' ' + ( c > 0 ? (:span, "#{c.to_s}&nbsp;#{'citations'.pluralize(c)}".html_safe, class: [:feedback, 'feedback-secondary', 'feedback-thin']) : '' )
    s += ' ' + (:span, 'doc/pdf', class: [:feedback, 'feedback-success', 'feedback-thin']) if source.documentation.where(project_id: sessions_current_project_id).any?
  else
    s += ' ' + (:span, 'out', class: [:feedback, 'feedback-warning', 'feedback-thin']) 
  end

  s.html_safe
end

#sources_search_formObject



42
43
44
# File 'app/helpers/sources_helper.rb', line 42

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