Class: CortexReaver::Tag
- Inherits:
-
Object
- Object
- CortexReaver::Tag
- Defined in:
- lib/cortex_reaver/model/tag.rb
Class Method Summary collapse
-
.autocomplete(string) ⇒ Object
Autocompletes a tag.
- .get(id) ⇒ Object
-
.refresh_counts ⇒ Object
Recalculates the number of children on each tag, and saves the updated values.
- .url ⇒ Object
Instance Method Summary collapse
- #atom_url ⇒ Object
-
#before_destroy ⇒ Object
When we delete a tag, ensure nothing else is linked to it.
-
#refresh_count ⇒ Object
Recalculates the number of children on this tag, and saves the update value.
- #to_s ⇒ Object
- #url ⇒ Object
- #validate ⇒ Object
Class Method Details
.autocomplete(string) ⇒ Object
Autocompletes a tag. Returns an array of matching candidates
31 32 33 |
# File 'lib/cortex_reaver/model/tag.rb', line 31 def self.autocomplete(string) filter(:title.like(/^#{string}/i)).limit(6).map(:title) end |
.get(id) ⇒ Object
45 46 47 |
# File 'lib/cortex_reaver/model/tag.rb', line 45 def self.get(id) self[:name => id] || self[id] end |
.refresh_counts ⇒ Object
Recalculates the number of children on each tag, and saves the updated values.
36 37 38 39 40 41 42 43 |
# File 'lib/cortex_reaver/model/tag.rb', line 36 def self.refresh_counts updated = [] order(:title).all.each do |tag| result = tag.refresh_count updated << [tag, result] if result end updated end |
.url ⇒ Object
49 50 51 |
# File 'lib/cortex_reaver/model/tag.rb', line 49 def self.url '/tags' end |
Instance Method Details
#atom_url ⇒ Object
53 54 55 |
# File 'lib/cortex_reaver/model/tag.rb', line 53 def atom_url '/tags/atom/' + name end |
#before_destroy ⇒ Object
When we delete a tag, ensure nothing else is linked to it.
20 21 22 23 24 25 26 27 28 |
# File 'lib/cortex_reaver/model/tag.rb', line 20 def before_destroy return false if super == false remove_all_photographs remove_all_journals remove_all_pages true end |
#refresh_count ⇒ Object
Recalculates the number of children on this tag, and saves the update value. Returns [old_count, new_count] if changed.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cortex_reaver/model/tag.rb', line 58 def refresh_count # Find counts old_count = self[:count] self[:count] = photographs_dataset.count + journals_dataset.count + pages_dataset.count # Save and return changed = changed_columns.include? :count self.save if changed [old_count, self[:count]] else nil end end |
#to_s ⇒ Object
80 81 82 |
# File 'lib/cortex_reaver/model/tag.rb', line 80 def to_s title || name end |
#url ⇒ Object
76 77 78 |
# File 'lib/cortex_reaver/model/tag.rb', line 76 def url '/tags/show/' + name end |
#validate ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/cortex_reaver/model/tag.rb', line 11 def validate validates_unique :name validates_presence :name validates_max_length 255, :name validates_presence :title validates_max_length 255, :title end |