From 79e92c678cc152df18cbb3155007208a95be558e Mon Sep 17 00:00:00 2001 From: Arne Hartherz Date: Fri, 31 Jan 2014 16:44:15 +0100 Subject: [PATCH] Refactor configuration module Use the same module namespace as RailsLts::VERSION. --- railties/lib/initializer.rb | 8 ++-- railties/lib/rails/rails_lts.rb | 76 --------------------------------- railties/lib/railslts.rb | 74 ++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 80 deletions(-) delete mode 100644 railties/lib/rails/rails_lts.rb create mode 100644 railties/lib/railslts.rb diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 64c68b7f3925..03fb033fa8c9 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -9,8 +9,8 @@ require 'rails/plugin/loader' require 'rails/gem_dependency' require 'rails/rack' -require 'rails/rails_lts' - +require 'railslts' +require 'railslts-version' RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV) @@ -643,11 +643,11 @@ def disable_dependency_loading end def configure_rails_lts - Rails::RailsLts.configuration = Rails::RailsLts::Configuration.new(Rails.configuration.rails_lts_options) + RailsLts::Configuration.prepare(Rails.configuration.rails_lts_options) end def finalize_rails_lts - Rails::RailsLts.finalize + RailsLts::Configuration.finalize end end diff --git a/railties/lib/rails/rails_lts.rb b/railties/lib/rails/rails_lts.rb deleted file mode 100644 index 67d441b6c4d1..000000000000 --- a/railties/lib/rails/rails_lts.rb +++ /dev/null @@ -1,76 +0,0 @@ -module Rails - - module RailsLts - - class << self - - attr_accessor :configuration - - def finalize - finalize_param_parsers - finalize_json_html_entity_escaping - end - - - private - - def finalize_param_parsers - if configuration.disable_json_parsing - ActionController::Base.param_parsers.delete(Mime::JSON) - end - if configuration.disable_xml_parsing - ActionController::Base.param_parsers.delete(Mime::XML) - end - end - - def finalize_json_html_entity_escaping - if configuration.escape_html_entities_in_json - ActiveSupport::JSON::Encoding.escape_html_entities_in_json = true - end - end - - end - - - class Configuration - - attr_accessor :disable_json_parsing - attr_accessor :disable_xml_parsing - - attr_accessor :escape_html_entities_in_json - - def initialize(options) - unless Rails.configuration.rails_lts_options - $stderr.puts(%{Please configure your rails_lts_options using config.rails_lts_options inside Rails::Initializer.run. Defaulting to "rails_lts_options = { :default => :compatible }. See https://makandracards.com/railslts/16311-configuring-rails-lts for documentation."}) - end - - options ||= {} - - set_defaults(options.delete(:default) || :compatible) - - options.each do |key, value| - self.send("#{key}=", value) - end - end - - def set_defaults(default) - unless [:hardened, :compatible].include?(default) - raise ArgumentError.new("Rails LTS: default needs to be :hardened or :compatible") - end - case default - when :hardened - self.disable_json_parsing = true - self.disable_xml_parsing = true - self.escape_html_entities_in_json = true - when :compatible - self.disable_json_parsing = false - self.disable_xml_parsing = false - self.escape_html_entities_in_json = false - end - end - - end - - end - -end diff --git a/railties/lib/railslts.rb b/railties/lib/railslts.rb new file mode 100644 index 000000000000..522c42c61ef8 --- /dev/null +++ b/railties/lib/railslts.rb @@ -0,0 +1,74 @@ +module RailsLts + + class << self + attr_accessor :configuration + end + + class Configuration + attr_accessor :disable_json_parsing + attr_accessor :disable_xml_parsing + + attr_accessor :escape_html_entities_in_json + + def self.prepare(rails_lts_options) + RailsLts.configuration = new(rails_lts_options) + end + + def self.finalize + RailsLts.configuration.finalize + end + + def initialize(options) + unless Rails.configuration.rails_lts_options + $stderr.puts(%{Please configure your rails_lts_options using config.rails_lts_options inside Rails::Initializer.run. Defaulting to "rails_lts_options = { :default => :compatible }. See https://makandracards.com/railslts/16311-configuring-rails-lts for documentation."}) + end + + options ||= {} + + set_defaults(options.delete(:default) || :compatible) + + options.each do |key, value| + self.send("#{key}=", value) + end + end + + def finalize + finalize_param_parsers + finalize_json_html_entity_escaping + end + + private + + def set_defaults(default) + unless [:hardened, :compatible].include?(default) + raise ArgumentError.new("Rails LTS: default needs to be :hardened or :compatible") + end + case default + when :hardened + self.disable_json_parsing = true + self.disable_xml_parsing = true + self.escape_html_entities_in_json = true + when :compatible + self.disable_json_parsing = false + self.disable_xml_parsing = false + self.escape_html_entities_in_json = false + end + end + + def finalize_param_parsers + if disable_json_parsing + ActionController::Base.param_parsers.delete(Mime::JSON) + end + if disable_xml_parsing + ActionController::Base.param_parsers.delete(Mime::XML) + end + end + + def finalize_json_html_entity_escaping + if escape_html_entities_in_json + ActiveSupport::JSON::Encoding.escape_html_entities_in_json = true + end + end + end + +end