Skip to content

Commit

Permalink
Refactor configuration module
Browse files Browse the repository at this point in the history
Use the same module namespace as RailsLts::VERSION.
  • Loading branch information
foobear committed Jan 31, 2014
1 parent bcf87a3 commit 79e92c6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 80 deletions.
8 changes: 4 additions & 4 deletions railties/lib/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down
76 changes: 0 additions & 76 deletions railties/lib/rails/rails_lts.rb

This file was deleted.

74 changes: 74 additions & 0 deletions railties/lib/railslts.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 79e92c6

Please sign in to comment.