artwork 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -33,10 +33,9 @@ you have a _2x versions of your thumbs, the helper will choose the _2x one.
33
33
 
34
34
  ## Requirements
35
35
 
36
+ - Ruby 1.8.7 or newer
36
37
  - Rails 2.3 or newer
37
38
  - Paperclip 2.3 or newer
38
- - jQuery
39
- - The jQuery.cookie plugin
40
39
 
41
40
  ## Installation
42
41
 
@@ -52,17 +51,9 @@ Or install it yourself as:
52
51
 
53
52
  $ gem install artwork
54
53
 
55
- Add the following in your `application.js` manifest:
54
+ Add the following line at the top of your `<head>` section:
56
55
 
57
- //= require artwork
58
-
59
- If you're using an older version of Rails which does not support the asset
60
- pipeline, run the following command from within the app to copy the necessary
61
- asset files in your `public/` folder first:
62
-
63
- ./script/generate artwork_assets
64
-
65
- And then manually include them with `javascript_include_tag` in your layout(s).
56
+ <%= activate_resolution_independence %>
66
57
 
67
58
  ## Configuration
68
59
 
data/artwork.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_dependency 'rails', '>= 2.3'
25
25
  spec.add_dependency 'paperclip', '>= 2.3'
26
+ spec.add_dependency 'uglifier'
26
27
  end
data/lib/artwork.rb CHANGED
@@ -7,4 +7,8 @@ require 'artwork/engine' if Rails.const_defined?(:Engine)
7
7
 
8
8
  module Artwork
9
9
  extend Configuration
10
+
11
+ def self.root_path
12
+ File.dirname(__FILE__)
13
+ end
10
14
  end
@@ -57,11 +57,11 @@ module Artwork
57
57
  end
58
58
 
59
59
  def fetch_2x_images_flag_from(request)
60
- request.cookies['_load2ximgs'].to_i > 0
60
+ request.cookies['_retina'].to_i > 0
61
61
  end
62
62
 
63
63
  def current_resolution_from(request)
64
- browser_width = request.cookies['_bSize'].to_i
64
+ browser_width = request.cookies['_width'].to_i
65
65
 
66
66
  return default_resolution if browser_width.zero?
67
67
 
@@ -1,3 +1,3 @@
1
1
  module Artwork
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/artwork/view.rb CHANGED
@@ -1,5 +1,14 @@
1
1
  module Artwork
2
2
  module View
3
+ def activate_resolution_independence
4
+ content_tag :script do
5
+ Thread.current[:artwork_script] ||= begin
6
+ artwork_script_path = Artwork.root_path + '/assets/javascripts/artwork.js'
7
+ Uglifier.compile(File.read(artwork_script_path)).html_safe
8
+ end
9
+ end
10
+ end
11
+
3
12
  def artwork_tag(record, attachment_name, size, options = {})
4
13
  image_tag_options = options[:image] || {}
5
14
  img_holder_options = options[:img_holder] || {}
@@ -1,62 +1,49 @@
1
- var Artwork = function () {
2
- var self = this;
3
-
4
- self.getSiteDomain = function () {
5
- var hostParts = location.host.toString().split('.');
6
- return hostParts.slice(hostParts.length - 2, hostParts.length).join('.');
7
- }
8
-
9
- self.getDevicePixelRatio = function () {
10
- if (typeof window.devicePixelRatio === 'undefined') {
11
- window.devicePixelRatio = 1.0;
12
- }
13
-
14
- return window.devicePixelRatio;
15
- }
16
-
17
- self.shouldLoad2xImages = function () {
18
- return self.getDevicePixelRatio() >= 1.5;
19
- }
20
-
21
- self.getPixelRatioCookie = function () {
22
- return $.cookie('_load2ximgs');
23
- }
24
-
25
- self.getBrowserResolutionCookie = function () {
26
- return $.cookie('_bSize');
27
- }
28
-
29
- self.setPixelRatioCookie = function () {
30
- var retinaFlag = self.shouldLoad2xImages() ? 1 : 0;
31
- $.cookie('_load2ximgs', retinaFlag, {expires: 365, domain: '.' + self.getSiteDomain()});
32
- }
33
-
34
- self.setBrowserResolutionCookie = function () {
35
- var windowSize = $(window).width() + 'x' + $(window).height();
36
- $.cookie('_bSize', windowSize, {expires: 365, domain: '.' + self.getSiteDomain()});
37
- }
38
-
39
- self.activateResolutionIndependance = function () {
40
- var oldPixelRatioCookie = self.getPixelRatioCookie();
41
- var oldBrowserResolutionCookie = self.getBrowserResolutionCookie();
42
-
43
- self.setPixelRatioCookie();
44
- self.setBrowserResolutionCookie();
45
-
46
- var cookiesWork = self.getPixelRatioCookie() !== null && self.getBrowserResolutionCookie() !== null;
47
- var pixelRatioChanged = oldPixelRatioCookie !== self.getPixelRatioCookie();
48
- var browserResolutionChanged = oldBrowserResolutionCookie !== self.getBrowserResolutionCookie();
49
-
50
- if (cookiesWork && (pixelRatioChanged || browserResolutionChanged)) {
51
- // Force reload without using the cache
52
- window.location.reload(true);
53
- return;
54
- }
55
-
56
- $(window).resize(self.setBrowserResolutionCookie);
57
- }
58
-
59
- self.activateResolutionIndependance();
60
- };
61
-
62
- Artwork.instance = new Artwork();
1
+ (function (window, documentElement) {
2
+ var cookieValidity = 365,
3
+ cookieDomain = location.host.toString().split('.').slice(-2).join('.'),
4
+ isRetina = (window.devicePixelRatio || 1.0) >= 1.5;
5
+
6
+ var getCookie = function (name) {
7
+ return (result = new RegExp('(?:^|; )_' + name + '=([^;]*)').exec(document.cookie)) ? result[1] : null;
8
+ }
9
+
10
+ var setCookie = function (name, value) {
11
+ var expires = new Date();
12
+ expires.setDate(expires.getDate() + cookieValidity);
13
+
14
+ document.cookie = [
15
+ '_' + name, '=', String(value),
16
+ '; expires=' + expires.toUTCString(),
17
+ '; domain=' + cookieDomain
18
+ ].join('');
19
+ };
20
+
21
+ var setSize = function () {
22
+ setCookie('width', window.innerWidth || documentElement.clientWidth);
23
+ };
24
+
25
+ var addEventHandlerTo = function (element, type, handler) {
26
+ if (element.addEventListener) {
27
+ element.addEventListener(type, handler, false);
28
+ } else if (element.attachEvent) {
29
+ element.attachEvent('on' + type, handler);
30
+ }
31
+ };
32
+
33
+ var oldRetina = getCookie('retina'),
34
+ oldWidth = getCookie('width');
35
+
36
+ setCookie('retina', isRetina ? 1 : 0);
37
+ setSize();
38
+
39
+ var cookiesWork = getCookie('retina') !== null,
40
+ retinaChanged = oldRetina !== getCookie('retina'),
41
+ widthChanged = oldWidth !== getCookie('width');
42
+
43
+ if (cookiesWork && (retinaChanged || widthChanged)) {
44
+ window.location.reload(true);
45
+ return;
46
+ }
47
+
48
+ addEventHandlerTo(window, 'resize', setSize);
49
+ })(window, document.documentElement);
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artwork
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dimitar Dimitrov
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2014-06-10 00:00:00 +03:00
18
+ date: 2014-06-11 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -77,6 +77,20 @@ dependencies:
77
77
  version: "2.3"
78
78
  type: :runtime
79
79
  version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: uglifier
82
+ prerelease: false
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ type: :runtime
93
+ version_requirements: *id005
80
94
  description: Automated user resolution based image size choosing for your Rails views, but done at the backend.
81
95
  email:
82
96
  - dimitar@live.bg
@@ -93,8 +107,6 @@ files:
93
107
  - README.md
94
108
  - Rakefile
95
109
  - artwork.gemspec
96
- - generators/artwork/USAGE
97
- - generators/artwork/artwork_assets_generator.rb
98
110
  - lib/artwork.rb
99
111
  - lib/artwork/configuration.rb
100
112
  - lib/artwork/controller.rb
@@ -1,7 +0,0 @@
1
- Usage:
2
-
3
- script/generate artwork_assets
4
-
5
- Copy the necessary artwork static files to your public directory. Use only with
6
- Rails versions without the Asset pipeline. For Rails 3.1 and newer the assets
7
- are available via the pipeline.
@@ -1,26 +0,0 @@
1
- require 'fileutils'
2
-
3
- class ArtworkAssetsGenerator < Rails::Generator::Base
4
- def manifest
5
- record do |m|
6
- has_assets = begin
7
- Rails.configuration.assets
8
- rescue
9
- nil
10
- end
11
-
12
- if has_assets
13
- puts <<-MESSAGE
14
- The version of Rails you're using (#{Rails.version}) supports
15
- the asset pipeline and the required assets will be automatically
16
- available for your app. You just need to require them in your
17
- manifest files. See how in the readme.
18
- MESSAGE
19
- else
20
- assets_root = File.expand_path('../../../lib/assets', __FILE__)
21
- FileUtils.cp_r Dir.glob("#{assets_root}/*"), Rails.root.join('public'), :verbose => true
22
- puts 'Done.'
23
- end
24
- end
25
- end
26
- end