47 lines
1.1 KiB
Ruby
47 lines
1.1 KiB
Ruby
class ModuleTag
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
|
|
field :is_default, type: Boolean, default: false
|
|
field :name, localize: true
|
|
|
|
has_one :tag, as: :tag_lease, autosave: true
|
|
belongs_to :module_app
|
|
|
|
belongs_to :parent, :class_name => 'ModuleTag', :inverse_of => :children
|
|
has_many :children, :class_name => 'ModuleTag', :inverse_of => :parent, autosave: true, dependent: :destroy
|
|
|
|
validates :name, :at_least_one => true
|
|
|
|
before_save :init_update_tag
|
|
before_destroy :destroy_tag
|
|
|
|
after_save :update_children
|
|
|
|
private
|
|
|
|
def destroy_tag
|
|
Tag.without_callback(:destroy, :before, :destroy_module_tag) do
|
|
self.tag.destroy
|
|
end
|
|
end
|
|
|
|
def init_update_tag
|
|
if self.new_record?
|
|
self.build_tag(name_translations: self.name_translations, is_default: self.is_default)
|
|
else
|
|
self.tag.name_translations = self.name_translations
|
|
self.tag.is_default = self.is_default
|
|
end
|
|
end
|
|
|
|
def update_children
|
|
unless self.children.blank?
|
|
self.children.each do |child|
|
|
child.update_attributes(name_translations: self.name_translations)
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|