Class: Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.existingObject



32
33
34
# File 'app/models/tag.rb', line 32

def existing
  order(:label).to_a
end

.with_label(label) ⇒ Object



36
37
38
39
40
# File 'app/models/tag.rb', line 36

def with_label(label)
  tag = where(:label => label).first
  tag = create :label => label unless tag
  tag
end

Instance Method Details

#previewable_url(preview) ⇒ Object



4
5
6
7
8
9
10
# File 'app/models/tag.rb', line 4

def previewable_url(preview)
  if preview
    "/admin/tags/#{id}"
  else
    "/tags/#{id}"
  end
end

#shown_entity_tags(preview) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/tag.rb', line 12

def shown_entity_tags(preview)
  @shown_entity_tags ||= entity_tags.to_a.select do |entity_tag|
    preview || !entity_tag.entity.kind_of?(Postable) || entity_tag.entity.posted?
  end.sort do |a, b|
    if a.entity.kind_of?(Postable) && a.entity.posted_at && b.entity.kind_of?(Postable) && b.entity.posted_at
      b.entity.posted_at.to_date <=> a.entity.posted_at.to_date
    elsif a.entity.kind_of?(Postable) && a.entity.posted_at
      1
    elsif b.entity.kind_of?(Postable) && b.entity.posted_at
      -1
    else
      a.description <=> b.description
    end
  end

  raise ActiveRecord::RecordNotFound.new("No records found!") if @shown_entity_tags.empty?
  @shown_entity_tags
end