Module: Horde::Actions::FavoriteMethods::TargetMethods::ClassMethods

Defined in:
lib/horde/actions/favorite.rb

Instance Method Summary collapse

Instance Method Details

#create_favorite_associationsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/horde/actions/favorite.rb', line 71

def create_favorite_associations
  target_class_name = self.name   # like "Article", "Photo"
  assn_name = "favorited_#{target_class_name.tableize}"   # "favorited_articles"

  # define belongs_to here because Setting.actor_clas_name has not been set
  ::Horde::Favorite.belongs_to :favoriter, 
                      :foreign_key => :actor_id,
                      :class_name => Horde::Setting.actor_class_name

  # favorite.favorited_article, this is created for
  # user.favorited_articles to work.
  ::Horde::Favorite.belongs_to assn_name.singularize.to_sym, 
                      :foreign_key => :target_id,
                      :class_name => target_class_name

  Horde::Setting.actor_class_name.constantize.instance_eval do
    include ActorMethods

    # user.favorited_articles
    has_many assn_name, 
             :through => :created_favorites,
             :source => assn_name.singularize,
             :conditions => {:"horde_favorites.target_type" => target_class_name}
  end
end