Sha256: f3384e42eec0feedd906ecf35b4f2a5ff8c8f71584855a2700b940c1a420beae

Contents?: true

Size: 814 Bytes

Versions: 4

Compression:

Stored size: 814 Bytes

Contents

class ReputationBehaviour < ActiveRecord::Base
  
  belongs_to :user
  belongs_to :rule, :class_name => 'ReputationRule'
  
  before_save :recalculate_rule
  
  attr_accessible :user, :rule, :metric
  
  validates_presence_of :user
  validates_presence_of :rule
  validates_numericality_of :metric
  
  validates_uniqueness_of :user_id, :scope => :rule_id
  
  # Set the rule, can either be a ReputationRule or the name of the rule
  #
  # @example
  #   behaviour.rule = 'rule_1'
  #   # or
  #   behaviour.rule = ReputationRule.find(:first)
  
  def rule=(rule)
    write_attribute :rule_id, case rule
    when ReputationRule
      rule.id
    else 
      ReputationRule.find_by_name( rule ).try(:id)
    end
  end
  
private

  def recalculate_rule
    rule.recalculate_intermediate_values_for user
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
reputation-0.0.5 app/models/reputation_behaviour.rb
reputation-0.0.4 app/models/reputation_behaviour.rb
reputation-0.0.3 app/models/reputation_behaviour.rb
reputation-0.0.2 app/models/reputation_behaviour.rb