Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Commit

Permalink
Adding a new serialized data field to the reputation model
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba authored and Sumit Shah committed Oct 6, 2014
1 parent d8037f9 commit 29eb46d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
Expand Up @@ -37,6 +37,7 @@ def create_migration_files
create_migration_file_if_not_exist 'change_evaluations_index_to_unique'
create_migration_file_if_not_exist 'change_reputation_messages_index_to_unique'
create_migration_file_if_not_exist 'change_reputations_index_to_unique'
create_migration_file_if_not_exist 'add_data_to_reputations'
end

private
Expand Down
@@ -0,0 +1,9 @@
class AddDataToReputations < ActiveRecord::Migration
def self.up
add_column :rs_reputations, :data, :text
end

def self.down
remove_column :rs_reputations, :data
end
end
2 changes: 2 additions & 0 deletions lib/reputation_system/models/reputation.rb
Expand Up @@ -33,6 +33,8 @@ def from(sender)

validates_uniqueness_of :reputation_name, :scope => [:target_id, :target_type]

serialize :data, Hash

def self.find_by_reputation_name_and_target(reputation_name, target)
target_type = get_target_type_for_sti(target, reputation_name)
ReputationSystem::Reputation.find_by_reputation_name_and_target_id_and_target_type(reputation_name.to_s, target.id, target_type)
Expand Down
7 changes: 7 additions & 0 deletions spec/reputation_system/models/reputation_spec.rb
Expand Up @@ -157,4 +157,11 @@
answer.reputation_for(:custom_rating).should be_within(DELTA).of(20)
end
end

describe "additional data" do
it "should have data as a serialized field" do
r = ReputationSystem::Reputation.create!(:reputation_name => "karma", :target_id => @user.id, :target_type => @user.class.to_s, :aggregated_by => 'sum')
r.data.should be_a(Hash)
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -56,6 +56,7 @@
t.string :aggregated_by
t.references :target, :polymorphic => true
t.boolean :active, :default => true
t.text :data
t.timestamps
end

Expand Down

0 comments on commit 29eb46d

Please sign in to comment.