summaryrefslogtreecommitdiff
path: root/lib/has_tag_proxy.rb
blob: 98ca3c6bda68c56626868efd49911cbd3a33c43d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# frozen_string_literal: true

module HasTagProxy
  def has_tag_proxy(on:, name:)
    define_method(name) do
      Tag.where(id: self[on]).order(name: :asc).pluck(:name).join(', ')
    end

    define_method("#{name}=") do |val|
      # FIXME: using #[]= fucks with HasArrayField, but would be much nicer here
      send "#{on}=", Tag.where(name: Tag.parse_tag_list(val)).pluck(:id)
    end
  end
end

ActiveRecord::Base.send(:extend, HasTagProxy)