Sha256: 9a50f494cd492bd9ec909da91a672f79ff148f08bd083c2b73f43ecbc2c01e26

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

class Reputation  
  module User
    
    def self.included(model)
      model.send(:include, InstanceMethods)
      model.send(:include, Relations)
    end
    
    module Relations
      def self.included(model)
        model.class_eval do
          
          has_many :behaviours, :class_name => "ReputationBehaviour", :extend => AddRails2 if Reputation::RailsVersion.rails_2?
          has_many :behaviours, :class_name => "ReputationBehaviour", :extend => AddRails3 if Reputation::RailsVersion.rails_3?
          
        end
      end
    end
    
    module AddRails2
      def add(rule, metric)
        rule = ReputationRule.find_by_name rule unless rule.is_a? ReputationRule
        b = proxy_reflection.klass.find_by_user_id_and_rule_id proxy_owner.id, rule.id
        b ||= proxy_reflection.klass.new :rule => rule, :user => proxy_owner
        b.update_attribute :metric, metric
        proxy_owner.behaviours(true)
        b
      end
    end
    
    module AddRails3
      def add(rule, metric)
        rule = ReputationRule.find_by_name rule unless rule.is_a? ReputationRule
        b = proxy_association.reflection.klass.find_by_user_id_and_rule_id proxy_association.owner.id, rule.id
        b ||= proxy_association.reflection.klass.new :rule => rule, :user => proxy_association.owner
        b.update_attribute :metric, metric
        proxy_association.owner.behaviours(true)
        b
      end
    end
    
    module InstanceMethods
      
      # Returns the reputation value for the user
      #
      def reputation
        ReputationRule.value_for(self)
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reputation-0.0.5 lib/reputation/user.rb
reputation-0.0.4 lib/reputation/user.rb