simple_active_storage 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: b52e3afcde6f569b71666a954f8019b15189e6a6
4
+ data.tar.gz: eff78df7e7e8d8f44748537934c28dfaad663bbb
5
+ SHA512:
6
+ metadata.gz: 95612549f883e1226ec2913fb8122f0412d25d88ab5274f61b20269e4d30d6cc056a29b9fc6e0da3ad03df9fba5e49666d131df564c1f17577b0031dc292e801
7
+ data.tar.gz: b410ab56952be48e9345d242a6a64331aff3e2d03b761e99f0f3f53955086b39f830202e8f2b9f29b73a2b8d17c181b23cfc674c0b8570a406a9e8494529518b
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 aotianlong
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # SimpleActiveStorage
2
+ 发现ActiveStorage在模型里面获取url非常不方便,想写一个容易的
3
+ 比如
4
+ ```ruby
5
+ user.avatar.variant("100x100")
6
+ ```
7
+ 每次都要写100x100多不方便
8
+ 我们是不是可以这样写
9
+ ```ruby
10
+ user.avatar.variant(:thumb)
11
+ ```
12
+ 这样是不是更简单一些
13
+ url也很不方便,每次都需要url_for才能获取到
14
+ 我们是不是能
15
+ ```ruby
16
+ user.avatar.path
17
+ user.avatar.url
18
+ ```
19
+ 这样更简单一点呢
20
+
21
+ ## Usage
22
+ config/initializers/simple_active_storage.rb
23
+ ```ruby
24
+ SimpleActiveStorage.default_url_options = {host: "www.example.com"}
25
+ SimpleActiveStorage.transformation :thumb do
26
+ {resize: "100x100"}
27
+ end
28
+ SimpleActiveStorage.transformation :tiny do
29
+ {resize: "20x20"}
30
+ end
31
+ SimpleActiveStorage.transformation :medium do
32
+ {resize: "400x400"}
33
+ end
34
+ ```
35
+
36
+ app/models/user.rb
37
+ ```ruby
38
+ class User < ApplicationRecord
39
+ has_one_attached :avatar
40
+ has_many_attached :photos
41
+ end
42
+
43
+ user = User.first
44
+ user.avatar.attach io: File.new(Rails.root.join("test","fixtures","files","test.png")),filename: "test.png"
45
+ user.photos.attach io: File.new(Rails.root.join("test","fixtures","files","test.png")),filename: "test.png"
46
+ user.avatar.path # => "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--08f03241905408289698b118698ca7642c3e691e/test.png"
47
+ user.avatar.url # => "http://www.example.com/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--08f03241905408289698b118698ca7642c3e691e/test.png"
48
+ user.avatar.variant(:thumb).path # => "http://www.example.com/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--08f03241905408289698b118698ca7642c3e691e/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9VWTI5dFltbHVaVjl2Y0hScGIyNXpld1k2QzNKbGMybDZaVWtpRERFd01IZ3hNREFHT2daRlZBPT0iLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--20ae94033d7a10dcb862bd24c1dcbb3740e61e7c/test.png"
49
+ user.avatar.variant(:thumb).url # => "http://www.example.com/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--08f03241905408289698b118698ca7642c3e691e/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9VWTI5dFltbHVaVjl2Y0hScGIyNXpld1k2QzNKbGMybDZaVWtpRERFd01IZ3hNREFHT2daRlZBPT0iLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--20ae94033d7a10dcb862bd24c1dcbb3740e61e7c/test.png"
50
+ photo = user.photos[0]
51
+ photo.variant(:thumb).url # =>"http://www.example.com/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e81a87b1d919f371e6b665e2c01301cff6d16a26/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9VWTI5dFltbHVaVjl2Y0hScGIyNXpld1k2QzNKbGMybDZaVWtpRERFd01IZ3hNREFHT2daRlZBPT0iLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--20ae94033d7a10dcb862bd24c1dcbb3740e61e7c/test.png"
52
+ photo.url # => "http://www.example.com/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e81a87b1d919f371e6b665e2c01301cff6d16a26/test.png"
53
+ photo.blob.url # => "http://www.example.com/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e81a87b1d919f371e6b665e2c01301cff6d16a26/test.png"
54
+ ```
55
+
56
+ ## Installation
57
+ Add this line to your application's Gemfile:
58
+
59
+ ```ruby
60
+ gem 'simple_active_storage'
61
+ ```
62
+
63
+ And then execute:
64
+ ```bash
65
+ $ bundle
66
+ ```
67
+
68
+ Or install it yourself as:
69
+ ```bash
70
+ $ gem install simple_active_storage
71
+ ```
72
+
73
+ ## Contributing
74
+ visit http://www.github.com/aotianlong/simple_active_storage
75
+ pull requests are welcome.
76
+
77
+ ## License
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SimpleActiveStorage'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate simple_active_storage
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,7 @@
1
+ class SimpleActiveStorageGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ def copy_initializer_file
5
+ copy_file "simple_active_storage.rb","config/initializers/simple_active_storage.rb"
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ # SimpleActiveStorage.default_url_options = {host: "www.example.com"}
2
+ #
3
+ # SimpleActiveStorage.transformation :thumb do
4
+ # {
5
+ # combine_options: {
6
+ # resize: "100x100"
7
+ # }
8
+ # }
9
+ # end
10
+ # SimpleActiveStorage.transformation :tiny do
11
+ # {
12
+ # combine_options: {
13
+ # resize: "20x20"
14
+ # }
15
+ # }
16
+ # end
17
+ #
18
+ # SimpleActiveStorage.transformation :medium do
19
+ # {
20
+ # combine_options: {
21
+ # resize: "400x400"
22
+ # }
23
+ # }
24
+ # end
@@ -0,0 +1,15 @@
1
+ module SimpleActiveStorage
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "simple_active_storage" do
4
+ ActiveStorage::Variation.send :prepend,SimpleActiveStorage::Variation
5
+ ActiveStorage::Attached::One.send :include,SimpleActiveStorage::Url
6
+ ActiveStorage::Attachment.send :include,SimpleActiveStorage::Url
7
+ ActiveStorage::Variant.send :include,SimpleActiveStorage::Url
8
+ ActiveStorage::Preview.send :include,SimpleActiveStorage::Url
9
+ config.after_initialize do
10
+ ActiveStorage::Blob.send :include,SimpleActiveStorage::Url
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ module SimpleActiveStorage
2
+ mattr_accessor :default_url_options,default: {}
3
+ def self.url_helpers
4
+ Rails.application.routes.url_helpers
5
+ end
6
+
7
+ module Url
8
+ extend ActiveSupport::Concern
9
+ class UnknowObjectError < Exception; end
10
+ def url(key = nil,options = {})
11
+ options.reverse_merge! only_path: false
12
+ options.reverse_merge! SimpleActiveStorage.default_url_options
13
+ helpers = SimpleActiveStorage.url_helpers
14
+ case self.class.name
15
+ when "ActiveStorage::Blob"
16
+ helpers.rails_blob_url(self,options)
17
+ when "ActiveStorage::Attachment","ActiveStorage::Attached::One"
18
+ helpers.rails_blob_url(self.blob,options)
19
+ when "ActiveStorage::Variant","ActiveStorage::Preview"
20
+ helpers.rails_representation_url(self,options)
21
+ else
22
+ raise UnknowObjectError.new("unknow object #{self}")
23
+ end
24
+ end
25
+
26
+ def path(key = nil)
27
+ url(key,only_path: true)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ module SimpleActiveStorage
2
+ class TransformationNotFound < Exception; end
3
+
4
+ mattr_accessor :transformations
5
+ def self.transformation(name,transformation = nil,&block)
6
+ self.transformations ||= {}
7
+ if block_given?
8
+ transformation = yield
9
+ end
10
+ transformations[name] = transformation
11
+ end
12
+
13
+ module Variation
14
+ extend ActiveSupport::Concern
15
+ def initialize(transformation)
16
+ if transformation.is_a? Symbol
17
+ transformation = SimpleActiveStorage.transformations[transformation]
18
+ if transformation.nil?
19
+ raise TransformationNotFound.new("transformation: #{transformation} not found, perhaps you forget define it?")
20
+ end
21
+ end
22
+ super(transformation)
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleActiveStorage
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,7 @@
1
+ require "simple_active_storage/railtie"
2
+ require "simple_active_storage/variation"
3
+ require "simple_active_storage/url"
4
+
5
+ module SimpleActiveStorage
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :simple_active_storage do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_active_storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aotianlong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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
+ description: improve active storage
42
+ email:
43
+ - aotianlong@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/generators/simple_active_storage/USAGE
52
+ - lib/generators/simple_active_storage/simple_active_storage_generator.rb
53
+ - lib/generators/simple_active_storage/templates/simple_active_storage.rb
54
+ - lib/simple_active_storage.rb
55
+ - lib/simple_active_storage/railtie.rb
56
+ - lib/simple_active_storage/url.rb
57
+ - lib/simple_active_storage/variation.rb
58
+ - lib/simple_active_storage/version.rb
59
+ - lib/tasks/simple_active_storage_tasks.rake
60
+ homepage: http://www.github.com/aotianlong
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.6.14
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: add shortcut variant support for active storage
84
+ test_files: []