Skip to content

Instantly share code, notes, and snippets.

@Fernan2
Created November 28, 2016 23:25
Show Gist options
  • Select an option

  • Save Fernan2/e8b0f7995e164475c7c01dd1e923129b to your computer and use it in GitHub Desktop.

Select an option

Save Fernan2/e8b0f7995e164475c7c01dd1e923129b to your computer and use it in GitHub Desktop.
Tematizable concern
require 'active_support/concern'
module Tematizable
extend ActiveSupport::Concern
included do
has_many :tematizaciones, as: :tematizable, class_name: 'Tematizacion'
has_many :tematicas, through: :tematizaciones
end
def fijar_tematicas!(ids_tematicas, grupo = '')
tematizaciones.where(tematizable_grupo: grupo).where.not(tematica_id: ids_tematicas).delete_all
(ids_tematicas || []).each { |tematica_id| asignar_tematica!(tematica_id, grupo) }
end
def asignar_tematica!(tematica_id, grupo = '')
busca_tematica(tematica_id, grupo) || tematizaciones.create!(tematica_id: tematica_id, tematizable_grupo: grupo)
end
def desasignar_tematica!(tematica_id, grupo = '')
busca_tematica(tematica_id, grupo).try(:destroy)
end
def busca_tematica(tematica_id, grupo)
tematizaciones.where(tematica_id: tematica_id, tematizable_grupo: grupo).first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment