Class: Annotation

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
AnnotationsVersionFu
Defined in:
lib/app/models/annotation.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AnnotationsVersionFu

included

Class Method Details

.create_multiple(params, separator) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/app/models/annotation.rb', line 159

def self.create_multiple(params, separator)
  success = true
  annotations = [ ]
  errors = [ ]
  
  annotatable = Annotation.find_annotatable(params[:annotatable_type], params[:annotatable_id])
  
  if annotatable
    values = params[:value]
    
    # Remove value from params hash
    params.delete("value")
    
    values.split(separator).each do |val|
      ann = Annotation.new(params)
      ann.value = val.strip
      
      if ann.save
        annotations << ann
      else
        error_text = "Error(s) occurred whilst saving annotation with attribute: '#{params[:attribute_name]}', and value: #{val} - #{ann.errors.full_messages.to_sentence}." 
        errors << error_text
        logger.info(error_text)
        success = false
      end
    end
  else
    errors << "Annotatable object doesn't exist"
    success = false
  end
   
  return [ success, annotations, errors ]
end

.find_annotatable(annotatable_type, annotatable_id) ⇒ Object

Helper class method to look up an annotatable object given the annotatable class name and ID.



119
120
121
122
123
124
125
126
# File 'lib/app/models/annotation.rb', line 119

def self.find_annotatable(annotatable_type, annotatable_id)
  return nil if annotatable_type.nil? or annotatable_id.nil?
  begin
    return annotatable_type.constantize.find(annotatable_id)
  rescue
    return nil
  end
end

.find_source(source_type, source_id) ⇒ Object

Helper class method to look up a source object given the source class name and ID.



130
131
132
133
134
135
136
137
# File 'lib/app/models/annotation.rb', line 130

def self.find_source(source_type, source_id)
  return nil if source_type.nil? or source_id.nil?
  begin
    return source_type.constantize.find(source_id)
  rescue
    return nil
  end
end

Instance Method Details

#attribute_nameObject



139
140
141
# File 'lib/app/models/annotation.rb', line 139

def attribute_name
  self.attribute.name
end

#attribute_name=(attr_name) ⇒ Object



143
144
145
146
# File 'lib/app/models/annotation.rb', line 143

def attribute_name=(attr_name)
  attr_name = attr_name.to_s.strip
  self.attribute = AnnotationAttribute.find_or_create_by_name(attr_name)
end

#original_set_value=Object



148
# File 'lib/app/models/annotation.rb', line 148

alias_method :original_set_value=, :value=

#value=(value_in) ⇒ Object



149
150
151
152
153
# File 'lib/app/models/annotation.rb', line 149

def value=(value_in)
  # Store this raw value in a temporary variable for 
  # later processing before the object is saved.
  @raw_value = value_in
end

#value_contentObject



155
156
157
# File 'lib/app/models/annotation.rb', line 155

def value_content
  self.value.nil? ? "" : self.value.ann_content
end