Skip to content

Instantly share code, notes, and snippets.

@marciojg
Created August 15, 2019 18:53
Show Gist options
  • Save marciojg/04d6537bebbbdb25bd860e2e565c2c8d to your computer and use it in GitHub Desktop.
Save marciojg/04d6537bebbbdb25bd860e2e565c2c8d to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class AutomaticBenefit < ApplicationRecord
include Activatable
attr_accessor :unit_id, :program_id, :teste
default_value_for :teste, value: false, allows_nil: false
belongs_to :material_resource
has_and_belongs_to_many :groups, before_add: :joga_erro2, before_remove: :joga_erro, after_remove: :joga_erro3
validates :material_resource, presence: true
validates :material_resource, active: true, on: :create
validates :quantity, presence: true, numericality: { greater_than: 0 }
validates :group_ids, presence: true
validate :groups_must_be_active, on: :create, if: :groups_present?
validate :material_resource_must_be_related_only_once_with_groups
# Parei tentando ver um modo de tratar as validações com problema no HABTM, ate agora, ele deleta, se array for vazio, add 1 de volta e retorna o erro.
# acho que nao tem como evitar a execução do delete. se nao der para evitar, recoloco. outro modo eh dar um rollback na class
before_save :ve_se_tem_erros
def joga_erro(group)
# byebug
# self.errors.add(:base, 'aaa1')
print 'passei aqui before_remove'
end
def joga_erro2(group)
print 'passei aqui before_add'
# self.errors.add(:base, 'aaa2')
end
def joga_erro3(group)
print 'passei aqui after_remove'
# self.groups = [group] if group_ids.blank?
# self.teste = true
# self.errors.add(:base, 'aaa2')
raise ActiveRecord::Rollback if self.roles.include? role
raise ActiveRecord::Rollback if group_ids.blank?
byebug
# has_and_belongs_to_many :people, before_add: :check_people_inside
# def check_people_inside person
# raise ActiveRecord::Rollback if people.count == 5
# end
# class User < ActiveRecord::Base
# has_and_belongs_to_many :roles, -> { uniq }
# end
# ?links
# https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
# https://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_associated
# https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
# https://bvnet.lbv.org.br/repo/projects/APSOC/repos/social/commits/b850a4858fa3e8f64e4b28ad815c51b0df4b24fa#Gemfile
# Parei aqui.. tentar fazer isso aqui https://guides.rubyonrails.org/association_basics.html#association-extensions
# ir la na tarefa que add o deferred para ver as issues
# https://stackoverflow.com/questions/38616387/how-to-validate-presence-of-associated-records-in-case-of-has-many-through-ass
end
def ve_se_tem_erros
byebug
errors.add(:group_ids, 'asdads') if self.teste
end
private
def groups_present?
groups.present?
end
def material_resource_must_be_related_only_once_with_groups
errors.add(:groups, :taken) if material_resource.present? && group_ids.present? && groups.select(&:active?).any? { |group| group.automatic_benefits.active.where('automatic_benefits.id != ?', id || -1).map(&:material_resource).select(&:active?).include?(material_resource) }
end
def groups_must_be_active
errors.add(:groups, :inactive) if groups.present? && groups.any?(&:inactive?)
end
end
# possivel solução para os casos de validar duplicidade
# Use the distinct method to keep the collection free of duplicates. This is mostly useful together with the :through option.
# https://guides.rubyonrails.org/association_basics.html#scopes-for-has-many-distinct
# has_and_belongs_to_many_depreciado, tem que tirar https://guides.rubyonrails.org/association_basics.html#additional-column-methods
# ===================
a = AutomaticBenefit.first
a.groups = []
a.save
a.groups = [Group.first]
a.save
reload!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment