acts_as_hoc_avatarable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82e80387e0a0578de143e77a4a28b49f6aa4ac7b
4
+ data.tar.gz: 361ae67b7f806a962ef95e6b50874029b5abbc04
5
+ SHA512:
6
+ metadata.gz: 163752b3f11ac8896b803df2dde18ef23628054b28fde7645bca100b385d37de915c1007ba15d0bbcba6edf6c8c2cb82026c82fc2a35272571363d8083a5d86e
7
+ data.tar.gz: 9895f1856de8e188647b7c793b7fc9f8922b5374577ae73e52732b351ab89238066598177dc2278908819ab8b167239c0f540f5bd867bc4f961b8f545102d550
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in acts_as_hoc_avatarable.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Gert Lavsen
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,30 @@
1
+ # ActsAsHocAvatarable
2
+
3
+ Ease the use of active storage as avatar
4
+ This uses ActiveStorage, thus minimum rails version is 5.2
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'acts_as_hoc_avatarable'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install acts_as_hoc_avatarable
21
+
22
+ ## Usage
23
+
24
+ install it with:
25
+
26
+ $ rails generate acts_as_hoc_avatarable:install
27
+
28
+ ## License
29
+
30
+ 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,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "acts_as_hoc_avatarable/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts_as_hoc_avatarable"
8
+ spec.version = ActsAsHocAvatarable::VERSION
9
+ spec.authors = ["Gert Lavsen"]
10
+ spec.email = ["gert@houseofcode.io"]
11
+
12
+ spec.summary = "Easy installation and helpers for using active storage for an avatar"
13
+ spec.description = "Easy installation and helpers for using active storage for an avatar"
14
+ spec.homepage = "https://github.com/house-of-code/acts_as_hoc_avatarable"
15
+ spec.license = "MIT"
16
+
17
+ # # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+ spec.add_dependency "mini_magick"
33
+ spec.add_development_dependency "bundler", "~> 1.16.a"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acts_as_hoc_avatarable"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,52 @@
1
+ module ActsAsHocAvatarable
2
+ extend ActiveSupport::Concern
3
+ included do
4
+ end
5
+
6
+ module ClassMethods
7
+ def acts_as_hoc_avatarable(_options = {})
8
+ has_one_attached :avatar
9
+ attr_accessor :avatar_contents
10
+ attr_accessor :avatar_name
11
+ attr_accessor :remove_avatar
12
+ before_save :clear_avatar
13
+ after_save :parse_avatar
14
+ end
15
+ end
16
+
17
+ def avatar_url
18
+ return Rails.application.routes.url_helpers.rails_blob_url(avatar, host: (ActsAsHocAvatarable.configuration.default_host ||= "localhost:3000")) if avatar.attached?
19
+ return "https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}" if (ActsAsHocAvatarable.configuration.fallback_to_gravatar ||= true)
20
+ return nil
21
+ end
22
+
23
+ def clear_avatar
24
+ if remove_avatar == '1'
25
+ avatar_contents = nil
26
+ avatar.purge_later
27
+ end
28
+ end
29
+
30
+ def parse_avatar
31
+ # If directly uploaded
32
+ unless avatar_contents.nil? || avatar_contents[/(image\/[a-z]{3,4})|(application\/[a-z]{3,4})/] == ''
33
+ content_type = avatar_contents[/(image\/[a-z]{3,4})|(application\/[a-z]{3,4})/]
34
+ content_type = content_type[/\b(?!.*\/).*/]
35
+ contents = avatar_contents.sub /data:((image|application)\/.{3,}),/, ''
36
+ decoded_data = Base64.decode64(contents)
37
+ mini_magick = MiniMagick::Image.read(decoded_data)
38
+ if (ActsAsHocAvatarable.configuration.resize_on_create ||= true)
39
+ mini_magick.resize (ActsAsHocAvatarable.configuration.resize_size ||= "100x100>")
40
+ end
41
+ decoded_data = mini_magick.to_blob
42
+ filename = avatar_name || "avatar_#{Time.zone.now.to_i}.#{content_type}"
43
+ File.open("#{Rails.root}/tmp/storage/#{filename}", 'wb') do |f|
44
+ f.write(decoded_data)
45
+ end
46
+ avatar.attach(io: File.open("#{Rails.root}/tmp/storage/#{filename}"), filename: filename)
47
+ FileUtils.rm("#{Rails.root}/tmp/images/#{filename}")
48
+ end
49
+ end
50
+
51
+ end
52
+ ActiveRecord::Base.send :include, ActsAsHocAvatarable
@@ -0,0 +1,10 @@
1
+ module ActsAsHocAvatarable
2
+ class Configuration
3
+ attr_accessor :resize_on_create
4
+ attr_accessor :resize_size
5
+ attr_accessor :fallback_to_gravatar
6
+ attr_accessor :default_host
7
+ def initialize
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsHocAvatarable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'acts_as_hoc_avatarable/version'
2
+ require 'acts_as_hoc_avatarable/configuration'
3
+ require 'acts_as_hoc_avatarable/acts_as_hoc_avatarable'
4
+ module ActsAsHocAvatarable
5
+ LOCK = Mutex.new
6
+ class << self
7
+ def configure(config_hash=nil)
8
+ if config_hash
9
+ config_hash.each do |k,v|
10
+ configuration.send("#{k}=", v) rescue nil if configuration.respond_to?("#{k}=")
11
+ end
12
+ end
13
+
14
+ yield(configuration) if block_given?
15
+ end
16
+
17
+ def configuration
18
+ @configuration = nil unless defined?(@configuration)
19
+ @configuration || LOCK.synchronize { @configuration ||= ActsAsHocAvatarable::Configuration.new }
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ ActsAsHocAvatarable.configure do |config|
2
+ config.default_host = "localhost:3000"
3
+ config.resize_on_create = true
4
+ config.resize_size = "100x100>"
5
+ config.fallback_to_gravatar = true
6
+ end
@@ -0,0 +1,15 @@
1
+ module ActsAsHocAvatarable
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def copy_initializer_file
7
+ template "initializer.rb", "config/initializers/acts_as_hoc_avatarable.rb"
8
+ end
9
+
10
+ def install_active_storage
11
+ rails_command 'activestorage::install'
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_hoc_avatarable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gert Lavsen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mini_magick
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.16.a
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.16.a
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Easy installation and helpers for using active storage for an avatar
56
+ email:
57
+ - gert@houseofcode.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - acts_as_hoc_avatarable.gemspec
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/acts_as_hoc_avatarable.rb
71
+ - lib/acts_as_hoc_avatarable/acts_as_avatarable.rb
72
+ - lib/acts_as_hoc_avatarable/configuration.rb
73
+ - lib/acts_as_hoc_avatarable/version.rb
74
+ - lib/generators/acts_as_hoc_avatarable/templates/initializer.rb
75
+ - lib/generators/install_generator.rb
76
+ homepage: https://github.com/house-of-code/acts_as_hoc_avatarable
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.13
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Easy installation and helpers for using active storage for an avatar
100
+ test_files: []