Sha256: f99648e44fe4cbca2e5a960952f0d0f22117ef01f1a5d5e3d0660012e86f70ae

Contents?: true

Size: 1001 Bytes

Versions: 1

Compression:

Stored size: 1001 Bytes

Contents

module ActiveRecord
  module GroupingEnum
    def grouping_enum(definitions)
      definitions.each do |name, values|
        name = name.to_sym
        klass = self
        values.each do |key, enums|
          _grouping_enum_methods_module.module_eval do
            klass.send(:detect_enum_conflict!, name, "#{key}?")
            define_method("#{key}?") { enums.map(&:to_s).include?(self[name]) }

            klass.send(:detect_enum_conflict!, name, key.to_s, true)
            klass.scope key.to_s, lambda {
              clause = enums.each_with_object Hash.new { |h, k| h[k] = [] } do |enum, obj|
                obj[name] << klass.public_send(name.to_s.pluralize.to_sym)[enum.to_s]
              end
              klass.where(clause)
            }
          end
        end
      end
    end

    private

    def _grouping_enum_methods_module
      @_grouping_enum_methods_module ||=
        begin
          mod = Module.new
          include mod
          mod
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-grouping_enum-1.0.0 lib/active_record/grouping_enum.rb