rails-sass-images 0.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9806128e2e822ebd8108ca48e2b066a9f7ffa5fa
4
- data.tar.gz: fbe35722a0d6dc11de4668e286acb1e5b0211c2f
3
+ metadata.gz: 0a313b8d58d03d3b7bc2820f00b72a769c19f839
4
+ data.tar.gz: c7930af69f98b6526ef628164fab6ec83c25f7b3
5
5
  SHA512:
6
- metadata.gz: ec1f223607e5e7afa37bfbf065bdc52463a77ff98de4448c241fe84712c8bf75642f6e2c2a7fed2553573163480ef022e714568f6f28128ee3c9fbb5cde18fba
7
- data.tar.gz: 56a66de671a3cbe7722ef50c5ff4516f35d16b4dea61b1f73aded30cd8803943ccbcc394999d497dc21b59f75e0852f997a6b766a2b2beadb07a3e77e8a56eef
6
+ metadata.gz: 7a41f969226d64fa32f8939bba614e242c004f10083f757287ea40267f5a66fb5c622e0f9251838da531eff741afbed5f8282caedb8c4ff579efbf27e9d5ae31
7
+ data.tar.gz: 23a160ecca764803a1e26a70376ab36438cc73f509806d00d690008c0131e6165b3908357d359dca959ebd2a7c981a6830f79d5d6eec15918c353c4b8e424e8e
data/ChangeLog.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 0.5 “Gentile Bellini”
2
+ * Allow to use gem without Sprockets.
3
+ * Add `hidpi-background` mixin for repeated backgrounds.
4
+
1
5
  ### 0.4 “Alessandro di Mariano di Vanni Filipepi”
2
6
  * Allow to work in Rails with disabled `initialize_on_precompile`.
3
7
 
data/README.md CHANGED
@@ -61,7 +61,7 @@ and import mixins in your `application.sass`:
61
61
  @import "rails-sass-images"
62
62
  ```
63
63
 
64
- # Other
64
+ ### Sprockets
65
65
 
66
66
  You can use Rails Sass Images with plain Ruby application with Sprockets.
67
67
  Just install in to Sprockets environment:
@@ -77,3 +77,15 @@ and import mixins in your Sass files:
77
77
  ```sass
78
78
  @import "rails-sass-images"
79
79
  ```
80
+
81
+ ### Other
82
+
83
+ You can use Rails Sass Images without Sprockets. Just set dir to load assets:
84
+
85
+ ```ruby
86
+ require 'rails-sass-images'
87
+
88
+ RailsSassImages.load_from = './images/'
89
+ ```
90
+
91
+ By default, load dir will be current dir `.`.
@@ -1,3 +1,4 @@
1
1
  @import "rails-sass-images/image-size"
2
2
  @import "rails-sass-images/hidpi-image"
3
3
  @import "rails-sass-images/hidpi-inline"
4
+ @import "rails-sass-images/hidpi-background"
@@ -0,0 +1,8 @@
1
+ // Set HiDPI (Retina) image background.
2
+ //
3
+ // .loader
4
+ // background-repeat: repeat-x
5
+ // +hidpi-background("nice/button.png")
6
+ @mixin hidpi-background($path)
7
+ background-image: image-url($path)
8
+ background-size: (image-width($path) / 2) (image-height($path) / 2)
@@ -4,5 +4,5 @@
4
4
  // +hidpi-image("nice/button.png")
5
5
  @mixin hidpi-image($path)
6
6
  +image-size($path, 2)
7
- background: image-url($path) no-repeat
8
- background-size: (image-width($path) / 2) (image-height($path) / 2)
7
+ +hidpi-background($path)
8
+ background-repeat: no-repeat
@@ -4,37 +4,49 @@ module RailsSassImages
4
4
  # Return asset by file `path` from Sass parser
5
5
  def self.asset(path)
6
6
  path = path.value
7
- asset = self.assets[path]
8
- raise "Can't find asset #{path}" unless asset
7
+
8
+ @load_from = @load_from.call() if @load_from.is_a? Proc
9
+
10
+ if @load_from.is_a? Pathname
11
+ asset = @load_from.join(path)
12
+ raise "Can't find asset #{path} in #{@load_from}" unless asset.exist?
13
+ elsif sprockets? @load_from
14
+ asset = @load_from[path]
15
+ raise "Can't find asset #{path}" unless asset
16
+ asset = asset.pathname
17
+ end
18
+
9
19
  asset
10
20
  end
11
21
 
12
22
  # Set Sprockets environment and add Rails Sass Images styles paths
13
23
  def self.install(sprockets)
14
24
  sprockets.append_path(Pathname(__FILE__).dirname.join('assets/stylesheets'))
15
- self.assets = sprockets
25
+ @load_from = sprockets
16
26
  end
17
27
 
18
- # Set Sprockets environment
19
- def self.assets=(env = nil)
20
- @assets = env
28
+ # Set Sprockets environment or assets dir path
29
+ def self.load_from=(source)
30
+ source = Pathname(source) if source.is_a? String
31
+ @load_from = source
21
32
  end
22
33
 
23
- # Get Sprockets environment
24
- def self.assets
25
- if @assets
26
- @assets
27
- elsif @assets_loader
28
- @assets = @assets_loader.call()
29
- end
34
+ # Get Sprockets environment or assets dir path
35
+ def self.load_from
36
+ @load_from
30
37
  end
31
38
 
32
- # Set lazy loader for assets
33
- def self.assets_loader=(loader)
34
- @assets_loader = loader
39
+ private
40
+
41
+ # Safe detect is `var` is a Sprockets environment
42
+ def self.sprockets?(var)
43
+ return false unless defined? Sprockets
44
+ var.is_a? Sprockets::Environment or var.is_a? Sprockets::Index
35
45
  end
36
46
  end
37
47
 
48
+ RailsSassImages.load_from = '.'
49
+
38
50
  dir = Pathname(__FILE__).dirname.join('rails-sass-images')
39
51
  require dir.join('version')
40
52
  require dir.join('sass')
@@ -5,6 +5,6 @@ module RailsSassImages
5
5
 
6
6
  # Rails integration
7
7
  class Railtie < Rails::Railtie
8
- RailsSassImages.assets_loader = proc { Rails.application.assets }
8
+ RailsSassImages.load_from = proc { Rails.application.assets }
9
9
  end
10
10
  end
@@ -12,8 +12,8 @@ module RailsSassImages::Sass
12
12
  def inline(path)
13
13
  asset = RailsSassImages.asset(path)
14
14
 
15
- mime = MIME::Types.type_for(asset.pathname.to_s).first.content_type
16
- file = asset.pathname.read
15
+ mime = MIME::Types.type_for(asset.to_s).first.content_type
16
+ file = asset.read
17
17
  file = [file].flatten.pack('m').gsub("\n", '')
18
18
 
19
19
  Sass::Script::String.new("url('data:#{mime};base64,#{file}')")
@@ -4,12 +4,12 @@ module RailsSassImages::Sass
4
4
  # Get image width
5
5
  def image_width(path)
6
6
  asset = RailsSassImages.asset(path)
7
- Sass::Script::Number.new(Dimensions.width(asset.pathname), ["px"])
7
+ Sass::Script::Number.new(Dimensions.width(asset), ["px"])
8
8
  end
9
9
 
10
10
  # Get image height
11
11
  def image_height(path)
12
12
  asset = RailsSassImages.asset(path)
13
- Sass::Script::Number.new(Dimensions.height(asset.pathname), ["px"])
13
+ Sass::Script::Number.new(Dimensions.height(asset), ["px"])
14
14
  end
15
15
  end
@@ -1,3 +1,3 @@
1
1
  module RailsSassImages
2
- VERSION = '0.4'.freeze unless defined? RailsSassImages::VERSION
2
+ VERSION = '0.5'.freeze unless defined? RailsSassImages::VERSION
3
3
  end
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
19
19
  s.license = 'MIT'
20
20
 
21
21
  s.add_dependency 'sass', ['> 0']
22
- s.add_dependency 'sprockets', ['> 0']
23
22
  s.add_dependency 'dimensions', ['> 0']
24
23
  s.add_dependency 'mime-types', ['> 0']
25
24
  end
@@ -0,0 +1,4 @@
1
+ @import "rails-sass-images"
2
+
3
+ .icon
4
+ +hidpi-background("monolith.png")
@@ -0,0 +1,3 @@
1
+ .icon
2
+ width: image-width("spec/app/app/assets/images/monolith.png")
3
+ height: image-height("spec/app/app/assets/images/monolith.png")
data/spec/plain_spec.rb CHANGED
@@ -4,7 +4,7 @@ require 'sprockets'
4
4
 
5
5
  describe RailsSassImages do
6
6
  before do
7
- @original = RailsSassImages.assets
7
+ @original = RailsSassImages.load_from
8
8
 
9
9
  @assets = Sprockets::Environment.new
10
10
  @assets.append_path(DIR.join('app/app/assets/images'))
@@ -16,51 +16,87 @@ describe RailsSassImages do
16
16
  "/assets/#{path}"
17
17
  end
18
18
  end
19
-
20
- RailsSassImages.install(@assets)
21
19
  end
22
20
 
23
21
  after do
24
- RailsSassImages.assets = @original
25
- RailsSassImages.assets_loader = nil
22
+ RailsSassImages.load_from = @original
26
23
  end
27
24
 
28
- it "loads assets lazy" do
29
- RailsSassImages.assets = nil
30
- another = Sprockets::Environment.new
25
+ describe 'assets loading' do
31
26
 
32
- RailsSassImages.assets_loader = proc { another }
27
+ it "loads from dir" do
28
+ RailsSassImages.load_from = DIR.join('app/app/assets/images')
29
+ @assets['size.css'].to_s.should == ".icon{width:4px;height:6px}\n"
30
+ end
33
31
 
34
- RailsSassImages.assets.should == another
35
- end
32
+ it "loads by default from current dir" do
33
+ RailsSassImages.load_from = '.'
34
+ @assets['relative.css'].to_s.should == ".icon{width:4px;height:6px}\n"
35
+ end
36
36
 
37
- it "inlines assets" do
38
- @assets['inline.css'].to_s.should == ".icon{background:#{INLINE}}\n"
39
- end
37
+ it "loads assets from sprockets" do
38
+ RailsSassImages.load_from = @assets
39
+ @assets['size.css'].to_s.should == ".icon{width:4px;height:6px}\n"
40
+ end
40
41
 
41
- it "raises error on unknown file" do
42
- proc {
43
- @assets['wrong-inline.css']
44
- }.should raise_error(/Can't find asset no\.png/)
45
- end
42
+ it "loads assets lazy" do
43
+ RailsSassImages.load_from = proc { @assets }
44
+ @assets['size.css'].to_s.should == ".icon{width:4px;height:6px}\n"
45
+ end
46
46
 
47
- it "gets image size" do
48
- @assets['size.css'].to_s.should == ".icon{width:4px;height:6px}\n"
49
- end
47
+ it "raises error on unknown file in assets" do
48
+ RailsSassImages.load_from = @assets
49
+ proc {
50
+ @assets['wrong-inline.css']
51
+ }.should raise_error(/Can't find asset no\.png/)
52
+ end
50
53
 
51
- it "gets image size by mixin" do
52
- @assets['image-size.css'].to_s.should == ".icon{width:4px;height:6px}\n"
53
- end
54
+ it "raises error on unknown file in path" do
55
+ RailsSassImages.load_from = '.'
56
+ proc {
57
+ @assets['wrong-inline.css']
58
+ }.should raise_error(/Can't find asset no\.png in \./)
59
+ end
54
60
 
55
- it "has hidpi-image mixin" do
56
- @assets['hidpi-image.css'].to_s == ".icon{width:2px;height:3px;" +
57
- "background:url(/assets/monolith.png) no-repeat;" +
58
- "background-size:2px 3px}\n"
59
61
  end
60
62
 
61
- it "has hidpi-inline mixin" do
62
- @assets['hidpi-inline.css'].to_s.should == ".icon{width:2px;height:3px;" +
63
- "background:#{INLINE} no-repeat;" +
64
- "background-size:2px 3px}\n"
63
+ describe 'mixins' do
64
+ before do
65
+ RailsSassImages.install(@assets)
66
+ end
67
+
68
+ it "inlines assets" do
69
+ @assets['inline.css'].to_s.should == ".icon{background:#{INLINE}}\n"
70
+ end
71
+
72
+ it "gets image size" do
73
+ @assets['size.css'].to_s.should == ".icon{width:4px;height:6px}\n"
74
+ end
75
+
76
+ it "gets image size by mixin" do
77
+ @assets['image-size.css'].to_s.should == ".icon{width:4px;height:6px}\n"
78
+ end
79
+
80
+ it "has hidpi-background mixin" do
81
+ @assets['hidpi-background.css'].to_s.should == (".icon{" +
82
+ "background-image:url(/assets/monolith.png);" +
83
+ "background-size:2px 3px}\n")
84
+ end
85
+
86
+ it "has hidpi-image mixin" do
87
+ @assets['hidpi-image.css'].to_s.should == ".icon{" +
88
+ "width:2px;height:3px;" +
89
+ "background-image:url(/assets/monolith.png);" +
90
+ "background-size:2px 3px;" +
91
+ "background-repeat:no-repeat}\n"
92
+ end
93
+
94
+ it "has hidpi-inline mixin" do
95
+ @assets['hidpi-inline.css'].to_s.should == ".icon{" +
96
+ "width:2px;height:3px;" +
97
+ "background:#{INLINE} no-repeat;" +
98
+ "background-size:2px 3px}\n"
99
+ end
100
+
65
101
  end
66
102
  end
data/spec/rails_spec.rb CHANGED
@@ -30,18 +30,29 @@ describe CssController, type: :controller do
30
30
  response.body.should == ".icon{width:4px;height:6px}\n"
31
31
  end
32
32
 
33
+ it "has hidpi-background mixin" do
34
+ get :test, file: 'hidpi-background'
35
+ response.should be_success
36
+ response.body.should == ".icon{" +
37
+ "background-image:url(/assets/monolith.png);" +
38
+ "background-size:2px 3px}\n"
39
+ end
40
+
33
41
  it "has hidpi-image mixin" do
34
42
  get :test, file: 'hidpi-image'
35
43
  response.should be_success
36
- response.body.should == ".icon{width:2px;height:3px;" +
37
- "background:url(/assets/monolith.png) no-repeat;" +
38
- "background-size:2px 3px}\n"
44
+ response.body.should == ".icon{" +
45
+ "width:2px;height:3px;" +
46
+ "background-image:url(/assets/monolith.png);" +
47
+ "background-size:2px 3px;" +
48
+ "background-repeat:no-repeat}\n"
39
49
  end
40
50
 
41
51
  it "has hidpi-inline mixin" do
42
52
  get :test, file: 'hidpi-inline'
43
53
  response.should be_success
44
- response.body.should == ".icon{width:2px;height:3px;" +
54
+ response.body.should == ".icon{" +
55
+ "width:2px;height:3px;" +
45
56
  "background:#{INLINE} no-repeat;" +
46
57
  "background-size:2px 3px}\n"
47
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-sass-images
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey "A.I." Sitnik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-21 00:00:00.000000000 Z
11
+ date: 2014-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sass
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: sprockets
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">"
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">"
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: dimensions
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -84,6 +70,7 @@ files:
84
70
  - README.md
85
71
  - Rakefile
86
72
  - lib/assets/stylesheets/rails-sass-images.sass
73
+ - lib/assets/stylesheets/rails-sass-images/hidpi-background.sass
87
74
  - lib/assets/stylesheets/rails-sass-images/hidpi-image.sass
88
75
  - lib/assets/stylesheets/rails-sass-images/hidpi-inline.sass
89
76
  - lib/assets/stylesheets/rails-sass-images/image-size.sass
@@ -96,10 +83,12 @@ files:
96
83
  - rails-sass-images.gemspec
97
84
  - spec/app/.gitignore
98
85
  - spec/app/app/assets/images/monolith.png
86
+ - spec/app/app/assets/stylesheets/hidpi-background.sass
99
87
  - spec/app/app/assets/stylesheets/hidpi-image.sass
100
88
  - spec/app/app/assets/stylesheets/hidpi-inline.sass
101
89
  - spec/app/app/assets/stylesheets/image-size.sass
102
90
  - spec/app/app/assets/stylesheets/inline.sass
91
+ - spec/app/app/assets/stylesheets/relative.sass
103
92
  - spec/app/app/assets/stylesheets/size.sass
104
93
  - spec/app/app/assets/stylesheets/wrong-inline.sass
105
94
  - spec/app/app/controllers/application_controller.rb