module Tools::WithAreaTag

Métodos comunes para los contenidos que tienen asignado un área a través del tag del área .

Attributes

area_id[RW]
area_tags[RW]

Public Class Methods

included(base) click to toggle source
# File app/models/tools/with_area_tag.rb, line 31
def self.included(base)
  base.after_save :sync_comments_area_tags
  base.before_save :save_tag_list_and_area_tag_list
end

Public Instance Methods

area() click to toggle source

Si hay más de un área que corresponde al documento, usamos el órden de las áreas y cogemos el primero.

# File app/models/tools/with_area_tag.rb, line 44
def area
  self.areas.first
end
area_id=(aid) click to toggle source
# File app/models/tools/with_area_tag.rb, line 48
def area_id=(aid)
  # guardamos el valor nuevo por si sale un error al guardar los datos y hay que volver al formulario    
  if aid.present?
    @area_id = aid.to_i
  
    # borrar el área anterior
    if self.area.present?
      current_area_tag = self.area.area_tag
      self.taggings = self.taggings.map {|t| t unless t.tag_id.eql?(current_area_tag.id)}.compact
    end
  
    # añadir el área nuevo. Puede estar en blanco
    if Area.exists?(aid)
      new_area_tag = Area.find(aid).area_tag 
      self.taggings.build(:tag_id => new_area_tag.id)
    end
    @area_id
  end
end
area_ids() click to toggle source

def #area_tags=(atags)

logger.info "------------ #{atags}"
@area_tags = atags

end

# File app/models/tools/with_area_tag.rb, line 89
def area_ids
  areas.collect(&:id)
end
area_name() click to toggle source
# File app/models/tools/with_area_tag.rb, line 77
def area_name
  self.area.present? ? self.area.name : ''
end
areas() click to toggle source

El área de un documento se coge de los tags de áreas que este tiene. Se ordenan de acuerdo con el orden oficial de las áreas

# File app/models/tools/with_area_tag.rb, line 38
def areas
  Tagging.find(:all, :conditions => {:taggable_type => 'Area', :tag_id => self.tag_ids}).map {|t| t.taggable}.sort {|a,b| a.position <=> b.position}
end

Protected Instance Methods

save_tag_list_and_area_tag_list() click to toggle source
# File app/models/tools/with_area_tag.rb, line 117
def save_tag_list_and_area_tag_list
  if @area_tags || @tag_list_without_areas
    tags = []
    tags << @area_tags if @area_tags
    tags << @tag_list_without_areas.split(',') if @tag_list_without_areas
    self.tag_list = TagList.from(tags).uniq.join(',')
  end
end
sync_comments_area_tags() click to toggle source
# File app/models/tools/with_area_tag.rb, line 94
def sync_comments_area_tags
  # logger.info "respond_to? :comments: #{self.respond_to?(:comments)}"
  if self.respond_to?(:comments)
    # logger.info "Added: #{self.added_tags.inspect}. Removed: #{self.removed_tags.inspect}"
    every_area_tag_names = Area.tags.collect {|a| a.name_es}
    to_add = (self.added_tags & every_area_tag_names)
    to_remove = (self.removed_tags & every_area_tag_names)
    if to_add.length > 0 || to_remove.length > 0
      self.comments.each do |comment|
        if to_add.length > 0
          # logger.info "Going to add tag areas #{to_add.join(',')} to comment #{comment.id}" 
          comment.tag_list.add to_add.join(',')
        end
        if to_remove.length > 0
          # logger.info "Going to remove tag areas #{to_remove.join(',')} from comment #{comment.id}"
          comment.tag_list.remove to_remove.join(',')
        end
        comment.save!
      end
    end
  end
end