Module: NewsmastMastodon::Concerns::TagConcern

Extended by:
ActiveSupport::Concern
Defined in:
app/models/newsmast_mastodon/concerns/tag_concern.rb

Class Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/newsmast_mastodon/concerns/tag_concern.rb', line 10

def self.prepended(base)
  base.singleton_class.class_eval do
    def matching_name(name_or_names, consider_ban_case = true)
      names = Array(name_or_names).map { |name| arel_table.lower(normalize(name)) }

      scope =
        if names.size == 1
          where(arel_table[:name].lower.eq(names.first))
        else
          where(arel_table[:name].lower.in(names))
        end

      scope = scope.listable if consider_ban_case
      scope
    end

    def find_or_create_by_names(name_or_names)
      names = Array(name_or_names).map { |str| [normalize(str), str] }.uniq(&:first)

      names.map do |(normalized_name, display_name)|
        tag = matching_name(normalized_name, false).first || create(
          name: normalized_name,
          display_name: display_name.gsub(Tag::HASHTAG_INVALID_CHARS_RE, '')
        )
        yield tag if block_given?
        tag
      end
    end
  end
end