From c0b9bf39ec3cf2fce28fe92c7892a0c47593ca09 Mon Sep 17 00:00:00 2001 From: GIGrave Date: Thu, 2 Nov 2023 18:32:57 +0500 Subject: [PATCH] feature: add string_tools config https://jira.railsc.ru/browse/BPC-22612 --- .gitignore | 1 + Gemfile | 2 ++ dip.yml | 1 + docker-compose.yml | 11 +++++++++++ lib/string_tools.rb | 7 +++++-- lib/string_tools/engine.rb | 14 ++++++++++++++ spec/internal/config/database.yml | 5 +++++ spec/internal/config/environments/test.rb | 10 ++++++++++ spec/internal/config/routes.rb | 5 +++++ spec/internal/db/schema.rb | 5 +++++ spec/spec_helper.rb | 9 +++++++++ string_tools.gemspec | 7 +++++-- 12 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 lib/string_tools/engine.rb create mode 100644 spec/internal/config/database.yml create mode 100644 spec/internal/config/environments/test.rb create mode 100644 spec/internal/config/routes.rb create mode 100644 spec/internal/db/schema.rb diff --git a/.gitignore b/.gitignore index 011b4fa..c9d17e7 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ /gemfiles /.idea Makefile.local +log/ diff --git a/Gemfile b/Gemfile index 49f984c..11c22c2 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,5 @@ source 'https://rubygems.org' gemspec gem 'actionpack', '~> 4.2.0', require: false +gem 'loofah', '< 2.20.0', require: false +gem 'pg', '< 1', require: false diff --git a/dip.yml b/dip.yml index b2e85d3..ce2f51e 100644 --- a/dip.yml +++ b/dip.yml @@ -3,6 +3,7 @@ version: '1' environment: DOCKER_RUBY_VERSION: 2.4 RUBY_IMAGE_TAG: 2.4-latest + POSTGRES_IMAGE_TAG: 11 COMPOSE_FILE_EXT: development RAILS_ENV: test diff --git a/docker-compose.yml b/docker-compose.yml index ba35a5d..4a8ced0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,4 +6,15 @@ services: environment: - SSH_AUTH_SOCK=/ssh/auth/sock - BUNDLE_PATH=/bundle/$DOCKER_RUBY_VERSION + - BUNDLE_APP_CONFIG=/app/.bundle + - TEST_DB_HOST=db + - TEST_DB_NAME=docker + - TEST_DB_USERNAME=postgres command: bash + depends_on: + - db + + db: + image: abakpress/postgres-db:$POSTGRES_IMAGE_TAG + environment: + - POSTGRES_DB=docker diff --git a/lib/string_tools.rb b/lib/string_tools.rb index 175d981..57b8896 100644 --- a/lib/string_tools.rb +++ b/lib/string_tools.rb @@ -1,10 +1,13 @@ -# coding: utf-8 # frozen_string_literal: true require 'string_tools/version' +require 'rails' +require 'action_view' +require 'active_record' require 'ru_propisju' require 'sanitize' require 'active_support/core_ext/string' require 'string_tools/core_ext/string' +require 'string_tools/engine' module StringTools autoload :HTML, 'string_tools/html' @@ -181,7 +184,7 @@ def sanitize(str, attrs = {}) str, :attributes => attributes, :elements => elements, - :css => {:properties => Sanitize::Config::RELAXED[:css][:properties], protocols: protocols}, + :css => ::Rails.application.config.string_tools.fetch(:css_sanitize).merge(protocols: protocols), :remove_contents => remove_contents || Set['style', 'script'], :allow_comments => false, :transformers => transformers diff --git a/lib/string_tools/engine.rb b/lib/string_tools/engine.rb new file mode 100644 index 0000000..58f5dec --- /dev/null +++ b/lib/string_tools/engine.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module StringTools + class Engine < ::Rails::Engine + config.autoload_paths += Dir[config.root.join('lib')] + + initializer :string_tools, before: :load_init_rb do |app| + app.config.string_tools = { + css_sanitize: {properties: Sanitize::Config::RELAXED[:css][:properties]}, + } + end + end +end + diff --git a/spec/internal/config/database.yml b/spec/internal/config/database.yml new file mode 100644 index 0000000..c2fc41f --- /dev/null +++ b/spec/internal/config/database.yml @@ -0,0 +1,5 @@ +test: + adapter: postgresql + host: <%= ENV.fetch("TEST_DB_HOST", "localhost") %> + database: <%= ENV.fetch("TEST_DB_NAME", "docker") %> + username: <%= ENV.fetch("TEST_DB_USERNAME", "docker") %> diff --git a/spec/internal/config/environments/test.rb b/spec/internal/config/environments/test.rb new file mode 100644 index 0000000..8d0dedd --- /dev/null +++ b/spec/internal/config/environments/test.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +HOST = 'test.host' +HOST_SHORT = 'test.host' +ASSETS_HOST = 'www.example.org' +ADMIN_HOST = 'admin.example.org' + +DO_NOT_REPLY = 'support@example.org' +DO_NOT_REPLY_RETURN_PATH = 'support@example.org' +APP_NAME = 'example' diff --git a/spec/internal/config/routes.rb b/spec/internal/config/routes.rb new file mode 100644 index 0000000..33d1711 --- /dev/null +++ b/spec/internal/config/routes.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +Rails.application.routes.draw do + default_url_options host: HOST +end diff --git a/spec/internal/db/schema.rb b/spec/internal/db/schema.rb new file mode 100644 index 0000000..cad1b4f --- /dev/null +++ b/spec/internal/db/schema.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +ActiveRecord::Schema.define do + # +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 766ef63..ee8dfe2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + $LOAD_PATH.unshift File.expand_path('../lib', __dir__) require 'simplecov' @@ -6,5 +7,13 @@ add_filter "/spec/" end +require 'bundler/setup' + require 'string_tools' require 'pry-byebug' + +require 'combustion' + +Combustion.initialize! :all + +require 'rspec/rails' diff --git a/string_tools.gemspec b/string_tools.gemspec index f59a85e..2746d30 100644 --- a/string_tools.gemspec +++ b/string_tools.gemspec @@ -1,5 +1,5 @@ -# coding: utf-8 # frozen_string_literal: true + lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'string_tools/version' @@ -20,6 +20,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ['lib'] + spec.add_runtime_dependency 'rails', '>= 4.2', '< 5' spec.add_runtime_dependency 'actionpack', '>= 4.2.0' spec.add_runtime_dependency 'activesupport', '>= 4.2.0' spec.add_runtime_dependency 'rchardet19', '~> 1.3.5' @@ -28,10 +29,12 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'sanitize', '>= 3.1.2' spec.add_runtime_dependency 'nokogiri' spec.add_runtime_dependency 'simpleidn', '>= 0.0.5' + spec.add_runtime_dependency 'pg' spec.add_development_dependency 'bundler', '~> 1.17.3' spec.add_development_dependency 'rake' - spec.add_development_dependency 'rspec', '>= 3.4' + spec.add_development_dependency 'rspec-rails', '~> 3.0' + spec.add_development_dependency 'combustion', '>= 0.5.4' spec.add_development_dependency 'appraisal', '>= 1.0.2' spec.add_development_dependency 'simplecov', '>= 0.9' spec.add_development_dependency 'pry-byebug'