artwork 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in artwork.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dimitar Dimitrov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Artwork
2
+
3
+ Automated image size scaling view helpers for your frontend, but done in the
4
+ backend. Works for any browser. Delivers the information needed for the
5
+ calculations (browser window's dimentions and device's pixel ratio) via a
6
+ cookie. Supports only Paperclip attachments.
7
+
8
+ Example usage:
9
+
10
+ <%= artwork_tag person, :avatar, '100x' %>
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'artwork'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install artwork
25
+
26
+ Add the following in your `application.js` manifest:
27
+
28
+ //= require artwork
29
+
30
+ If you're using an older version of Rails which does not support the asset
31
+ pipeline, run the following command from within the app to copy the necessary
32
+ asset files in your `public/` folder first:
33
+
34
+ ./script/generate artwork_assets
35
+
36
+ And then manually include them with `javascript_include_tag` in your layout(s).
37
+
38
+ ## Configuration
39
+
40
+ Set the following variables in an app initializer:
41
+
42
+ - `Artwork.supported_resolutions_list`
43
+ - `Artwork.default_resolution`
44
+
45
+ Name your Paperclip attachment styles using the following convention:
46
+
47
+ :'320x'
48
+ :'320x_2x'
49
+ :'320x_some_label'
50
+ :'320x_some_label_2x'
51
+ :'320x500'
52
+ :'320x500_2x'
53
+ :'320x500_crop'
54
+ :'320x500_crop_2x'
55
+ :'320x500_black_and_white_crop'
56
+ :'320x500_black_and_white_crop_2x'
57
+
58
+ The artwork methods will recognize and work with these styles. All other naming
59
+ conventions will be ignored and will bypass the artwork auto-sizing logic.
60
+
61
+ ## Usage Example
62
+
63
+ Configure the gem by putting the following code in `config/initializers/artwork.rb`:
64
+
65
+ Artwork.default_resolution = 1440
66
+ Artwork.supported_resolutions_list = [1024, 1280, 1440, 1600, 1920, 2048, 3200, 3840]
67
+
68
+ Include `Artwork::Model` in your models which have artworks.
69
+
70
+ Include `Artwork::Model` in your `ApplicationController` or wherever you want
71
+ to have the artwork functionality.
72
+
73
+ Then you can use `Artwork.load_2x_images?`, `Artwork.current_resolution` and
74
+ the `artwork_tag` view helper. Example:
75
+
76
+ <%= artwork_tag @film, :board, :'1440x', :image => {:class => 'poster'} %>
77
+ <%= artwork_tag @gallery, :cover, :'900x' %>
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it ( https://github.com/[my-github-username]/artwork/fork )
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+
data/artwork.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'artwork/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'artwork'
8
+ spec.version = Artwork::VERSION
9
+ spec.authors = ['Dimitar Dimitrov']
10
+ spec.email = ['dimitar@live.bg']
11
+ spec.summary = 'Automated image size scaling view helpers.'
12
+ spec.description = 'Works for any browser and uses cookies for transporting browser info to the backend.'
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+
24
+ spec.add_dependency 'rails', '>= 2.3'
25
+ spec.add_dependency 'paperclip', '>= 2.3'
26
+ end
@@ -0,0 +1,7 @@
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.
@@ -0,0 +1,26 @@
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
data/lib/artwork.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'artwork/version'
2
+ require 'artwork/configuration'
3
+ require 'artwork/model'
4
+ require 'artwork/view'
5
+ require 'artwork/controller'
6
+ require 'artwork/engine' if Rails.const_defined?(:Engine)
7
+
8
+ module Artwork
9
+ extend Configuration
10
+ end
@@ -0,0 +1,75 @@
1
+ module Artwork
2
+ module Configuration
3
+ def supported_resolutions_list
4
+ get_required :supported_resolutions_list
5
+ end
6
+
7
+ def supported_resolutions_list=(resolutions)
8
+ set :supported_resolutions_list, resolutions.map(&:to_i).sort
9
+ end
10
+
11
+ def default_resolution
12
+ get_required :default_resolution
13
+ end
14
+
15
+ def default_resolution=(resolution)
16
+ set :default_resolution, resolution
17
+ end
18
+
19
+ def load_2x_images?
20
+ get(:load_2x_images) || false
21
+ end
22
+
23
+ def load_2x_images=(flag)
24
+ set :load_2x_images, flag
25
+ end
26
+
27
+ def current_resolution
28
+ get(:current_resolution) || default_resolution
29
+ end
30
+
31
+ def current_resolution=(resolution)
32
+ set :current_resolution, resolution
33
+ end
34
+
35
+ def configure_for(request)
36
+ Artwork.load_2x_images = fetch_2x_images_flag_from(request)
37
+ Artwork.current_resolution = current_resolution_from(request)
38
+ end
39
+
40
+ def reset_configuration
41
+ set :current_resolution, nil
42
+ set :load_2x_images, nil
43
+ end
44
+
45
+ private
46
+
47
+ def set(setting, value)
48
+ Thread.current[setting] = value
49
+ end
50
+
51
+ def get(setting)
52
+ Thread.current[setting]
53
+ end
54
+
55
+ def get_required(setting)
56
+ get(setting) or raise "Please set #{name}.#{setting}"
57
+ end
58
+
59
+ def fetch_2x_images_flag_from(request)
60
+ request.cookies['_load2ximgs'].to_i > 0
61
+ end
62
+
63
+ def current_resolution_from(request)
64
+ browser_width = request.cookies['_bSize'].to_i
65
+
66
+ return default_resolution if browser_width.zero?
67
+
68
+ supported_resolutions_list.each do |resolution|
69
+ return resolution if browser_width <= resolution
70
+ end
71
+
72
+ supported_resolutions_list.last
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,19 @@
1
+ module Artwork
2
+ module Controller
3
+ def self.included(controller)
4
+ controller.class_eval do
5
+ around_filter :initialize_artwork_env
6
+ helper View
7
+ end
8
+ end
9
+
10
+ private
11
+
12
+ def initialize_artwork_env
13
+ Artwork.configure_for request
14
+ yield
15
+ ensure
16
+ Artwork.reset_configuration
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module Artwork
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,66 @@
1
+ module Artwork
2
+ module Model
3
+ THUMBNAIL_NAME_PATTERN = /^[0-9]+x(\w*?)(_2x)?$/i.freeze
4
+
5
+ def artwork_thumb_for(attachment_name, size)
6
+ size = size.to_s
7
+ matching_thumb_name = nil
8
+
9
+ if size =~ THUMBNAIL_NAME_PATTERN
10
+ desired_size = size.to_i / ratio_for_current_resolution
11
+
12
+ available_attachments = []
13
+
14
+ # Pick attachments which follow our naming conventions, skipping retina images
15
+ attachment_styles_for(attachment_name).each do |thumb_name|
16
+ if thumb_name.to_s =~ THUMBNAIL_NAME_PATTERN
17
+ is_retina = $2
18
+ thumb_width = thumb_name.to_s.to_i
19
+
20
+ available_attachments << [thumb_name, thumb_width] unless is_retina
21
+ end
22
+ end
23
+
24
+ # Sort attachments by width, in ascending order
25
+ available_attachments = available_attachments.sort_by do |thumb_name, thumb_width|
26
+ thumb_width
27
+ end
28
+
29
+ available_attachments.each do |thumb_name, thumb_width|
30
+ if desired_size <= thumb_width
31
+ matching_thumb_name = thumb_name
32
+ break
33
+ end
34
+ end
35
+
36
+ # If we did not find any matching attachment definitions,
37
+ # the desired size is probably larger than all of our thumb widths,
38
+ # So pick the last (largest) one we have.
39
+ matching_thumb_name ||= available_attachments.last.first
40
+ end
41
+
42
+ matching_thumb_name ||= size.to_sym
43
+
44
+ if Artwork.load_2x_images? and attachment_styles_for(attachment_name).include?(:"#{matching_thumb_name}_2x")
45
+ matching_thumb_name = :"#{matching_thumb_name}_2x"
46
+ end
47
+
48
+ matching_thumb_name.to_sym
49
+ end
50
+
51
+ def artwork_url(attachment_name, size, options = {})
52
+ thumb_name = artwork_thumb_for(attachment_name, size)
53
+ send(attachment_name).url(size, options)
54
+ end
55
+
56
+ def attachment_styles_for(attachment_name)
57
+ self.class.attachment_definitions[attachment_name.to_sym][:styles].keys
58
+ end
59
+
60
+ private
61
+
62
+ def ratio_for_current_resolution
63
+ Artwork.default_resolution.to_f / Artwork.current_resolution.to_f
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module Artwork
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,38 @@
1
+ module Artwork
2
+ module View
3
+ def artwork_tag(record, attachment_name, size, options = {})
4
+ image_tag_options = options[:image] || {}
5
+ img_holder_options = options[:img_holder] || {}
6
+
7
+ image_tag_options[:alt] ||= extract_title_from(record)
8
+
9
+ image_url = record.artwork_url attachment_name, size, options
10
+
11
+ if options[:auto_height]
12
+ image = record.send(attachment_name)
13
+
14
+ if image.height.present? and image.width.present?
15
+ padding = ((image.height.to_f / image.width) * 100).round(4)
16
+
17
+ img_holder_options[:style] = "padding-bottom:#{padding}%;"
18
+ end
19
+ end
20
+
21
+ content_tag :div, :class => attachment_name do
22
+ content_tag :div, img_holder_options.merge(:class => 'img-holder') do
23
+ image_tag image_url, image_tag_options
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def extract_title_from(record)
31
+ if record.respond_to? :title
32
+ record.title
33
+ elsif record.respond_to? :name
34
+ record.name
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,62 @@
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();
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artwork
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Dimitar Dimitrov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2014-06-10 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 1
32
+ - 6
33
+ version: "1.6"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rails
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 5
59
+ segments:
60
+ - 2
61
+ - 3
62
+ version: "2.3"
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: paperclip
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 5
74
+ segments:
75
+ - 2
76
+ - 3
77
+ version: "2.3"
78
+ type: :runtime
79
+ version_requirements: *id004
80
+ description: Works for any browser and uses cookies for transporting browser info to the backend.
81
+ email:
82
+ - dimitar@live.bg
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files: []
88
+
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - artwork.gemspec
96
+ - generators/artwork/USAGE
97
+ - generators/artwork/artwork_assets_generator.rb
98
+ - lib/artwork.rb
99
+ - lib/artwork/configuration.rb
100
+ - lib/artwork/controller.rb
101
+ - lib/artwork/engine.rb
102
+ - lib/artwork/model.rb
103
+ - lib/artwork/version.rb
104
+ - lib/artwork/view.rb
105
+ - lib/assets/javascripts/artwork.js
106
+ has_rdoc: true
107
+ homepage: ""
108
+ licenses:
109
+ - MIT
110
+ post_install_message:
111
+ rdoc_options: []
112
+
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ requirements: []
134
+
135
+ rubyforge_project:
136
+ rubygems_version: 1.6.2
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: Automated image size scaling view helpers.
140
+ test_files: []
141
+