Skip to content

Commit

Permalink
Merge pull request #2397 from bf4/remove_safe_yaml
Browse files Browse the repository at this point in the history
Remove safe_yaml.
  • Loading branch information
bbenezech committed Jun 7, 2016
2 parents 64cbbef + 5c14d48 commit 9819122
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
23 changes: 23 additions & 0 deletions lib/rails_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'rails_admin/extensions/history'
require 'rails_admin/support/csv_converter'
require 'rails_admin/support/hash_helper'
require 'yaml'

module RailsAdmin
# Setup RailsAdmin
Expand All @@ -33,6 +34,28 @@ def self.config(entity = nil, &block)
RailsAdmin::Config
end
end

# Backwards-compatible with safe_yaml/load when SafeYAML isn't available.
# Evaluates available YAML loaders at boot and creates appropriate method,
# so no conditionals are required at runtime.
begin
require 'safe_yaml/load'
def self.yaml_load(yaml)
SafeYAML.load(yaml)
end
rescue LoadError
if YAML.respond_to?(:safe_load)
def self.yaml_load(yaml)
YAML.safe_load(yaml)
end
else
raise LoadError.new "Safe-loading of YAML is not available. Please install 'safe_yaml' or install Psych 2.0+"
end
end

def self.yaml_dump(object)
YAML.dump(object)
end
end

require 'rails_admin/bootstrap-sass' unless defined? Bootstrap
4 changes: 2 additions & 2 deletions lib/rails_admin/config/fields/types/serialized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class Serialized < RailsAdmin::Config::Fields::Types::Text
RailsAdmin::Config::Fields::Types.register(self)

register_instance_option :formatted_value do
YAML.dump(value) unless value.nil?
RailsAdmin.yaml_dump(value) unless value.nil?
end

def parse_value(value)
value.present? ? (SafeYAML.load(value) || nil) : nil
value.present? ? (RailsAdmin.yaml_load(value) || nil) : nil
end

def parse_input(params)
Expand Down
1 change: 0 additions & 1 deletion lib/rails_admin/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
require 'rails'
require 'rails_admin'
require 'remotipart'
require 'safe_yaml/load'

module RailsAdmin
class Engine < Rails::Engine
Expand Down
1 change: 0 additions & 1 deletion rails_admin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'rack-pjax', '~> 0.7'
spec.add_dependency 'rails', ['>= 4.0', '< 6']
spec.add_dependency 'remotipart', '~> 1.0'
spec.add_dependency 'safe_yaml', '~> 1.0'
spec.add_dependency 'sass-rails', ['>= 4.0', '< 6']
spec.add_development_dependency 'bundler', '~> 1.0'
spec.authors = ['Erik Michaels-Ober', 'Bogdan Gaza', 'Petteri Kaapa', 'Benoit Benezech', 'Mitsuhiro Shibuya']
Expand Down
10 changes: 5 additions & 5 deletions spec/rails_admin/abstract_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
end

it 'lists elements within outbound limits' do
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['', 'January 02, 2012 00:00', 'January 03, 2012 00:00'], o: 'between'}}}).count).to eq(2)
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['', 'January 02, 2012 00:00', 'January 02, 2012 00:00'], o: 'between'}}}).count).to eq(1)
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['', 'January 03, 2012 00:00', ''], o: 'between'}}}).count).to eq(2)
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['', '', 'January 02, 2012 00:00'], o: 'between'}}}).count).to eq(2)
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['January 02, 2012 00:00'], o: 'default'}}}).count).to eq(1)
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['', 'January 02, 2012 12:00', 'January 03, 2012 12:00'], o: 'between'}}}).count).to eq(2)
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['', 'January 02, 2012 12:00', 'January 02, 2012 12:00'], o: 'between'}}}).count).to eq(1)
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['', 'January 03, 2012 12:00', ''], o: 'between'}}}).count).to eq(2)
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['', '', 'January 02, 2012 12:00'], o: 'between'}}}).count).to eq(2)
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['January 02, 2012 12:00'], o: 'default'}}}).count).to eq(1)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rails_admin/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ class PlayerWithDefaultScope < Player

it 'supports datetime type query' do
scope = FieldTest.all
expect(abstract_model.send(:filter_scope, scope, 'datetime_field' => {'1' => {v: ['', 'February 01, 2012 00:00', 'March 01, 2012 00:00'], o: 'between'}}).where_values).to eq(scope.where(['(field_tests.datetime_field BETWEEN ? AND ?)', Time.local(2012, 2, 1), Time.local(2012, 3, 1).end_of_day]).where_values)
expect(abstract_model.send(:filter_scope, scope, 'datetime_field' => {'1' => {v: ['', 'March 01, 2012 00:00', ''], o: 'between'}}).where_values).to eq(scope.where(['(field_tests.datetime_field >= ?)', Time.local(2012, 3, 1)]).where_values)
expect(abstract_model.send(:filter_scope, scope, 'datetime_field' => {'1' => {v: ['', '', 'February 01, 2012 00:00'], o: 'between'}}).where_values).to eq(scope.where(['(field_tests.datetime_field <= ?)', Time.local(2012, 2, 1).end_of_day]).where_values)
expect(abstract_model.send(:filter_scope, scope, 'datetime_field' => {'1' => {v: ['February 01, 2012 00:00'], o: 'default'}}).where_values).to eq(scope.where(['(field_tests.datetime_field BETWEEN ? AND ?)', Time.local(2012, 2, 1), Time.local(2012, 2, 1).end_of_day]).where_values)
expect(abstract_model.send(:filter_scope, scope, 'datetime_field' => {'1' => {v: ['', 'February 01, 2012 12:00', 'March 01, 2012 12:00'], o: 'between'}}).where_values).to eq(scope.where(['(field_tests.datetime_field BETWEEN ? AND ?)', Time.local(2012, 2, 1), Time.local(2012, 3, 1).end_of_day]).where_values)
expect(abstract_model.send(:filter_scope, scope, 'datetime_field' => {'1' => {v: ['', 'March 01, 2012 12:00', ''], o: 'between'}}).where_values).to eq(scope.where(['(field_tests.datetime_field >= ?)', Time.local(2012, 3, 1)]).where_values)
expect(abstract_model.send(:filter_scope, scope, 'datetime_field' => {'1' => {v: ['', '', 'February 01, 2012 12:00'], o: 'between'}}).where_values).to eq(scope.where(['(field_tests.datetime_field <= ?)', Time.local(2012, 2, 1).end_of_day]).where_values)
expect(abstract_model.send(:filter_scope, scope, 'datetime_field' => {'1' => {v: ['February 01, 2012 12:00'], o: 'default'}}).where_values).to eq(scope.where(['(field_tests.datetime_field BETWEEN ? AND ?)', Time.local(2012, 2, 1), Time.local(2012, 2, 1).end_of_day]).where_values)
end

it 'supports enum type query' do
Expand Down

0 comments on commit 9819122

Please sign in to comment.