Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/merit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def self.current_user_method
"current_#{@config.user_model_name.downcase}".to_sym
end

def self.yaml_safe_load_permitted_classes
[
ActiveModel::Attribute.const_get(:FromDatabase),
ActiveModel::Attribute.const_get(:FromUser),
ActiveModel::Type::String,
ActiveSupport::HashWithIndifferentAccess
] + @config.yaml_safe_load_permitted_classes
end

def self.observers
@config.observers
end
Expand All @@ -47,13 +56,14 @@ def self.remove_point_rules

class Configuration
attr_accessor :checks_on_each_request, :orm, :user_model_name, :observers,
:current_user_method
:current_user_method, :yaml_safe_load_permitted_classes

def initialize
@checks_on_each_request = true
@orm = :active_record
@user_model_name = 'User'
@observers = []
@yaml_safe_load_permitted_classes = []
end

def add_observer(class_name)
Expand Down
13 changes: 12 additions & 1 deletion lib/merit/base_target_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ def model_class
end

def reanimate_target_from_action
YAML.load(@action.target_data)
parse_target_data(@action.target_data)
end

private

def parse_target_data(target_data)
model_class.new JSON.parse(target_data)
rescue JSON::ParserError
YAML.safe_load(
target_data,
permitted_classes: Merit.yaml_safe_load_permitted_classes
)
end
end
end
4 changes: 2 additions & 2 deletions lib/merit/controller_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def merit_action_hash
had_errors: had_errors?,
target_model: controller_path,
target_id: target_id,
target_data: target_object.to_yaml,
target_data: JSON.generate(target_object.as_json)
}
end

Expand All @@ -42,7 +42,7 @@ def had_errors?
def target_object
variable_name = :"@#{controller_name.singularize}"
if instance_variable_defined?(variable_name)
if target_obj = instance_variable_get(variable_name)
if (target_obj = instance_variable_get(variable_name))
target_obj
else
warn_no_object_found
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/config/initializers/merit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Merit.setup do |config|
# Add application observers to get notifications any time merit changes reputation.
config.add_observer 'DummyObserver'

config.yaml_safe_load_permitted_classes = %w(Comment)
config.orm = ENV['ORM'].try(:to_sym)
end

Expand Down
1 change: 0 additions & 1 deletion test/integration/navigation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ def teardown
# Destroying a comment should remove points from the comment creator.
comment_to_destroy = user.comments.last
visit '/comments'
skip "see bug https://github.com/merit-gem/merit/issues/365"
assert_difference lambda { user.reload.points }, -5 do
within("tr#c_#{comment_to_destroy.id}") do
click_link 'Destroy'
Expand Down
7 changes: 3 additions & 4 deletions test/unit/action_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

describe Merit::Action do
it 'saves correctly with a serialised model' do
skip "see bug https://github.com/merit-gem/merit/issues/365"
comment = Comment.new(name: 'the comment name')
action = Merit::Action.create(target_model: 'comment',
target_id: 2,
target_data: comment.to_yaml)
comment_yaml = Merit::Action.find(action.id).target_data
assert_equal comment.name, YAML::load(comment_yaml).name
target_data: JSON.generate(comment.as_json))
comment_json = Merit::Action.find(action.id).target_data
assert_equal comment.name, JSON.parse(comment_json)['name']
end
end
15 changes: 14 additions & 1 deletion test/unit/base_target_finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,20 @@

describe 'target was destroyed' do
it 'gets the object from the JSON data in the merit_actions table' do
skip "see bug https://github.com/merit-gem/merit/issues/365"
comment = Comment.new(name: 'the comment name')

rule = Merit::Rule.new
rule.to = :itself
rule.model_name = 'comment'
action = Merit::Action.new(target_model: 'comment',
target_id: 2,
target_data: JSON.generate(comment.as_json))

finder = Merit::BaseTargetFinder.new(rule, action)
_(finder.find.name).must_be :==, 'the comment name'
end

it 'gets the object from the legacy YAML data in the merit_actions table' do
comment = Comment.new(name: 'the comment name')

rule = Merit::Rule.new
Expand Down