Module: ObservationsHelper

Includes:
RecordNavigationHelper
Defined in:
app/helpers/observations_helper.rb

Instance Method Summary collapse

Methods included from RecordNavigationHelper

for, #parent_records

Instance Method Details

#continuous_observation_cell_label(observation) ⇒ Object



97
98
99
# File 'app/helpers/observations_helper.rb', line 97

def continuous_observation_cell_label(observation)
  [observation.continuous_value, observation.continuous_unit].compact.join(' ')
end

#label_for_observation(observation) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/observations_helper.rb', line 20

def label_for_observation(observation)
  return nil if observation.nil?
  a = observation.descriptor.name
  b = observation_cell(observation, 'label', true)
  c = send(
    "label_for_#{observation.observation_object_type.underscore}",
    observation.observation_object
  )

  if b.empty?
    "#{a} on #{c}"
  else
    "#{a}: #{b} on #{c}"
  end
end

#next_records(observation) ⇒ Object

Returns !!Array!!.

Returns:

  • !!Array!!



144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/observations_helper.rb', line 144

def next_records(observation)
  # !! Note we only return observations on Otus currently.
  o = ::Observation
    .joins("JOIN otus ON observations.observation_object_type = 'Otu' AND observations.observation_object_id = otus.id")
    .where(project_id: observation.project_id)
    .where('observations.id > ?', observation.id)
    .order(:id)
    .first

  [o].compact
end

#observation_autocomplete_tag(observation, term = nil) ⇒ Object



16
17
18
# File 'app/helpers/observations_helper.rb', line 16

def observation_autocomplete_tag(observation, term = nil)
  mark_tag(observation_tag(observation), term)
end

#observation_cell(observation, label_or_tag, verbose = false) ⇒ Object



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
# File 'app/helpers/observations_helper.rb', line 46

def observation_cell(observation, label_or_tag, verbose = false)
  case observation.type
  when 'Observation::Qualitative'
    qualitative_observation_cell_label(observation, verbose)
  when 'Observation::Continuous'
    continuous_observation_cell_label(observation)
  when 'Observation::Sample'
    if label_or_tag == 'tag'
      sample_observation_cell_tag(observation)
    else
      # TODO
      ''
    end
  when 'Observation::PresenceAbsence'
    if label_or_tag == 'tag'
      presence_absence_observation_cell_tag(observation)
    else
      presence_absence_observation_cell_label(observation)
    end
  when 'Observation::Working' # TODO: Validate in format
    if label_or_tag == 'tag'
      tag.span('X', title: observation.description)
    else
      # TODO
      ''
    end
  when 'Observation::Media'
    # do nothing, the label comes from the image matrix
    ''
  else
    '!! display not done !!'
  end
end

#observation_made_on_tag(observation) ⇒ Object



80
81
82
83
84
85
86
87
# File 'app/helpers/observations_helper.rb', line 80

def observation_made_on_tag(observation)
  return nil if observation.nil?

  [observation.year_made,
   observation.month_made,
   observation.day_made,
   observation.time_made ].compact.join('-')
end

#observation_matrix_cell_tag(observation_object, descriptor) ⇒ Object

Joins multiple observations to concat for cells



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

def observation_matrix_cell_tag(observation_object, descriptor)
  q = Observation.object_scope(observation_object).where(descriptor: descriptor)
  q.collect{|o| observation_cell(o, 'tag')}.sort.join(' ').html_safe
end

#observation_tag(observation) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/observations_helper.rb', line 4

def observation_tag(observation)
  return nil if observation.nil?
  a = descriptor_tag(observation.descriptor)
  b = observation_cell(observation, 'tag', true)
  c = send(
    "#{observation.observation_object_type.underscore}_tag",
    observation.observation_object
  )

  "#{a}: #{b} on #{c}".html_safe
end

#observation_type_label(observation) ⇒ Object



36
37
38
# File 'app/helpers/observations_helper.rb', line 36

def observation_type_label(observation)
  observation.type.split('::').last
end

#presence_absence_observation_cell_label(observation) ⇒ Object



106
107
108
# File 'app/helpers/observations_helper.rb', line 106

def presence_absence_observation_cell_label(observation)
  observation.presence ? 'present' : 'absent'
end

#presence_absence_observation_cell_tag(observation) ⇒ Object



101
102
103
104
# File 'app/helpers/observations_helper.rb', line 101

def presence_absence_observation_cell_tag(observation)
  # TODO: messing with visualization here, do something more clean
  observation.presence ? '✓' : '❌'
end

#previous_records(observation) ⇒ Object

Returns !!Array!!.

Returns:

  • !!Array!!



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

def previous_records(observation)
  # !! Note we only return observations on Otus currently.
  o = ::Observation
    .joins("JOIN otus ON observations.observation_object_type = 'Otu' AND observations.observation_object_id = otus.id")
    .where(project_id: observation.project_id)
    .where('observations.id < ?', observation.id)
    .order(id: :desc)
    .first

  [o].compact
end

#qualitative_observation_cell_label(observation, verbose = false) ⇒ Object



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

def qualitative_observation_cell_label(observation, verbose = false)
  if verbose
    observation.character_state.label + ': ' + observation.character_state.name
  else
    observation.character_state.label
  end
end

#sample_observation_cell_tag(observation) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/helpers/observations_helper.rb', line 110

def sample_observation_cell_tag(observation)
  o = observation
  r = []

  r.push [o.sample_min, o.sample_max].compact.join('-')
  r.push "#{o.sample_units}" if o.sample_units.present?

  m = []

  m.push "median = #{o.sample_median}" if o.sample_median.present?
  m.push "&#956; = #{o.sample_mean}" if o.sample_mean.present?
  m.push ["s = #{o.sample_standard_error}", (o.sample_units.present? ? " #{o.sample_units}" : nil)].compact.join if o.sample_standard_error.present?
  m.push "n = #{o.sample_n}" if o.sample_n.present?
  m.push "&#963; = #{o.sample_standard_deviation}" if o.sample_standard_deviation.present?

  r.push '(' + m.join(', ') + ')' if m.any?

  r.compact.join(' ').html_safe
end