Class: SocialEngine::Engine

Inherits:
Rails::Engine
  • Object
show all
Defined in:
lib/social_engine/engine.rb

Class Method Summary collapse

Class Method Details

.add_poly_belongs_toObject

TODO:refactor add polymorhpic_belongs_to to inherited_resources controllers after detecting which models use ‘able’ functions



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/social_engine/engine.rb', line 30

def self.add_poly_belongs_to
  [CommentsController,RatingsController,VotesController,FavoritesController].each do |controller|
    poly_belongs_tos = []
    Dir.glob("#{Rails.root.to_s}/app/models/**/*.rb").each do |model_name|
      #TODO: gotta be a better way to do this.  REFACTOR!
      model_name = model_name.split("/").last.gsub(".rb","")
      klass = model_name.camelize.constantize rescue next
      able_id = controller.to_s.gsub("sController","").gsub("ing","e").downcase+"able?"
      poly_belongs_tos << model_name.to_sym if klass.instance_methods.include?(able_id.to_sym)
    end
    controller.class_eval("polymorphic_belongs_to :#{poly_belongs_tos.join(',:')}") unless poly_belongs_tos.blank?
  end
end