Commit d3732796b6c6367d2059bbca4dcf70ca043bfed5

Authored by Victor Costa
1 parent 65cdb388
Exists in master

Do not store serialized objects in target_data

db/migrate/20160216134912_clean_target_data_column.rb 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +class CleanTargetDataColumn < ActiveRecord::Migration
  2 +
  3 + def up
  4 + models = {'article' => 'Article', 'comment' => 'Comment', 'vote' => 'Vote',
  5 + 'friendship' => 'Friendship', 'profile' => 'Profile',
  6 + 'articlefollower' => 'ArticleFollower'}
  7 +
  8 + Merit::Action.where("target_model NOT IN (?)", models.keys).find_each do |action|
  9 + next if action.target_data.blank?
  10 + obj = YAML.load(action.target_data) rescue nil
  11 + unless obj.nil?
  12 + action.update_attribute(:target_model, obj.class.respond_to?(:base_class) ? obj.class.base_class.name : obj.class.name)
  13 + end
  14 + end
  15 +
  16 + models.each do |old_name, new_name|
  17 + Merit::Action.where(target_model: old_name).update_all(target_model: new_name)
  18 + end
  19 +
  20 + Merit::Action.update_all(target_data: nil)
  21 + end
  22 +
  23 + def down
  24 + puts "Warning: cannot restore target_data"
  25 + end
  26 +
  27 +end
... ...
lib/merit_ext.rb
1 1 require 'merit/badge_ext'
2 2 require 'merit/sash'
3 3 require 'merit/badges_sash'
  4 +require 'merit/action'
4 5  
5 6 module Merit
6 7  
... ... @@ -40,6 +41,16 @@ module Merit
40 41 end
41 42 end
42 43  
  44 + class Action
  45 + def target_obj
  46 + target_model.constantize.find_by_id(target_id)
  47 + end
  48 +
  49 + def rules_matcher
  50 + @rules_matcher ||= ::Merit::RulesMatcher.new(target_model.downcase, action_method)
  51 + end
  52 + end
  53 +
43 54 module ClassMethods
44 55  
45 56 def has_merit_actions(options = {})
... ... @@ -64,9 +75,8 @@ module Merit
64 75 :user_id => user ? user.id : nil,
65 76 :action_method => action,
66 77 :had_errors => self.errors.present?,
67   - :target_model => self.class.base_class.name.downcase,
68   - :target_id => self.id,
69   - :target_data => self.to_yaml
  78 + :target_model => self.class.base_class.name,
  79 + :target_id => self.id
70 80 })
71 81 action.check_all_rules
72 82 action
... ...
script/check_merit_actions_vs_points.rb
... ... @@ -17,7 +17,9 @@ class ProcessObserver
17 17 merit = changed_data[:merit_object]
18 18 if merit.kind_of?(Merit::Score::Point)
19 19 action = Merit::Action.find(changed_data[:merit_action_id])
20   - new_date = YAML.load(action.target_data).created_at
  20 + model = action.target_obj
  21 + return if model.nil?
  22 + new_date = model.created_at
21 23 action.update_attribute(:created_at, new_date)
22 24 merit.update_attribute(:created_at, new_date)
23 25 end
... ...
script/process_merit_rules.rb
... ... @@ -6,6 +6,8 @@ class ProcessObserver
6 6 merit = changed_data[:merit_object]
7 7 if merit.kind_of?(Merit::Score::Point)
8 8 action = Merit::Action.find(changed_data[:merit_action_id])
  9 + model = action.target_obj
  10 + return if model.nil?
9 11 new_date = YAML.load(action.target_data).created_at
10 12 action.update_attribute(:created_at, new_date)
11 13 merit.update_attribute(:created_at, new_date)
... ...
test/unit/article_test.rb
... ... @@ -209,4 +209,11 @@ class ArticleTest &lt; ActiveSupport::TestCase
209 209 assert_equal [], person.badges
210 210 end
211 211  
  212 + should 'restore article object from action' do
  213 + create_point_rule_definition('article_author')
  214 + article = create(TextArticle, :profile_id => person.id, :author => person)
  215 + assert_equal 1, person.score_points.count
  216 + assert_equal article, person.score_points.first.action.target_obj
  217 + end
  218 +
212 219 end
... ...
test/unit/merit_ext_test.rb
... ... @@ -24,4 +24,12 @@ class MeritExtTest &lt; ActiveSupport::TestCase
24 24 assert !point.undo_rule?
25 25 end
26 26  
  27 + should 'return target object associated to the merit action' do
  28 + article = fast_create(Article)
  29 + action = Merit::Action.new
  30 + action.target_model = article.class.base_class.name
  31 + action.target_id = article.id
  32 + assert_equal article, action.target_obj
  33 + end
  34 +
27 35 end
... ...