1require 'textacular/searchable' |
|
3class Tag < ActiveRecord::Base |
|
4 extend Searchable(:name, :description) |
|
5 include Browseable |
|
7 validates_presence_of :name |
|
8 validates_uniqueness_of :name |
|
9 validates_length_of :name, maximum: 50 |
|
10 validates_length_of :description, maximum: 200 |
|
12 facet :all, -> { by_name } |
|
14 after_destroy :remove_tag_from_petitions |
|
15 after_destroy :remove_tag_from_archived_petitions |
|
17 class << self |
|
18 def by_name |
|
19 order(name: :asc) |
|
20 end |
|
21 end |
|
23 private
|
|
|
25 def remove_tag_from_petitions |
26 Petition.tagged_with(id).update_all(["tags = array_remove(tags, ?)", id]) |
|
27 end |
|
|
29 def remove_tag_from_archived_petitions |
30 Archived::Petition.tagged_with(id).update_all(["tags = array_remove(tags, ?)", id]) |
|
31 end |
|
32end |