Module: Partitional::ClassMethods

Defined in:
lib/partitional.rb

Instance Method Summary collapse

Instance Method Details

#partial_optionsObject


13
14
15
# File 'lib/partitional.rb', line 13

def partial_options
  @partial_options ||= {}
end

#partialsObject


9
10
11
# File 'lib/partitional.rb', line 9

def partials
  @partials ||= []
end

#partition(name, class_name: nil, mapping: {}, prefix: nil, validation: true) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/partitional.rb', line 17

def partition(name, class_name: nil, mapping: {}, prefix: nil, validation: true)
  name = name.to_s.to_sym
  partials.push(name)

  klass = (class_name || name).to_s.classify.constantize

  klass.attributes.each do |attr|
    mapping[attr] ||= "#{prefix ? "#{prefix}_" : ''}#{attr}"
  end

  partial_options[name] = { mapping: mapping }

  define_partial_accessor(name, klass, mapping)
  define_partial_validator(name, mapping) if validation
end