Sha256: 383b25e0ed16c41303651fac4b94cc1c672560cdaba918e8556dd4c70842807b

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require "enumerize_with_groups/version"
require "enumerize"

module EnumerizeWithGroups
  def self.extended(mod)
    unless mod.respond_to?(:enumerize)
      fail <<-MESSAGE
        You have to `extend Enumerize` before `extend EnumerizeWithGroups`.
      MESSAGE
    end

    mod.module_eval do
      def self.enumerize(name, options = {})
        super

        self.define_singleton_method("#{name}_groups") do
          options[:groups]
        end

        if options[:groups]
          options[:groups].each do |key, items|
            scope_name = "enumerize_with_groups_#{name}_#{key}_group"

            self.define_singleton_method("#{name}_#{key}") do
              items
            end

            define_method("in_#{name}_#{key}?") do
              values = options[:groups][key]
              fail "You have to define #{key} as group" unless values.is_a?(Array)

              values.map(&:to_s).include?(self.public_send(name))
            end

            if self.ancestors.include?(ActiveRecord::Base)
              scope scope_name, ->{ where(name => items) }

              self.define_singleton_method("#{name}_#{key}_scope") do
                self.send(scope_name)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enumerize_with_groups-0.2.0 lib/enumerize_with_groups.rb