paperclip-eitheror 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 977bcd0b0e3c7535687ef21d01803b7c616659c6
4
+ data.tar.gz: fe78f2825400ace39fc663f90d90d4fce0b1388e
5
+ SHA512:
6
+ metadata.gz: bbc423e8ba839df5f6cae5d71c84342b568bd19a67383c79e07ba9bfed32975ea41fe756710adb04480811f5f14debddf810c5568339f164926905d5a2ce18e1
7
+ data.tar.gz: 8765c21b4b21be2f15d6d7fbf2d3d13551ba873cf59ccf8433db2df9a27523085e557da80f168093c23a6c035f484d6a00bd5d3d44086956528c12c007635daa
@@ -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
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at matheus.abegg@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperclip-eitheror.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Matheus Abegg
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.
@@ -0,0 +1,199 @@
1
+ # Paperclip::Eitheror
2
+ [![Build Status](https://travis-ci.org/powerhome/paperclip-eitheror.svg?branch=master)](https://travis-ci.org/powerhome/paperclip-eitheror)
3
+
4
+ A [Paperclip](https://github.com/thoughtbot/paperclip/) Storage which supports a secondary (called 'or') storage as a fallback while using the primary one (called 'either').
5
+
6
+ The purpose of this gem is to help us while migrating our assets to a different place, a better place <3
7
+
8
+ Dependency versions are locked to the current versions we have running.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'paperclip-eitheror'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install paperclip-eitheror
25
+
26
+ ## Usage
27
+
28
+ Given you have the gem installed and some model, you need to configure your attachment with `storage: :eitheror`, and set up the primary (`either`) and the secondary/fallback (`or`) storages:
29
+
30
+ ```ruby
31
+ has_attached_file :avatar, {
32
+ storage: :eitheror,
33
+ either: {
34
+ storage: :fog
35
+ },
36
+ or: {
37
+ storage: :filesystem
38
+ }
39
+ }
40
+ ```
41
+
42
+ You can use specific configuration by passing them on one of the storages config. For instance:
43
+
44
+ ```ruby
45
+ has_attached_file :avatar, {
46
+ storage: :eitheror,
47
+ either: {
48
+ storage: :fog,
49
+ path: ':attachment/:id/:style/:filename',
50
+ url: ':attachment/:id/:style/:filename'
51
+ },
52
+ or: {
53
+ storage: :filesystem,
54
+ path: ':rails_root/public/attachments/:class/:attachment/:style/:filename'
55
+ }
56
+ }
57
+ ```
58
+
59
+ The configuration for each storage inherits whatever attributes are defined at the configuration top level config, and existing attributes may be overridden with storage specific values. For example:
60
+
61
+ ```ruby
62
+ has_attached_file :avatar, {
63
+ storage: :eitheror,
64
+ url: '/api/v1/attachments/:attachment/:id/:style',
65
+ path: ':rails_root/public/attachments/:class/:attachment/:style/:filename',
66
+ either: {
67
+ storage: :fog,
68
+ path: ':attachment/:id/:style/:filename',
69
+ url: ':attachment/:id/:style/:filename'
70
+ },
71
+ or: {
72
+ storage: :filesystem,
73
+ }
74
+ }
75
+ ```
76
+
77
+ In the example above, the storage **or** will inherit the attributes `path` and `url` from the base configuration, while `either` will provide its own `path` and `url` attributes. The following configuration is equivalent:
78
+
79
+ ```ruby
80
+ has_attached_file :avatar, {
81
+ storage: :eitheror,
82
+ either: {
83
+ storage: :fog,
84
+ path: ':attachment/:id/:style/:filename',
85
+ url: ':attachment/:id/:style/:filename'
86
+ },
87
+ or: {
88
+ storage: :filesystem,
89
+ url: '/api/v1/attachments/:attachment/:id/:style',
90
+ path: ':rails_root/public/attachments/:class/:attachment/:style/:filename',
91
+ }
92
+ }
93
+ ```
94
+
95
+ That is particularly useful when globally configuring `paparclip-eitheror`.
96
+
97
+ ## Global Configuration
98
+
99
+ On large codebases it might become very tedious and error prone to chase down all paperclip usages and adapt their configuration to use `paperclip-eitheror`. An alternative is to configure `paperclip` default options to use `paperclip-eitheror` as the default storage.
100
+
101
+ ```ruby
102
+ # config/initializers/paperclip.rb
103
+
104
+ Paperclip::Attachment.default_options[:storage] = :eitheror
105
+ Paperclip::Attachment.default_options[:either] = {
106
+ storage: :fog,
107
+ path: ':attachment/:id/:style/:filename',
108
+ url: ':attachment/:id/:style/:filename',
109
+ }
110
+
111
+ Paperclip::Attachment.default_options[:or] = {
112
+ storage: :filesystem,
113
+ }
114
+ ```
115
+
116
+ Since storages inherit configuration from the base config, you would not have to change any of your existing models and in this case the **or** storage would inherit the configuration on your models acting as your existing storage.
117
+
118
+ In case you need a particular model to **NOT** be affected by the global configuration, you can explicitly define the storage type you want the model to use as part of its `has_attached_file` options:
119
+
120
+ ```ruby
121
+ class User < ActiveRecord::Base
122
+ has_attached_file :avatar, storage: :filesystem
123
+ end
124
+ ```
125
+
126
+ # `:autosync`
127
+
128
+ You can configure `paperclip-eitheror` to automatically synchronize attachments from the **or** (fallback) storage to **either** (primary).
129
+
130
+ When `:autosync` is set to `true`, whenever an attachment which exists only on **or** is accessed, the attachment will be synced to the primary storage and access (such as `path` and `url`) will be provided through the primary storage.
131
+
132
+ ```ruby
133
+ has_attached_file :avatar, {
134
+ storage: :eitheror,
135
+ autosync: true,
136
+ ...
137
+ }
138
+ ```
139
+
140
+ # Method aliasing/overriding
141
+
142
+ Different storages provide different ways of accessing attachments.
143
+ For instance, when using `:fog` storage, you have access to methods which only make sense to that particular storage.
144
+ Or when you're migrating your assets from a `:filesystem` storage to a `:fog` storage, you might not want to go after every reference to `attachment.url` in your code in order to change it to `attachment.public_url`.
145
+
146
+ `paperclip-eitheror` gives you the ability to provide an `:alias` key, which then will allow you to tell it how to behave when such scenario happens.
147
+
148
+ For instance:
149
+
150
+ ```ruby
151
+ has_attached_file :avatar, {
152
+ storage: :eitheror,
153
+ either: {
154
+ storage: :fog,
155
+ alias: {
156
+ url: :public_url
157
+ }
158
+ },
159
+ or: {
160
+ storage: :filesystem,
161
+ }
162
+ }
163
+ ```
164
+
165
+ Int he example above we're telling `paperclip-eitheror` that whenever `avatar.url` is called, the `:fog` storage will delegate it to `attachment.public_url`. Mapping `fog.url` to `fog.public_url` seems like a very straight-forward mapping.
166
+
167
+ Not always mapping from one method to another is going to be that simple, you can write you own overrides by mapping a method to a `lambda`, like so:
168
+
169
+ ```ruby
170
+ has_attached_file :avatar, {
171
+ storage: :eitheror,
172
+ either: {
173
+ storage: :fog,
174
+ alias: {
175
+ url: ->(either_storage, or_storage, attachment, *args) do
176
+ puts "Look, I can do whatever I want"
177
+ end
178
+ }
179
+ },
180
+ or: {
181
+ storage: :filesystem,
182
+ }
183
+ }
184
+ ```
185
+
186
+ The `lambda` will receive as parameters both storages (**either** and **or**), the attachment instance and any other arguments given when the method was called.
187
+
188
+ ## Development
189
+
190
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
191
+
192
+ ## Contributing
193
+
194
+ Bug reports and pull requests are welcome on GitHub at https://github.com/powerhome/paperclip-eitheror. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
195
+
196
+
197
+ ## License
198
+
199
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ require "paperclip/eitheror/version"
2
+ require "paperclip/storage/eitheror"
@@ -0,0 +1,5 @@
1
+ module Paperclip
2
+ module Eitheror
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,95 @@
1
+ module Paperclip
2
+ module Storage
3
+ module Eitheror
4
+ def self.extended base
5
+ base.instance_eval do
6
+ @either = Attachment.new(base.name, base.instance, base.options.merge(base.options[:either]))
7
+ @or = Attachment.new(base.name, base.instance, base.options.merge(base.options[:or]))
8
+
9
+ define_aliases @either, base.options[:either].fetch(:alias, {})
10
+ define_aliases @or, base.options[:or].fetch(:alias, {})
11
+ end
12
+ end
13
+
14
+ def sync
15
+ @either.assign @or
16
+ @either.save
17
+ end
18
+
19
+ def synced?
20
+ @either.exists?
21
+ end
22
+
23
+ def syncable?
24
+ @or.exists?
25
+ end
26
+
27
+ def path(style_name = default_style)
28
+ usable_storage.path(style_name)
29
+ end
30
+
31
+ def url(style_name = default_style, options = {})
32
+ usable_storage.url(style_name, options)
33
+ end
34
+
35
+ def flush_writes
36
+ storage = usable_storage
37
+ storage.instance_variable_set(:@queued_for_write, @queued_for_write)
38
+ storage.flush_writes
39
+
40
+ @queued_for_write = {}
41
+ end
42
+
43
+ def flush_deletes
44
+ all_storages.each do |storage|
45
+ storage.instance_variable_set(:@queued_for_delete, @queued_for_delete)
46
+ storage.flush_deletes
47
+ end
48
+
49
+ @queued_for_delete = []
50
+ end
51
+
52
+ def queue_some_for_delete(*styles)
53
+ @queued_for_delete += styles.flatten.uniq.map do |style|
54
+ all_storages.map { |s| s.path(style) if s.exists?(style) }
55
+ end.flatten.compact
56
+ end
57
+
58
+ def queue_all_for_delete
59
+ queue_some_for_delete([:original, *styles.keys].uniq)
60
+ super
61
+ end
62
+
63
+ def method_missing method, *args
64
+ usable_storage.send(method, *args)
65
+ end
66
+
67
+ private
68
+ def all_storages
69
+ [@either, @or]
70
+ end
71
+
72
+ def usable_storage
73
+ return @either if !@or.exists? || @either.exists?
74
+ options[:autosync] && sync ? @either : @or
75
+ end
76
+
77
+ def define_aliases target, aliases = {}
78
+ aliases.each do |name, value|
79
+ block = is_callable?(value) ?
80
+ ->(*args) { value.call(@either, @or, self, *args) } :
81
+ ->(*args) { target.send(value, *args) }
82
+ create_method(target, name, &block)
83
+ end
84
+ end
85
+
86
+ def is_callable?(o)
87
+ o.respond_to?(:call)
88
+ end
89
+
90
+ def create_method(target, name, &block)
91
+ target.class.send(:define_method, name, &block)
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'paperclip/eitheror/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "paperclip-eitheror"
8
+ spec.version = Paperclip::Eitheror::VERSION
9
+ spec.authors = ["Matheus Abegg"]
10
+ spec.email = ["matheus.abegg@gmail.com"]
11
+
12
+ spec.summary = "Storage for Paperclip which will fallback in case of" \
13
+ " a file not existing on the primary storage."
14
+ spec.homepage = "https://github.com/powerhome/paperclip-eitheror"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "paperclip", "= 4.3.1"
25
+
26
+ spec.add_development_dependency "activerecord", "= 3.2.22.5"
27
+ spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "bundler", "~> 1.13"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "byebug"
32
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-eitheror
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matheus Abegg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: paperclip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 4.3.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 4.3.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.22.5
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.22.5
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
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
+ description:
112
+ email:
113
+ - matheus.abegg@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - lib/paperclip-eitheror.rb
127
+ - lib/paperclip/eitheror/version.rb
128
+ - lib/paperclip/storage/eitheror.rb
129
+ - paperclip-eitheror.gemspec
130
+ homepage: https://github.com/powerhome/paperclip-eitheror
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.4.5.1
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Storage for Paperclip which will fallback in case of a file not existing
154
+ on the primary storage.
155
+ test_files: []