less-rails 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ree
data/CHANGELOG.md CHANGED
@@ -1,6 +1,12 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
+ 2.0.3 - 11/13/2011
5
+ ------------------
6
+ * Add generator support. Fixes #8.
7
+ * Add a Less::Rails::CssCompressor if config.assets.compress is true. Fixes #7.
8
+
9
+
4
10
  2.0.2 - 10/09/2011
5
11
  ------------------
6
12
  * Extend LESS with Rails asset pipeline helpers.
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  This gem provides integration for Rails projects using the Less stylesheet language in the asset pipeline.
4
4
 
5
5
 
6
+
6
7
  ## Installing
7
8
 
8
9
  Just bundle up less-rails in your Gemfile. This will pull in less as a runtime dependency too.
@@ -10,6 +11,7 @@ Just bundle up less-rails in your Gemfile. This will pull in less as a runtime d
10
11
  gem 'less-rails'
11
12
 
12
13
 
14
+
13
15
  ## Configuration
14
16
 
15
17
  This gem was made for other gems to properly hook into one place to provide paths to the Less::Parser. For example, the less-rails-bootstrap project at http://github.com/metaskills/less-rails-bootstrap and each project should do the path configuration for you. If you need to, you can configure less-rails with additional paths. These paths have higher priority than those from your applications assets load paths.
@@ -21,6 +23,11 @@ MyProject::Application.configure do
21
23
  end
22
24
  ```
23
25
 
26
+ #### About Compression
27
+
28
+ If `config.assets.compress` is set to true, we will set the `config.assets.css_compressor` to `Less::Rails::CssCompressor.new` for you automatically. Feel free to set the `css_compressor` to something more stronger like `:yui` in your `config/environments/production.rb` file.
29
+
30
+
24
31
 
25
32
  ## Helpers
26
33
 
@@ -54,6 +61,33 @@ asset-data-url("rails.png") /* Becomes: url(data:image/png;base64,iVBORw0K
54
61
  Please note that these helpers are only available server-side, and something like ERB templates should be used if client-side rendering is desired.
55
62
 
56
63
 
64
+
65
+ ## Generators
66
+
67
+ Installation of the gem will set your applications stylesheet engine to use Less. It is possible to have many gems that set the stylesheet engine, for instance the sass-rails and/or stylus gems. In this case, you can resolve the ambiguity by setting the stylesheet engine in your `config/application.rb` file like so. Doing so would mean all generated assets will be in the a fresh `css.less` template.
68
+
69
+ ```ruby
70
+ config.app_generators.stylesheet_engine :less
71
+ ```
72
+
73
+ We have generators for both assets and scaffold in the `less` namespace. For instance the following would generate a blank `app/assets/stylesheets/posts.css.less` template.
74
+
75
+ ```
76
+ $ rails generate less:assets posts
77
+ ```
78
+
79
+ We also have a generator for rails scaffold CSS. Just like the Sass gem, we simply parse the scaffold.css in the default rails generator and save it as a scaffolds.css.less file. This is done automatically during other scaffold generator actions.
80
+
81
+
82
+
83
+ ## Testing
84
+
85
+ Simply just run `bundle install` and `bundle exec rake` and the tests will will. We also use Travis CI to run our tests too. Current build status is:
86
+
87
+ [![Build Status](https://secure.travis-ci.org/metaskills/less-rails.png)](http://travis-ci.org/metaskills/less-rails)
88
+
89
+
90
+
57
91
  ## License
58
92
 
59
93
  Less::Rails is Copyright (c) 2011 Ken Collins, <ken@metaskills.net> and is distributed under the MIT license.
data/lib/less/rails.rb CHANGED
@@ -12,4 +12,5 @@ require 'sprockets/railtie'
12
12
  require 'less/rails/version'
13
13
  require 'less/rails/helpers'
14
14
  require 'less/rails/template_handlers'
15
+ require 'less/rails/css_compressor'
15
16
  require 'less/rails/railtie'
@@ -0,0 +1,11 @@
1
+ module Less
2
+ module Rails
3
+ class CssCompressor
4
+
5
+ def compress(css)
6
+ ::Less::Parser.new.parse(css).to_css(:compress => true).strip
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -9,6 +9,7 @@ module Less
9
9
  config.less = ActiveSupport::OrderedOptions.new
10
10
  config.less.paths = []
11
11
  config.less.compress = false
12
+ config.app_generators.stylesheet_engine :less
12
13
 
13
14
  config.before_initialize do |app|
14
15
  require 'less'
@@ -26,6 +27,12 @@ module Less
26
27
  app.assets.context_class.less_config = app.config.less
27
28
  end
28
29
 
30
+ initializer 'less-rails.setup_compression', :group => :all do |app|
31
+ if app.config.assets.compress
32
+ app.config.assets.css_compressor = CssCompressor.new
33
+ end
34
+ end
35
+
29
36
  end
30
37
  end
31
38
  end
@@ -1,5 +1,5 @@
1
1
  module Less
2
2
  module Rails
3
- VERSION = "2.0.2"
3
+ VERSION = "2.0.3"
4
4
  end
5
5
  end
@@ -0,0 +1,15 @@
1
+ require "rails/generators/named_base"
2
+
3
+ module Less
4
+ module Generators
5
+ class AssetsGenerator < ::Rails::Generators::NamedBase
6
+
7
+ source_root File.expand_path '../templates', __FILE__
8
+
9
+ def copy_less
10
+ template 'stylesheet.css.less', File.join('app/assets/stylesheets', class_path, "#{file_name}.css.less")
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ // Place all the styles related to the <%= name %> controller here.
2
+ // They will automatically be included in application.css.
3
+ // You can use Less here: http://lesscss.org/
@@ -0,0 +1,17 @@
1
+ require "less"
2
+ require "rails/generators/named_base"
3
+ require "rails/generators/rails/scaffold/scaffold_generator"
4
+
5
+ module Less
6
+ module Generators
7
+ class ScaffoldGenerator < ::Rails::Generators::NamedBase
8
+
9
+ def copy_stylesheet
10
+ file = File.join ::Rails::Generators::ScaffoldGenerator.source_root, 'scaffold.css'
11
+ css = Less::Parser.new.parse(File.read(file)).to_css
12
+ create_file "app/assets/stylesheets/scaffolds.css.less", css
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -12,5 +12,8 @@ class BasicsSpec < Less::Rails::Spec
12
12
  basics.must_match %r{#test-mixin span\{border:1px solid black;\}}
13
13
  end
14
14
 
15
+ it 'must have a CSSCompressor' do
16
+ Less::Rails::CssCompressor.new.compress('.class {width: 1+1}').must_equal '.class{width:2;}'
17
+ end
15
18
 
16
19
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ require 'rails/generators/less/assets/assets_generator'
3
+ require 'rails/generators/less/scaffold/scaffold_generator'
4
+
5
+ class AssetsGeneratorSpec < Less::Rails::GeneratorSpec
6
+
7
+ it 'should generate a posts.css.less file' do
8
+ run_generator ['posts']
9
+ assert_file 'app/assets/stylesheets/posts.css.less' do |contents|
10
+ contents.must_match %r{Place all the styles related to the posts controller here}
11
+ contents.must_match %r{You can use Less here}
12
+ end
13
+ end
14
+
15
+ end
16
+
17
+ class ScaffoldGeneratorSpec < Less::Rails::GeneratorSpec
18
+
19
+ it 'should parse and copy the scaffold to a less file' do
20
+ run_generator ['posts']
21
+ assert_file 'app/assets/stylesheets/scaffolds.css.less' do |contents|
22
+ contents.must_match %r{background-color: #fff}
23
+ end
24
+ end
25
+
26
+ end
27
+
data/test/spec_helper.rb CHANGED
@@ -5,18 +5,20 @@ require 'less/rails'
5
5
  require 'minitest/spec'
6
6
  require 'minitest/autorun'
7
7
  require 'dummy_app/init'
8
+ require 'rails/generators'
9
+ require 'fileutils'
8
10
 
9
11
  module Less
10
12
  module Rails
13
+
11
14
  class Spec < MiniTest::Spec
12
-
15
+
13
16
  before do
14
17
  reset_config_options
15
18
  reset_caches
16
19
  end
17
20
 
18
-
19
- private
21
+ protected
20
22
 
21
23
  def dummy_app
22
24
  Dummy::Application
@@ -45,6 +47,75 @@ module Less
45
47
  end
46
48
 
47
49
  end
50
+
51
+ # Heavily inspired by Rails::Generators::TestCase.
52
+ class GeneratorSpec < Spec
53
+
54
+ include FileUtils
55
+
56
+ class_attribute :destination_root, :current_path, :generator_class, :default_arguments
57
+ delegate :destination_root, :current_path, :generator_class, :default_arguments, :to => :'self.class'
58
+
59
+ self.current_path = File.expand_path(Dir.pwd)
60
+ self.default_arguments = []
61
+ self.destination_root = "#{Dummy::Application.root}/tmp"
62
+
63
+ before { destination_root_is_set? ; ensure_current_path ; prepare_destination ; no_color! ; setup_generator_class }
64
+ after { remove_destination ; ensure_current_path }
65
+
66
+ protected
67
+
68
+ def no_color!
69
+ Thor::Base.shell = Thor::Shell::Basic
70
+ end
71
+
72
+ def destination_root_is_set?
73
+ raise "You need to configure your Less::Rails::GeneratorSpec destination root." unless destination_root
74
+ end
75
+
76
+ def ensure_current_path
77
+ cd current_path
78
+ end
79
+
80
+ def prepare_destination
81
+ remove_destination
82
+ mkdir_p destination_root
83
+ end
84
+
85
+ def remove_destination
86
+ rm_rf destination_root
87
+ end
88
+
89
+ def setup_generator_class
90
+ self.class.generator_class = Less::Generators.const_get(self.class.name.sub(/Spec$/, ''))
91
+ end
92
+
93
+ def run_generator(args=default_arguments, config={})
94
+ capture(:stdout) { generator_class.start(args, config.reverse_merge(:destination_root => destination_root)) }
95
+ end
96
+
97
+ def generator(args=default_arguments, options={}, config={})
98
+ @generator ||= generator_class.new(args, options, config.reverse_merge(:destination_root => destination_root))
99
+ end
100
+
101
+ def assert_file(relative, *contents)
102
+ absolute = File.expand_path(relative, destination_root)
103
+ assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not"
104
+ read = File.read(absolute) if block_given? || !contents.empty?
105
+ yield read if block_given?
106
+ contents.each do |content|
107
+ case content
108
+ when String
109
+ assert_equal content, read
110
+ when Regexp
111
+ assert_match content, read
112
+ end
113
+ end
114
+ end
115
+ alias :assert_directory :assert_file
116
+
117
+ end
118
+
48
119
  end
49
120
  end
50
121
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: less-rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 2
10
- version: 2.0.2
9
+ - 3
10
+ version: 2.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ken Collins
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-09 00:00:00 -04:00
18
+ date: 2011-11-13 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -105,6 +105,7 @@ extra_rdoc_files: []
105
105
 
106
106
  files:
107
107
  - .gitignore
108
+ - .travis.yml
108
109
  - CHANGELOG.md
109
110
  - Gemfile
110
111
  - Guardfile
@@ -114,11 +115,16 @@ files:
114
115
  - less-rails.gemspec
115
116
  - lib/less-rails.rb
116
117
  - lib/less/rails.rb
118
+ - lib/less/rails/css_compressor.rb
117
119
  - lib/less/rails/helpers.rb
118
120
  - lib/less/rails/railtie.rb
119
121
  - lib/less/rails/template_handlers.rb
120
122
  - lib/less/rails/version.rb
123
+ - lib/rails/generators/less/assets/assets_generator.rb
124
+ - lib/rails/generators/less/assets/templates/stylesheet.css.less
125
+ - lib/rails/generators/less/scaffold/scaffold_generator.rb
121
126
  - test/cases/basics_spec.rb
127
+ - test/cases/generators_spec.rb
122
128
  - test/cases/helpers_spec.rb
123
129
  - test/cases/railtie_spec.rb
124
130
  - test/dummy_app/app/assets/images/1x1.png
@@ -163,6 +169,7 @@ specification_version: 3
163
169
  summary: The dynamic stylesheet language for the Rails asset pipeline.
164
170
  test_files:
165
171
  - test/cases/basics_spec.rb
172
+ - test/cases/generators_spec.rb
166
173
  - test/cases/helpers_spec.rb
167
174
  - test/cases/railtie_spec.rb
168
175
  - test/dummy_app/app/assets/images/1x1.png