Module: Ekylibre::Record::Dependents::InstanceMethods

Defined in:
lib/ekylibre/record/dependents.rb

Instance Method Summary collapse

Instance Method Details

#has_dependents?Boolean

Look for all has_one, has_many and has_and_belongs_to_many reflections

Returns:

  • (Boolean)

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ekylibre/record/dependents.rb', line 10

def has_dependents?
  refs = self.class.reflect_on_all_associations.select { |r| r.macro.to_s.match(/^has_/) }
  return false if refs.empty?
  method_name = 'has_' + refs.collect { |r| r.name.to_s }.sort.join('_or_') + '?'
  unless respond_to?(method_name)
    code = ''
    code << "def #{method_name}\n"
    code << '  return (' + refs.collect do |r|
      if r.macro.to_s == 'has_one'
        "self.#{r.name}"
      else
        "self.#{r.name}.first"
      end
    end.join(' || ') + ")\n"
    code << "end\n"
    class_eval(code)
  end
  send(method_name)
end