Module: EnumerizeWithGroups

Defined in:
lib/enumerize_with_groups.rb,
lib/enumerize_with_groups/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object



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
35
36
37
38
39
# File 'lib/enumerize_with_groups.rb', line 5

def self.extended(mod)
  unless mod.respond_to?(:enumerize)
    fail "      You have to `extend Enumerize` before `extend EnumerizeWithGroups`.\n    MESSAGE\n  end\n\n  mod.module_eval do\n    def self.enumerize(name, options = {})\n      super\n\n      self.define_singleton_method(\"\#{name}_groups\") do\n        options[:groups]\n      end\n\n      if options[:groups]\n        options[:groups].each do |key, items|\n          scope_name = \"enumerize_with_groups_\#{name}_\#{key}_group\"\n\n          self.define_singleton_method(\"\#{name}_\#{key}\") do\n            items\n          end\n\n          if self.ancestors.include?(ActiveRecord::Base)\n            scope scope_name, ->{ where(name => items) }\n\n            self.define_singleton_method(\"\#{name}_\#{key}_scope\") do\n              self.send(scope_name)\n            end\n          end\n        end\n      end\n    end\n  end\nend\n"