Class: CortexReaver::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/cortex_reaver/model/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

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_countsObject

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

.urlObject



49
50
51
# File 'lib/cortex_reaver/model/tag.rb', line 49

def self.url
  '/tags'
end

Instance Method Details

#atom_urlObject



53
54
55
# File 'lib/cortex_reaver/model/tag.rb', line 53

def atom_url
  '/tags/atom/' + name
end

#before_destroyObject

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_countObject

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_sObject



80
81
82
# File 'lib/cortex_reaver/model/tag.rb', line 80

def to_s
  title || name
end

#urlObject



76
77
78
# File 'lib/cortex_reaver/model/tag.rb', line 76

def url
  '/tags/show/' + name
end

#validateObject



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