Module: Augmentor::ActiveRecord::ClassMethods

Defined in:
lib/augmentor.rb

Instance Method Summary collapse

Instance Method Details

#augment(*args) ⇒ Object



11
12
13
14
15
16
# File 'lib/augmentor.rb', line 11

def augment(*args)
  validate_augmentor_args(args)
  klass, options, klass_name = parse_augmentor_args(args)
  belongs_to klass, options
  validates_presence_of :"#{klass}"
end

#augmented_by(*args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/augmentor.rb', line 18

def augmented_by(*args)
  validate_augmentor_args(args)
  klass, options, klass_name = parse_augmentor_args(args)
  has_one klass, {dependent: :destroy, inverse_of: :"#{self.name.underscore}"}.merge(options)
  define_method "#{klass}_must_be_valid" do
    if self.send(klass).valid?
      true
    else
      self.errors.messages.merge!(self.send(klass).errors.messages)
      false
    end
  end
  validate :"#{klass}_must_be_valid"

  define_method "#{klass}_with_autobuild" do
    self.send(:"#{klass}_without_autobuild") || self.send(:"build_#{klass}")
  end
  alias_method_chain :"#{klass}", :autobuild

  after_initialize do
    all_attributes = klass_name.constantize.content_columns.map(&:name)
    attributes_to_delegate = all_attributes - self.class.content_columns.map(&:name)
    attributes_to_delegate.each do |attrib|
      class_eval <<-RUBY
        def #{attrib}
          #{klass}.#{attrib}
        end
        def #{attrib}=(value)
          self.#{klass}.#{attrib} = value
        end
        def #{attrib}?
          self.#{klass}.#{attrib}?
        end
      RUBY
    end
  end
end