summaryrefslogtreecommitdiff
path: root/app/models/concerns/has_whatnot.rb
blob: 987c969e24ab468bae1caa9ac2e44aff8855981a (plain)
1
2
3
4
5
6
7
8
9
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
module HasWhatnot
  extend ActiveSupport::Concern

  included do
    def whatnot
      @whatnot ||= super.is_a?(Hash) ? super.deep_symbolize_keys! : nil
    end

    def tags
      whatnot && whatnot[:tags] ? whatnot[:tags] : []
    end

    def has_tag?(tag)
      return false unless tags.any?
      tags.include?(tag)
    end

    def where_has_tag(tag)
      where("whatnot->'tags' ? :tag", tag: tag)
    end

    def conditions_met?(data)
      data[:conditions]&.each do |conditions_data|
        case conditions_data[:type]
        when "time_of_day"
          return false unless conditions_data[:times].include? World.time_of_day.to_s
        else
          raise "Invalid condition type GID (#{conditions_data[:type]})"
        end
      end
      true
    end
  end
end