Class: Tag

Inherits:
ApplicationRecord show all
Defined in:
app/models/tag.rb

Overview

== Schema Information

Table name: tags
Database name: primary

id :integer not null, primary key
name :string(255)
public :boolean default(FALSE), not null
slug :string
taggings_count :integer
created_at :datetime not null
updated_at :datetime not null

Indexes

index_tags_on_name (name) UNIQUE
index_tags_on_name_lower_unique (lower((name)::text)) UNIQUE
index_tags_on_public (public)
index_tags_on_slug_unique (slug) UNIQUE
index_tags_on_taggings_count (taggings_count)

Instance Attribute Summary collapse

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_scopes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#nameObject (readonly)



35
# File 'app/models/tag.rb', line 35

validates :name, presence: true

#slugObject (readonly)



37
# File 'app/models/tag.rb', line 37

validates :slug, uniqueness: true, allow_nil: true

Class Method Details

.by_taggable_typeActiveRecord::Relation<Tag>

A relation of Tags that are by taggable type. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Tag>)

See Also:



51
52
53
# File 'app/models/tag.rb', line 51

scope :by_taggable_type, ->(type) {
  joins(:taggings).where(taggings: { taggable_type: type }).distinct
}

.find_or_create_all_by_name(names) ⇒ Array<Tag>

Find or create multiple tags by name

Parameters:

  • names (Array<String>)

    The tag names to find or create

Returns:

  • (Array<Tag>)

    The found or newly created tags



84
85
86
# File 'app/models/tag.rb', line 84

def self.find_or_create_all_by_name(names)
  Array(names).filter_map { |name| find_or_create_by_name(name) }
end

.find_or_create_by_name(name) ⇒ Tag

Find or create a tag by name (case-insensitive)

Parameters:

  • name (String)

    The tag name to find or create

Returns:

  • (Tag)

    The found or newly created tag



71
72
73
74
75
76
77
78
79
# File 'app/models/tag.rb', line 71

def self.find_or_create_by_name(name)
  return nil if name.blank?

  normalized = name.to_s.strip.downcase
  find_by('LOWER(name) = ?', normalized) || create!(name: normalized)
rescue ActiveRecord::RecordNotUnique
  # Concurrent worker created the same tag between our find and create.
  find_by('LOWER(name) = ?', normalized)
end

.most_usedActiveRecord::Relation<Tag>

A relation of Tags that are most used. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Tag>)

See Also:



49
# File 'app/models/tag.rb', line 49

scope :most_used, -> { order(taggings_count: :desc) }

.orderedActiveRecord::Relation<Tag>

A relation of Tags that are ordered. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Tag>)

See Also:



48
# File 'app/models/tag.rb', line 48

scope :ordered, -> { order(:name) }

.public_tagsActiveRecord::Relation<Tag>

A relation of Tags that are public tags. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Tag>)

See Also:



50
# File 'app/models/tag.rb', line 50

scope :public_tags, -> { where(public: true) }

.ransackable_associations(_auth_object = nil) ⇒ Object



60
61
62
# File 'app/models/tag.rb', line 60

def self.ransackable_associations(_auth_object = nil)
  %w[taggings]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object

Ransack configuration



56
57
58
# File 'app/models/tag.rb', line 56

def self.ransackable_attributes(_auth_object = nil)
  %w[id name slug public taggings_count created_at updated_at]
end

.ransortable_attributes(_auth_object = nil) ⇒ Object



64
65
66
# File 'app/models/tag.rb', line 64

def self.ransortable_attributes(_auth_object = nil)
  %w[name taggings_count created_at updated_at]
end

.searchActiveRecord::Relation<Tag>

A relation of Tags that are search. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Tag>)

See Also:



43
44
45
46
47
# File 'app/models/tag.rb', line 43

scope :search, ->(query) {
  return all if query.blank?

  where('LOWER(name) LIKE LOWER(?)', "%#{sanitize_sql_like(query)}%")
}

Instance Method Details

#activity_typesActiveRecord::Relation<ActivityType>

Returns:

See Also:



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

has_many :activity_types, through: :taggings, source: :taggable, source_type: 'ActivityType'

#articlesActiveRecord::Relation<Article>

Polymorphic associations through taggings

Returns:

  • (ActiveRecord::Relation<Article>)

See Also:



27
# File 'app/models/tag.rb', line 27

has_many :articles, through: :taggings, source: :taggable, source_type: 'Article'

#digital_assetsActiveRecord::Relation<DigitalAsset>

Returns:

See Also:



29
# File 'app/models/tag.rb', line 29

has_many :digital_assets, through: :taggings, source: :taggable, source_type: 'DigitalAsset'

#floor_plan_displaysActiveRecord::Relation<FloorPlanDisplay>

Returns:

See Also:



30
# File 'app/models/tag.rb', line 30

has_many :floor_plan_displays, through: :taggings, source: :taggable, source_type: 'FloorPlanDisplay'

#itemsActiveRecord::Relation<Item>

Returns:

  • (ActiveRecord::Relation<Item>)

See Also:



31
# File 'app/models/tag.rb', line 31

has_many :items, through: :taggings, source: :taggable, source_type: 'Item'

#product_linesActiveRecord::Relation<ProductLine>

Returns:

See Also:



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

has_many :product_lines, through: :taggings, source: :taggable, source_type: 'ProductLine'

#showcasesActiveRecord::Relation<Showcase>

Returns:

See Also:



28
# File 'app/models/tag.rb', line 28

has_many :showcases, through: :taggings, source: :taggable, source_type: 'Showcase'

#taggingsActiveRecord::Relation<Tagging>

Returns:

  • (ActiveRecord::Relation<Tagging>)

See Also:



24
# File 'app/models/tag.rb', line 24

has_many :taggings, dependent: :destroy

#to_paramObject



103
104
105
# File 'app/models/tag.rb', line 103

def to_param
  slug || id.to_s
end

#to_sObject



99
100
101
# File 'app/models/tag.rb', line 99

def to_s
  name
end

#usage_by_typeHash

Get usage count by taggable type

Returns:

  • (Hash)

    Hash of taggable_type => count



90
91
92
# File 'app/models/tag.rb', line 90

def usage_by_type
  taggings.group(:taggable_type).count
end

#usage_countObject

Total usage count (alternative to taggings_count for accuracy)



95
96
97
# File 'app/models/tag.rb', line 95

def usage_count
  taggings_count || taggings.count
end