module Tools::WithPoliticiansTags

Métodos comunes para los contenidos que tienen asigando uno o varios políticos a través de los tags.

Public Class Methods

included(base) click to toggle source
# File app/models/tools/with_politicians_tags.rb, line 28
def self.included(base)
  base.validate :politicians_names_are_valid    
end

Public Instance Methods

politician_ids() click to toggle source
# File app/models/tools/with_politicians_tags.rb, line 86
def politician_ids
  # tags = self.politicians_tags.map {|ptag| ptag.kind_info.to_i}
  tags = politicians.collect(&:id)
end
politicians() click to toggle source
# File app/models/tools/with_politicians_tags.rb, line 73
def politicians
  tags = self.politicians_tags
  politicians = []
  if tags.present?
    # Need to do separate finds if we want politicians to appear in the order they were introduced
    # Politician.find(:all, :conditions => {"id" => tags.map {|t| t.kind_info}})
    tags.each do |tag|
      politicians << Politician.find(tag.kind_info)
    end
  end
  politicians
end
politicians_names_are_valid() click to toggle source
# File app/models/tools/with_politicians_tags.rb, line 67
def politicians_names_are_valid
  if @politicians_errors.present?
    errors.add(:politicians_tag_list, @politicinas_error)
  end
end
politicians_tag_list() click to toggle source
# File app/models/tools/with_politicians_tags.rb, line 36
def politicians_tag_list
  @politicians_tag_list || tags.public.politicians.map {|t| t.name}.join(", ")
end
politicians_tag_list=(tag_names) click to toggle source
# File app/models/tools/with_politicians_tags.rb, line 40
def politicians_tag_list=(tag_names)
  # asignamos a la instance variable el valor que ha puesto el usuario para volver a mostrarlo si hay errores.
  @politicians_tag_list = tag_names 
  
  valid_names = true

  # Cogemos todos los tags menos los que corresponden a un político
  self.taggings = self.taggings.map {|t| t unless t.tag.kind.eql?('Político')}.compact
  
  logger.info "Assigns politicians tags for #{tag_names}"
  # Asignamos los tags nuevos
  tag_names.split(", ").each do |name_es|
    if t = Tag.politicians.find_by_name_es(name_es)
      self.taggings.build(:tag_id => t.id)
    else
      @politicians_errors = "No existe político con nombre #{name_es}"
      valid_names = false
    end
  end

  @politicians_tag_list
end
politicians_tags() click to toggle source
# File app/models/tools/with_politicians_tags.rb, line 32
def politicians_tags
  tags.public.politicians
end
public_tags_without_politicians() click to toggle source
# File app/models/tools/with_politicians_tags.rb, line 63
def public_tags_without_politicians
  self.tags.public.find(:all, :conditions => "((kind IS NULL) OR (kind != 'Político'))")
end