diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d71158..db7b19d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +### 2.7.2 - ? + +* Make it possible to pass parameters to less.rb + Fixes https://github.com/metaskills/less-rails/issues/94 + ### 2.7.1 - 2016-09-01 * Fix Sprockets 3 integration. Thanks @suzan2go and @ccallebs! diff --git a/README.md b/README.md index 4531295..c8b0e8c 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,16 @@ Will end up acting as if you had done this below: #leftnav { .border-radius(5px); } ``` +## Passing parameters to less.rb + +You can pass any parameters that the [`less.rb`](https://github.com/cowboyd/less.rb) gem (which `less-rails` is based upon) supports by modifying `Rails.application.config.less.raw`. +For example, `less.rb` uses [lessc --relative-urls](http://lesscss.org/usage/#command-line-usage-relative-urls) by default. This means that `url('../ralative/paths.png')` for `@import`ed files will be modified according to `.less` file location. To return back to default `lessc` behavior, add these lines to `config/initializers/assets.rb`: + +```ruby +Rails.application.config.less.raw.relativeUrls = false +``` + +For more parameters supported by `less.rb`, please consult [less.rb gem](https://github.com/cowboyd/less.rb/blob/master/lib/less/defaults.rb). ## Helpers diff --git a/lib/less/rails/railtie.rb b/lib/less/rails/railtie.rb index 507228d..64b3aa0 100644 --- a/lib/less/rails/railtie.rb +++ b/lib/less/rails/railtie.rb @@ -6,6 +6,7 @@ class Railtie < ::Rails::Railtie config.less = ActiveSupport::OrderedOptions.new config.less.paths = [] config.less.compress = false + config.less.raw = ActiveSupport::OrderedOptions.new config.app_generators.stylesheet_engine :less config.before_initialize do |app| diff --git a/lib/less/rails/template_handlers.rb b/lib/less/rails/template_handlers.rb index 6c68580..d1df122 100644 --- a/lib/less/rails/template_handlers.rb +++ b/lib/less/rails/template_handlers.rb @@ -26,7 +26,7 @@ def config_to_less_parser_options(scope) paths = config_paths(scope) + scope.environment.paths local_path = scope.pathname.dirname paths += [local_path] unless paths.include? local_path - {:filename => eval_file, :line => line, :paths => paths, :dumpLineNumbers => config_from_rails(scope).line_numbers} + {:filename => eval_file, :line => line, :paths => paths, :dumpLineNumbers => config_from_rails(scope).line_numbers}.merge(config_raw(scope)) end def config_to_css_options(scope) @@ -36,6 +36,10 @@ def config_to_css_options(scope) def config_paths(scope) config_from_rails(scope)[:paths] end + + def config_raw(scope) + config_from_rails(scope)[:raw] + end def config_from_rails(scope) scope.environment.context_class.less_config diff --git a/test/cases/basics_spec.rb b/test/cases/basics_spec.rb index e4709a0..698a0a2 100644 --- a/test/cases/basics_spec.rb +++ b/test/cases/basics_spec.rb @@ -45,6 +45,21 @@ class BasicsSpec < Less::Rails::Spec end + describe 'relative path setting must be effective' do + after do + Rails.application.config.less.raw.clear + end + + it 'must use relavite paths by default' do + basics.must_match %r{body\{background-image:url\('i-have-no-imagination.png'\)\}} + end + + it 'should respond to config modification' do + Rails.application.config.less.raw.relativeUrls = false + basics.must_match %r{body\{background-image:url\('../../i-have-no-imagination.png'\)\}} + end + end + protected def basics diff --git a/test/dummy_app/app/assets/stylesheets/basics.css.less b/test/dummy_app/app/assets/stylesheets/basics.css.less index 2cfe8f6..90e64dc 100644 --- a/test/dummy_app/app/assets/stylesheets/basics.css.less +++ b/test/dummy_app/app/assets/stylesheets/basics.css.less @@ -16,3 +16,5 @@ // Vendored @import "vendored"; #test-vendored { .vendored; } + +@import "frameworks/bootstrap/relative_path_url"; diff --git a/test/dummy_app/app/assets/stylesheets/frameworks/bootstrap/relative_path_url.less b/test/dummy_app/app/assets/stylesheets/frameworks/bootstrap/relative_path_url.less new file mode 100644 index 0000000..a05469f --- /dev/null +++ b/test/dummy_app/app/assets/stylesheets/frameworks/bootstrap/relative_path_url.less @@ -0,0 +1,3 @@ +body { + background-image: url('../../i-have-no-imagination.png'); +}