named_variant 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
+ SHA256:
3
+ metadata.gz: aae2ca4f1de62ee57337c10488954ecc89da34a24db1fc3e80da9be699bed3ed
4
+ data.tar.gz: 1a8c5519cff74ee34d3c780feb2b42263c050431980712f699b40a70eb6877c2
5
+ SHA512:
6
+ metadata.gz: bb3406e05f711c80fcbaa6edc0a136f3a8da2d5d821def3c2a401d7ff16ab8b9119796df299a9e7c27b3c8a1705b6b6fbe0b8da2571d196acc3a5a8c5914583d
7
+ data.tar.gz: cd077ff4bc3ea76fb4d91c6c1ee34b377b1d0d5c6c7ab416d5d56680261cef29fead4681c657640483bfa8ef14be75215a12eb352b2db2db16f33036494aadda
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Yukito Ito
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,51 @@
1
+ # NamedVariant
2
+
3
+ Make ActiveStorage's variant named and configurable.
4
+
5
+ Usual process is
6
+
7
+ ```ruby
8
+ class User < ActiveRecord::Base
9
+ has_one_attached :avatar
10
+ end
11
+ ```
12
+
13
+ and view.
14
+
15
+ ```ruby
16
+ = image_tag(user.avatar.variant(resize: "100x100", monochrome: true, flip: "-90").processed)
17
+ ```
18
+
19
+ It's annoying, and sometimes not DRY. NamedVarient gives a solution.
20
+
21
+ ```ruby
22
+ class User < ActiveRecord::Base
23
+ has_one_attached :avatar
24
+
25
+ variant_name :monochrome, resize: "100x100", monochrome: true, flip: "-90"
26
+ end
27
+ ```
28
+
29
+ ```ruby
30
+ = image_tag(user.avatar.variant(:monochrome).processed)
31
+ # only user's attachment can call `monochrome` variant.
32
+ ```
33
+
34
+ ## Installation
35
+
36
+ ```ruby
37
+ gem 'named_variant'
38
+ ```
39
+
40
+ And then execute:
41
+ ```bash
42
+ $ bundle
43
+ ```
44
+
45
+ ## Todo
46
+
47
+ - [ ] scoped variant name support
48
+ - [ ] config file support
49
+
50
+ ## License
51
+ 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,14 @@
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 "rspec/core/rake_task"
8
+ Bundler::GemHelper.install_tasks
9
+
10
+ RSpec::Core::RakeTask.new do |t|
11
+ t.rspec_opts = %w[--format documentation --colour]
12
+ end
13
+
14
+ task :default => "spec"
@@ -0,0 +1,39 @@
1
+ require "named_variant/railtie"
2
+
3
+ module NamedVariant
4
+ mattr_reader :named_variants, default: {}
5
+
6
+ module VariantExtension
7
+ # prepend ActiveStorage::Blob::Representable#variant
8
+ # https://github.com/rails/rails/blob/3c823271af52a61e825123def170fe2187057577/activestorage/app/models/active_storage/blob/representable.rb#L28
9
+ def variant(args)
10
+ if args.is_a?(Symbol)
11
+ # self is ActiveStorage::Attached::One
12
+ named_variant = ::NamedVariant.find_named_variant_for(klass: self.record.class, sym: args) || raise(VariantNotFound)
13
+ return super(named_variant.to_h)
14
+ end
15
+
16
+ super
17
+ end
18
+ end
19
+
20
+ module ActiveRecordExtension
21
+ def variant_name(name, **opts)
22
+ ::NamedVariant.add_variant("#{self}/#{name}", opts) ## key looks like: User/xsmall
23
+ end
24
+ end
25
+
26
+ def self.find_named_variant_for(klass: nil, sym:)
27
+ # NOTE: name params
28
+ named_variants["#{klass}/#{sym}"] || named_variants[sym.to_s]
29
+ end
30
+
31
+ def self.add_variant(name, opts)
32
+ named_variants[name.to_s] = Variant.new(opts)
33
+ end
34
+
35
+ class VariantNotFound < StandardError
36
+ end
37
+ end
38
+
39
+ require "named_variant/variant"
@@ -0,0 +1,11 @@
1
+ module NamedVariant
2
+ class Railtie < ::Rails::Railtie
3
+ ActiveSupport.on_load(:active_record) do
4
+ ActiveRecord::Base.extend ::NamedVariant::ActiveRecordExtension
5
+ end
6
+
7
+ config.after_initialize do
8
+ ActiveStorage::Attached::One.prepend ::NamedVariant::VariantExtension
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module NamedVariant
2
+ class Variant
3
+ attr_reader :options
4
+
5
+ def initialize(options)
6
+ @options = options
7
+ end
8
+
9
+ def to_h
10
+ options
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module NamedVariant
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :named_variant do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: named_variant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yukito Ito
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-02 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'
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mini_magick
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: onkcop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.53'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.53'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
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: sqlite3
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
+ description: Make ActiveStorage's variant named and configurable
84
+ email:
85
+ - yukibukiyou@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/named_variant.rb
94
+ - lib/named_variant/railtie.rb
95
+ - lib/named_variant/variant.rb
96
+ - lib/named_variant/version.rb
97
+ - lib/tasks/named_variant_tasks.rake
98
+ homepage: https://github.com/ykpythemind/named_variant
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubygems_version: 3.0.3
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Named ActiveStorage's variant
121
+ test_files: []