mardi 29 septembre 2015

Twitter reputation_system vote on no related model

I have a product model that belongs to shop

class Product < ActiveRecord::Base
  belongs_to :shop
has_reputation :votes, source: :user, aggregated_by: :sum

end

And i would like to vote on products with current_user

class User < ActiveRecord::Base

  has_one :shop,  :dependent => :destroy
  has_many :rs_evaluations, class_name: "ReputationSystem::Evaluation", as: :source

  has_reputation :votes, source: {reputation: :votes, of: :products}, aggregated_by: :sum

  def voted_for?(product)
    evaluations.where(target_type: product.class, target_id: product.id).present?
  end    
end

view

<% if current_user && !current_user.voted_for?(product) %>
  | <%= link_to "up", vote_product_path(product, type: "aumentar"), method: "post" %>
  | <%= link_to "down", vote_product_path(product, type: "diminuir"), method: "post" %>
<% end 

Someone have tried this before? thank's for any tip

Aucun commentaire:

Enregistrer un commentaire