ActiveRecord::Base
(Not documented)
# File lib/app/models/annotation.rb, line 154 154: def self.create_multiple(params, separator) 155: success = true 156: annotations = [ ] 157: errors = [ ] 158: 159: annotatable = Annotation.find_annotatable(params[:annotatable_type], params[:annotatable_id]) 160: 161: if annotatable 162: values = params[:value] 163: 164: # Remove value from params hash 165: params.delete("value") 166: 167: values.split(separator).each do |val| 168: ann = Annotation.new(params) 169: ann.value = val.strip 170: 171: if ann.save 172: annotations << ann 173: else 174: error_text = "Error(s) occurred whilst saving annotation with attribute: '#{params[:attribute_name]}', and value: #{val} - #{ann.errors.full_messages.to_sentence}." 175: errors << error_text 176: logger.info(error_text) 177: success = false 178: end 179: end 180: else 181: errors << "Annotatable object doesn't exist" 182: success = false 183: end 184: 185: return [ success, annotations, errors ] 186: end
Helper class method to look up an annotatable object given the annotatable class name and ID.
# File lib/app/models/annotation.rb, line 121 121: def self.find_annotatable(annotatable_type, annotatable_id) 122: return nil if annotatable_type.nil? or annotatable_id.nil? 123: begin 124: return annotatable_type.constantize.find(annotatable_id) 125: rescue 126: return nil 127: end 128: end
Returns all the annotatable objects that have a specified attribute name and value.
NOTE (1): both the attribute name and the value will be treated case insensitively.
# File lib/app/models/annotation.rb, line 67 67: def self.find_annotatables_with_attribute_name_and_value(attribute_name, value) 68: return [ ] if attribute_name.blank? or value.nil? 69: 70: anns = Annotation.find(:all, 71: :joins => :attribute, 72: :conditions => { :annotation_attributes => { :name => attribute_name.strip.downcase }, 73: :value => value.strip.downcase }) 74: 75: return anns.map{|a| a.annotatable} 76: end
Same as the Annotation.find_annotatables_with_attribute_name_and_value method but takes in arrays for attribute names and values.
This allows you to build any combination of attribute names and values to search on. E.g. (1): Annotation.find_annotatables_with_attribute_names_and_values([ “tag” ], [ “fiction”, “sci-fi”, “fantasy” ]) E.g. (2): Annotation.find_annotatables_with_attribute_names_and_values([ “tag”, “keyword”, “category” ], [ “fiction”, “fantasy” ])
NOTE (1): the arguments to this method MUST be Arrays of Strings. NOTE (2): all attribute names and the values will be treated case insensitively.
# File lib/app/models/annotation.rb, line 87 87: def self.find_annotatables_with_attribute_names_and_values(attribute_names, values) 88: return [ ] if attribute_names.blank? or values.blank? 89: 90: anns = Annotation.find(:all, 91: :joins => :attribute, 92: :conditions => { :annotation_attributes => { :name => attribute_names }, 93: :value => values }) 94: 95: return anns.map{|a| a.annotatable} 96: end
Helper class method to look up a source object given the source class name and ID.
# File lib/app/models/annotation.rb, line 132 132: def self.find_source(source_type, source_id) 133: return nil if source_type.nil? or source_id.nil? 134: begin 135: return source_type.constantize.find(source_id) 136: rescue 137: return nil 138: end 139: end
(Not documented)
# File lib/app/models/annotation.rb, line 141 141: def attribute_name 142: self.attribute.name 143: end
Validations
# File lib/app/models/annotation.rb, line 218 218: def check_annotatable 219: if Annotation.find_annotatable(self.annotatable_type, self.annotatable_id).nil? 220: self.errors.add(:annotatable_id, "doesn't exist") 221: return false 222: else 223: return true 224: end 225: end
This method checks whether duplicates are allowed for this particular annotation type (ie: for the attribute that this annotation belongs to). If not, it checks for a duplicate existing annotation.
# File lib/app/models/annotation.rb, line 238 238: def check_duplicate 239: attr_name = self.attribute_name.downcase 240: if Annotations::Config.attribute_names_to_allow_duplicates.include?(attr_name) 241: return true 242: else 243: existing = Annotation.find(:all, 244: :joins => [ :attribute ], 245: :conditions => { :annotatable_type => self.annotatable_type, 246: :annotatable_id => self.annotatable_id, 247: :value => self.value, 248: :annotation_attributes => { :name => attr_name } }) 249: 250: if existing.length == 0 or existing[0].id == self.id 251: # It's all good... 252: return true 253: else 254: self.errors.add_to_base("This annotation already exists and is not allowed to be created again.") 255: return false 256: end 257: end 258: end
This method uses the ‘limits_per_source config’ setting to check whether a limit has been reached.
NOTE: this check is only carried out on new records, not records that are being updated.
# File lib/app/models/annotation.rb, line 263 263: def check_limit_per_source 264: attr_name = self.attribute_name.downcase 265: if self.new_record? and Annotations::Config::limits_per_source.has_key?(attr_name) 266: options = Annotations::Config::limits_per_source[attr_name] 267: max = options[0] 268: can_replace = options[1] 269: 270: unless (found_annotatable = Annotation.find_annotatable(self.annotatable_type, self.annotatable_id)).nil? 271: anns = found_annotatable.annotations_with_attribute_and_by_source(attr_name, self.source) 272: 273: if anns.length >= max 274: self.errors.add_to_base("The limit has been reached for annotations with this attribute and by this source.") 275: return false 276: else 277: return true 278: end 279: else 280: return true 281: end 282: else 283: return true 284: end 285: end
(Not documented)
# File lib/app/models/annotation.rb, line 227 227: def check_source 228: if Annotation.find_source(self.source_type, self.source_id).nil? 229: self.errors.add(:source_id, "doesn't exist") 230: return false 231: else 232: return true 233: end 234: end
(Not documented)
# File lib/app/models/annotation.rb, line 287 287: def check_value_restrictions 288: attr_name = self.attribute_name.downcase 289: value_to_check = self.value.downcase 290: if Annotations::Config::value_restrictions.has_key?(attr_name) 291: options = Annotations::Config::value_restrictions[attr_name] 292: 293: case options[:in] 294: when Array 295: if options[:in].map{|s| s.downcase}.include?(value_to_check) 296: return true 297: else 298: self.errors.add_to_base(options[:error_message]) 299: return false 300: end 301: 302: when Range 303: # Need to take into account that "a_string".to_i == 0 304: if value_to_check == "0" 305: if options[:in] === 0 306: return true 307: else 308: self.errors.add_to_base(options[:error_message]) 309: return false 310: end 311: else 312: if options[:in] === value_to_check.to_i 313: return true 314: else 315: self.errors.add_to_base(options[:error_message]) 316: return false 317: end 318: end 319: 320: else 321: return true 322: end 323: else 324: return true 325: end 326: end
(Not documented)
# File lib/app/models/annotation.rb, line 194 194: def process_value_adjustments 195: attr_name = self.attribute_name.downcase 196: # Make lowercase or uppercase if required 197: self.value.downcase! if Annotations::Config::attribute_names_for_values_to_be_downcased.include?(attr_name) 198: self.value.upcase! if Annotations::Config::attribute_names_for_values_to_be_upcased.include?(attr_name) 199: 200: # Apply strip text rules 201: Annotations::Config::strip_text_rules.each do |attr, strip_rules| 202: if attr_name == attr.downcase 203: if strip_rules.is_a? Array 204: strip_rules.each do |s| 205: self.value = self.value.gsub(s, '') 206: end 207: elsif strip_rules.is_a? String or strip_rules.is_a? Regexp 208: self.value = self.value.gsub(strip_rules, '') 209: end 210: end 211: end 212: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.