Class: EntityTag

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BelongsToEntity
Defined in:
app/models/entity_tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_tag(params) ⇒ Object



19
20
21
22
23
24
# File 'app/models/entity_tag.rb', line 19

def create_tag(params)
  tag = Tag.with_label params[:label]
  result = where(:tag_id => tag.id, :entity_id => params[:entity_id].to_i, :entity_type => params[:entity_type]).first
  result = create :tag_id => tag.id, :entity_id => params[:entity_id].to_i, :entity_type => params[:entity_type] unless result
  result
end

.tagged_entity(params) ⇒ Object



26
27
28
# File 'app/models/entity_tag.rb', line 26

def tagged_entity(params)
  where(:tag_id => params[:id].to_i, :entity_id => params[:entity_id].to_i, :entity_type => params[:entity_type]).first
end

.tags_for(entity) ⇒ Object



30
31
32
33
# File 'app/models/entity_tag.rb', line 30

def tags_for(entity)
  tag_ids = where(:entity_id => entity.id, :entity_type => entity.entity_type).pluck(:tag_id)
  Tag.where(:id => tag_ids).order(:label).to_a
end

Instance Method Details

#posted_headerObject



6
7
8
9
10
# File 'app/models/entity_tag.rb', line 6

def posted_header
  if entity.kind_of?(Postable) && entity.posted_at
    "#{entity.posted_at.to_time.localtime.strftime "%m/%d/%Y"}:"
  end
end

#untag!Object



12
13
14
15
16
# File 'app/models/entity_tag.rb', line 12

def untag!
  destroy
  remaining = EntityTag.where(:tag_id => tag.id).count
  tag.destroy if remaining == 0
end