has-imgix-attachment 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24496d71ccc8016329b19bcc8a99cb4a0f6c8624
4
+ data.tar.gz: e0f03d6e4e906c0e380fb438ff7222092d0aa80d
5
+ SHA512:
6
+ metadata.gz: e1eb677b7ee1faf5edfc8a3e3f4b2c0e9e71f4d4bf5fdcfa568ad6208403351e9d89ab67291e72272c844571e882f85e839926aa1c70369920daa3682810a89f
7
+ data.tar.gz: a53c6db47b61b8a98a2e431c1c70c0280ac255d016039929757c1ee405f7b183441e297186f5dd3d0a519430ba0240350b79193cadf97f3960b971bfb9ec3b86
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in imgix.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ryan Abbott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # has-imgix-attachment
2
+
3
+ has-imgix-attachment allows you to easier work with the [imgix](http://www.imgix.com/) API through model defined attributes for easier retrieval.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'has-imgix-attachment'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Model Configuration
16
+
17
+ To add imgix to your model, you simply need to call the `has-imgix-attachment` method on your model.
18
+
19
+ ```ruby
20
+ class Post < ActiveRecord::Base
21
+ has_imgix_attachment :photo,
22
+ :subdomain => "imgix-subdomain",
23
+ :default => "default.png",
24
+ :prefix => "photos",
25
+ :styles => {
26
+ :thumbnail => {
27
+ :h => 90,
28
+ :w => 110,
29
+ :fit => "crop"
30
+ },
31
+ :large => {
32
+ :h => 480,
33
+ :w => 640,
34
+ :fit => "crop"
35
+ }
36
+ }
37
+ end
38
+ ```
39
+
40
+ The `has-imgix-attachment` method will take accept a name, and a list of options. The first value is the name and of the field that we'll create (which can also be referenced with `imgix_attachment`). Below is a list of valid options that can follow the name.
41
+
42
+ ### Options
43
+
44
+ * `subdomain` - Subdomain defined within your imgix account
45
+ * `default` - Name of the image to be used as a default image (must appear in the bucket with all other images)
46
+ * `prefix` - If your bucket has folders, supply the folder name here
47
+ * `filename_field` - Override the default column name for the image filename. Default: **filename**
48
+ * `file_size_field` - Override the default column name for the image file size. Default: **file_size**
49
+ * `content_type_field` - Override the default column name for the image content type. Default: **content_type**
50
+ * `styles` - Hash that defines all the variations of the image you'd like to be able to refer to
51
+
52
+ **NOTE:** Examples of the model configurations can be found in the [`spec/support/models.rb`](spec/support/models.rb) file.
53
+
54
+ ## Usage
55
+
56
+ The gem comes with a view helper ``imgix_tag`` which is just an extension of the ``image_tag``.
57
+
58
+ ```ruby
59
+ <%= imgix_tag(@post.photo, "large") %>
60
+ ```
61
+ will generate the same thing as, just a less verbose shortcut
62
+ ```ruby
63
+ <%= imgix_tag(@post.imgix_attachment, "large") %>
64
+ ```
65
+
66
+ imgix_tag will accept all the same additional options that image_tag supports
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ * Please make sure to include the appropriate specs
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create new Pull Request
76
+
77
+ ## License
78
+
79
+ See [LICENSE.txt](LICENSE.txt).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "imgix"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/imgix.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "imgix/version"
2
+ require 'imgix/config'
3
+ require 'imgix/style'
4
+ require 'imgix/attachment'
5
+ require 'imgix/model_extensions'
6
+ require 'imgix/instance_methods'
7
+ require 'imgix/action_view/helpers'
8
+ require 'imgix/railtie' if defined? Rails
9
+
10
+
11
+ module Imgix
12
+ end
@@ -0,0 +1,19 @@
1
+ module Imgix
2
+ module ActionView
3
+ module Helpers
4
+ # Simple image_tag override that uses imgix as the source
5
+ def imgix_tag(object, style, html_options = {})
6
+ image_tag(imgix_url(object, style), html_options)
7
+ end
8
+
9
+ # Helper that will return only a url
10
+ def imgix_url(object, style)
11
+ if object && object.respond_to?('url')
12
+ object.url(style)
13
+ else
14
+ Attachment.url(object, style)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,89 @@
1
+ module Imgix
2
+ class Attachment
3
+ attr_reader :name, :instance, :options, :styles
4
+
5
+ def initialize(name, instance, options = {})
6
+ @name = name
7
+ @instance = instance
8
+ @options = options
9
+ end
10
+
11
+ # generate the full imgix url for the provided style
12
+ def url(style = nil)
13
+ # construct the pieces of the url
14
+ full_url = [@instance.base_url, @options[:prefix], filename].compact.join("/")
15
+
16
+ # append imgix params to the filename before returning
17
+ styled_filename(full_url, style)
18
+ end
19
+
20
+ def self.url(filename, style)
21
+ # url including source previx
22
+ full_url = [@instance.base_url, @options[:prefix], filename].compact.join("/")
23
+
24
+ # add the style options and return
25
+ styled_filename(full_url, style)
26
+ end
27
+
28
+ # Determine the field/column name to find the filename in
29
+ def filename_field
30
+ @options.fetch(:filename_field, "filename")
31
+ end
32
+
33
+ # Determine the field/column name to find the content_type in
34
+ def content_type_field
35
+ @options.fetch(:content_type_field, "content_type")
36
+ end
37
+
38
+ # Determine the field/column name to find the file size in
39
+ def file_size_field
40
+ @options.fetch(:file_size_field, "file_size")
41
+ end
42
+
43
+ # Retrieve the filename from the instance provided
44
+ def filename
45
+ # attempt to get the value from the database, but fall back
46
+ # to a default image supplied
47
+ @instance.send(self.filename_field) || @options[:default]
48
+ end
49
+
50
+ # Retrieve the content type from the instance provided
51
+ def content_type
52
+ @instance.send(self.content_type_field)
53
+ end
54
+
55
+ # Retrieve the file_size from the instance provided
56
+ def file_size
57
+ @instance.send(self.file_size_field)
58
+ end
59
+
60
+ # Append query parameters to the filename of this attachment
61
+ def styled_filename(url, style)
62
+ if style.present? && self.styles.include?(style.to_sym)
63
+ return [url, self.style(style).to_s].join("?")
64
+ end
65
+
66
+ # default to the basic url, without any options
67
+ return url
68
+ end
69
+
70
+ # Retrieve a particular style of this attachment
71
+ def style(style = nil)
72
+ # look for the provided style, return an empty has by default
73
+ self.styles.fetch(style.to_sym, {})
74
+ end
75
+
76
+ # Retrieve all styles provided for this attachment
77
+ def styles
78
+ # initialize an empty hash
79
+ styles = {}
80
+
81
+ @options.fetch(:styles, {}).each_pair do |name, options|
82
+ styles[name.to_sym] = Imgix::Style.new(name, self, options)
83
+ end
84
+
85
+ # returns the hash of Imgix::Style objects
86
+ styles
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,41 @@
1
+ module Imgix
2
+ class Config
3
+ DEFAULT_IMGIX_ATTACHMENT_ATTR = :photo
4
+ DEFAULT_IMGIX_PROTOCOL = "https"
5
+ DEFAULT_IMGIX_BASE_URL = "imgix.net"
6
+
7
+ attr_reader :base_url, :subdomain, :protocol
8
+
9
+ def field_name(name = nil)
10
+ if name.nil?
11
+ @field_name || Imgix::Config::DEFAULT_IMGIX_ATTACHMENT_ATTR
12
+ else
13
+ @field_name = name
14
+ end
15
+ end
16
+
17
+ def base_url(url = nil)
18
+ if url.nil?
19
+ @base_url || Imgix::Config::DEFAULT_IMGIX_BASE_URL
20
+ else
21
+ @base_url = url
22
+ end
23
+ end
24
+
25
+ def subdomain(_subdomain = nil)
26
+ if _subdomain.nil?
27
+ @subdomain || Imgix::Config::DEFAULT_IMGIX_PROTOCOL
28
+ else
29
+ @subdomain = _subdomain
30
+ end
31
+ end
32
+
33
+ def protocol(_protocol = nil)
34
+ if _protocol.nil?
35
+ @protocol || Imgix::Config::DEFAULT_IMGIX_PROTOCOL
36
+ else
37
+ @protocol = _protocol
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,18 @@
1
+ module Imgix
2
+ module InstanceMethods
3
+ # def attachment_for(name, options)
4
+ # @_attachments ||= {}
5
+ # @_attachments[name] ||= Imgix::Attachment.new(name, self, options)
6
+ # end
7
+
8
+ # Detrmine the base url given the imgix configurations
9
+ def base_url
10
+ url = URI::Generic.build(
11
+ scheme: self.class.imgix_config.protocol(),
12
+ host: [self.class.imgix_config.subdomain(), self.class.imgix_config.base_url()].join(".")
13
+ )
14
+
15
+ return url
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_support/concern'
2
+
3
+ module Imgix
4
+ module ModelExtensions
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def imgix_config
9
+ @imgix_config ||= Imgix::Config.new
10
+ end
11
+
12
+ def has_imgix_attachment(name, options = {})
13
+ include InstanceMethods
14
+
15
+ imgix_config.base_url(options.delete(:base_url))
16
+ imgix_config.protocol(options.delete(:protocol))
17
+ imgix_config.subdomain(options.delete(:subdomain))
18
+
19
+ define_method(name) do |*args|
20
+ a = Imgix::Attachment.new(name, self, options)
21
+ end
22
+
23
+ # simple helper method to use instead of having to use
24
+ # the name provided
25
+ define_method("imgix_attachment") do ||
26
+ self.send(name)
27
+ end
28
+
29
+ # Define how the setter of our attachment works
30
+ # define_method "#{name}=" do |file|
31
+ # attachment_for(name).assign(file)
32
+ # end
33
+
34
+ # Determine if the attachment is valid
35
+ # define_method "#{name}?" do
36
+ # attachment_for(name).file?
37
+ # end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ module Imgix
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'imgix.model_additions' do
4
+ ActiveSupport.on_load(:active_record) do
5
+ include Imgix::ModelExtensions
6
+ end
7
+
8
+ ActiveSupport.on_load(:action_view) do
9
+ include Imgix::ActionView::Helpers
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ module Imgix
2
+ class Style
3
+ attr_reader :name, :options
4
+
5
+ VALID_PARAMS = [:auto, :bg, :blend, :blendalign, :blendalpha,
6
+ :blendcrop, :blendfit, :blendheight, :blendmode, :blendpadding,
7
+ :blendsize, :blendwidth, :blur, :border, :bri, :colors, :con,
8
+ :crop, :dl, :dpr, :exp, :fit, :flip, :fm, :gam, :h, :high, :htn,
9
+ :hue, :invert, :mark, :markalign, :markfit, :markh, :markpad,
10
+ :markscale, :markw, :mask, :mono, :nr, :nrs, :or, :page, :palette,
11
+ :prefix, :px, :q, :rect, :rot, :sat, :sepia, :shad, :sharp, :txt,
12
+ :txtalign, :txtclip, :txtclr, :txtfit, :txtfont, :txtline, :txtlineclr,
13
+ :txtpad, :txtshad, :txtsize, :txtwidth, :vib, :w]
14
+
15
+ def initialize(name, attachment, options = {})
16
+ @name = name
17
+ @attachment = attachment
18
+ @options = options
19
+ if @options.is_a?(Hash)
20
+ @options = @options.delete_if {|k, v| !Imgix::Style.is_param_valid?(k) }
21
+ end
22
+ end
23
+
24
+ def to_s
25
+ self.options.to_param
26
+ end
27
+
28
+ def self.is_param_valid?(param)
29
+ VALID_PARAMS.include?(param.to_sym)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Imgix
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,203 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has-imgix-attachment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Abbott
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: database_cleaner
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: capybara
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activerecord
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: activesupport
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Gem to assist with the display of imgix images
154
+ email:
155
+ - ryan@lateralventures.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".travis.yml"
163
+ - CODE_OF_CONDUCT.md
164
+ - Gemfile
165
+ - LICENSE.txt
166
+ - README.md
167
+ - Rakefile
168
+ - bin/console
169
+ - bin/setup
170
+ - lib/imgix.rb
171
+ - lib/imgix/action_view/helpers.rb
172
+ - lib/imgix/attachment.rb
173
+ - lib/imgix/config.rb
174
+ - lib/imgix/instance_methods.rb
175
+ - lib/imgix/model_extensions.rb
176
+ - lib/imgix/railtie.rb
177
+ - lib/imgix/style.rb
178
+ - lib/imgix/version.rb
179
+ homepage: ''
180
+ licenses:
181
+ - MIT
182
+ metadata: {}
183
+ post_install_message:
184
+ rdoc_options: []
185
+ require_paths:
186
+ - lib
187
+ required_ruby_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ required_rubygems_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ requirements: []
198
+ rubyforge_project:
199
+ rubygems_version: 2.4.6
200
+ signing_key:
201
+ specification_version: 4
202
+ summary: ''
203
+ test_files: []