Class: Tag
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Tag
- 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
- #name ⇒ Object readonly
- #slug ⇒ Object readonly
Has many collapse
- #activity_types ⇒ ActiveRecord::Relation<ActivityType>
-
#articles ⇒ ActiveRecord::Relation<Article>
Polymorphic associations through taggings.
- #digital_assets ⇒ ActiveRecord::Relation<DigitalAsset>
- #floor_plan_displays ⇒ ActiveRecord::Relation<FloorPlanDisplay>
- #items ⇒ ActiveRecord::Relation<Item>
- #product_lines ⇒ ActiveRecord::Relation<ProductLine>
- #showcases ⇒ ActiveRecord::Relation<Showcase>
- #taggings ⇒ ActiveRecord::Relation<Tagging>
Class Method Summary collapse
-
.by_taggable_type ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are by taggable type.
-
.find_or_create_all_by_name(names) ⇒ Array<Tag>
Find or create multiple tags by name.
-
.find_or_create_by_name(name) ⇒ Tag
Find or create a tag by name (case-insensitive).
-
.most_used ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are most used.
-
.ordered ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are ordered.
-
.public_tags ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are public tags.
- .ransackable_associations(_auth_object = nil) ⇒ Object
-
.ransackable_attributes(_auth_object = nil) ⇒ Object
Ransack configuration.
- .ransortable_attributes(_auth_object = nil) ⇒ Object
-
.search ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are search.
Instance Method Summary collapse
- #to_param ⇒ Object
- #to_s ⇒ Object
-
#usage_by_type ⇒ Hash
Get usage count by taggable type.
-
#usage_count ⇒ Object
Total usage count (alternative to taggings_count for accuracy).
Methods inherited from ApplicationRecord
ransackable_scopes, #to_relation
Methods included from Models::EventPublishable
Instance Attribute Details
#name ⇒ Object (readonly)
35 |
# File 'app/models/tag.rb', line 35 validates :name, presence: true |
#slug ⇒ Object (readonly)
37 |
# File 'app/models/tag.rb', line 37 validates :slug, uniqueness: true, allow_nil: true |
Class Method Details
.by_taggable_type ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are by taggable type. Active Record Scope
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
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)
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_used ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are most used. Active Record Scope
49 |
# File 'app/models/tag.rb', line 49 scope :most_used, -> { order(taggings_count: :desc) } |
.ordered ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are ordered. Active Record Scope
48 |
# File 'app/models/tag.rb', line 48 scope :ordered, -> { order(:name) } |
.public_tags ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are public tags. Active Record Scope
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 |
.search ⇒ ActiveRecord::Relation<Tag>
A relation of Tags that are search. Active Record Scope
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_types ⇒ ActiveRecord::Relation<ActivityType>
33 |
# File 'app/models/tag.rb', line 33 has_many :activity_types, through: :taggings, source: :taggable, source_type: 'ActivityType' |
#articles ⇒ ActiveRecord::Relation<Article>
Polymorphic associations through taggings
27 |
# File 'app/models/tag.rb', line 27 has_many :articles, through: :taggings, source: :taggable, source_type: 'Article' |
#digital_assets ⇒ ActiveRecord::Relation<DigitalAsset>
29 |
# File 'app/models/tag.rb', line 29 has_many :digital_assets, through: :taggings, source: :taggable, source_type: 'DigitalAsset' |
#floor_plan_displays ⇒ ActiveRecord::Relation<FloorPlanDisplay>
30 |
# File 'app/models/tag.rb', line 30 has_many :floor_plan_displays, through: :taggings, source: :taggable, source_type: 'FloorPlanDisplay' |
#items ⇒ ActiveRecord::Relation<Item>
31 |
# File 'app/models/tag.rb', line 31 has_many :items, through: :taggings, source: :taggable, source_type: 'Item' |
#product_lines ⇒ ActiveRecord::Relation<ProductLine>
32 |
# File 'app/models/tag.rb', line 32 has_many :product_lines, through: :taggings, source: :taggable, source_type: 'ProductLine' |
#showcases ⇒ ActiveRecord::Relation<Showcase>
28 |
# File 'app/models/tag.rb', line 28 has_many :showcases, through: :taggings, source: :taggable, source_type: 'Showcase' |
#taggings ⇒ ActiveRecord::Relation<Tagging>
24 |
# File 'app/models/tag.rb', line 24 has_many :taggings, dependent: :destroy |
#to_param ⇒ Object
103 104 105 |
# File 'app/models/tag.rb', line 103 def to_param slug || id.to_s end |
#to_s ⇒ Object
99 100 101 |
# File 'app/models/tag.rb', line 99 def to_s name end |
#usage_by_type ⇒ Hash
Get usage count by taggable type
90 91 92 |
# File 'app/models/tag.rb', line 90 def usage_by_type taggings.group(:taggable_type).count end |
#usage_count ⇒ Object
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 |