Created
November 28, 2016 23:25
-
-
Save Fernan2/e8b0f7995e164475c7c01dd1e923129b to your computer and use it in GitHub Desktop.
Tematizable concern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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