Skip to content

Instantly share code, notes, and snippets.

@su10
Last active August 29, 2015 14:01
Show Gist options
  • Save su10/79418e435aaea2535121 to your computer and use it in GitHub Desktop.
Save su10/79418e435aaea2535121 to your computer and use it in GitHub Desktop.
def hash_for_includes_all_masters(recursive=true, all_associations=[])
all_associations << self.to_s
result_arr = []
self.reflect_on_all_associations.each do |model|
association_name = model.name
class_name = model.class_name
next if all_associations.include?(class_name)
next unless class_name.include?('Master')
next if model.options.has_key?(:polymorphic)
# when depth:1
(result_arr << association_name.to_sym) and next unless recursive
# when recursive
tmp = class_name.classify.constantize.hash_for_includes_all_masters(true, all_associations)
if tmp.blank?
# has no relation
result_arr << association_name.to_sym
else
# has relation(s)
result_arr << Hash[association_name, tmp]
end
end
result_arr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment