Class: Groupy::OuterGroup

Inherits:
Group
  • Object
show all
Defined in:
lib/groupy.rb

Instance Attribute Summary

Attributes inherited from Group

#name, #sub_groups

Instance Method Summary collapse

Methods inherited from Group

#subgroups, #value_groups, #values

Constructor Details

#initialize(&block) ⇒ OuterGroup

Returns a new instance of OuterGroup.



73
74
75
# File 'lib/groupy.rb', line 73

def initialize(&block)
  super(:all, &block)
end

Instance Method Details

#attach!(klass, column_name, options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/groupy.rb', line 77

def attach!(klass, column_name, options)
  self.subgroups.each do |group_name, group_values|

    method_name = if options[:suffix]
      "#{group_name}_#{column_name}"
    else
      group_name
    end

    klass.class_eval <<-RUBY
      def #{method_name}?
        #{group_values.inspect}.include?(self.#{column_name})
      end
    RUBY
    
    scope_name = method_name.to_s.pluralize
    
    if defined?(ActiveRecord) && klass < ActiveRecord::Base
      klass.scope(scope_name, klass.where(column_name => group_values))
    end
  end
  
  if options[:constants]
    self.value_groups.each do |value_group|
      
      constant_name = if options[:suffix]
        "#{value_group.name}_#{column_name}"
      else
        value_group.name
      end
      klass.const_set("#{constant_name.upcase}", value_group.value)
    end
  end
  
  klass.class_eval <<-RUBY
    def self.all_#{column_name.to_s.pluralize}
      self.groupies[#{column_name.inspect}].values
    end
  RUBY
end