avatari 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58d6762f5d16be413ff91cf6cef2aed554b7e61d1123c3c0e916ec2b8c658b37
4
- data.tar.gz: e51d50a4bd162966c840d6adf724b87957a8516751da32766936bb8d43e52646
3
+ metadata.gz: 60d011079934dc01108eefb33a033e31c01153c97415b9c229cb3998bb72de2f
4
+ data.tar.gz: 8949fd84c3d2408942560fa9b109e1b99b5a1a238f748a459ff6970c413f10d9
5
5
  SHA512:
6
- metadata.gz: 74b7f808676b6115e4f25c94a247b4e98551f8a2191e02a3c5733a9bd26dd57f1f8a14d5673992325aa0612b2b5b96c72fb53230c37352d31c0396534fa9b583
7
- data.tar.gz: d3050c08d7f3faf5ccba1adc8a823d9d7e7c7ddbe798985db0dab8acfe1830c9df3a6f8e6133001b8971187bc1878ab3a12be06a4bfe997b74c2f325aba9a2ec
6
+ metadata.gz: 60ca63d49fd416274b0ed2f4e1233211bab53a88df33138f3f475b443c6b88b3d874edaa6e640d33c81851c161d8129793e9a8111be62ca0d5d8e8162c588d77
7
+ data.tar.gz: 14509ef9c79be2c99ed575f590098330745337df56b57d4bd1b7bb237f1bb83d0c20c27866169f69ca141f82db2256d5b1ffbed569d197ddfd43bb594df6aa71
data/CHANGELOG.md CHANGED
@@ -4,6 +4,19 @@
4
4
 
5
5
  * nothing yet
6
6
 
7
- ### 1.0.0 - 2018/01/06
7
+ ### 1.1.0 - 2018/01/08
8
+
9
+ * features
10
+ * add generator with initializer to allow configuration of Avatari
11
+ * introduce `Avatari::Avatar` instances associated with ActiveRecord classes
12
+ * enhancements
13
+ * automatically fetch first letters from passed initials accessor
14
+ * allow to override the default version for a specific class
15
+ * customize image formats and preprocessed versions
16
+ * unrequire Fog & MiniMagick
17
+ * bugfixes
18
+ * fixed dependencies
19
+
20
+ ### 1.0.0 - 2018/01/07
8
21
 
9
22
  * initial release
data/README.md CHANGED
@@ -14,6 +14,7 @@ Avatari extends [CarrierWave](https://github.com/carrierwaveuploader/carrierwave
14
14
  * [CarrierWave](#carrierwave)
15
15
  * [Models](#models)
16
16
  * [Views](#views)
17
+ * [Configuration](#configuration)
17
18
  * [To Do](#to-do)
18
19
  * [Contributing](#contributing)
19
20
  * [Contributors](#contributors)
@@ -44,6 +45,10 @@ If you always want to be up to date fetch the latest from GitHub in your `Gemfil
44
45
  gem 'avatari', github: 'jonhue/avatari'
45
46
  ```
46
47
 
48
+ Now run the generator:
49
+
50
+ $ rails g avatari
51
+
47
52
  ## Usage
48
53
 
49
54
  ### Assets
@@ -60,7 +65,7 @@ To render Avatari avatars, you have to require the necessary assets to be added
60
65
  */
61
66
  ```
62
67
 
63
- **Note:** You can override the [styles](vendor/assets/stylesheets/avitari.sass) when you are looking for a custom integration.
68
+ **Note:** You can override the [styles](vendor/assets/stylesheets/avatari.sass) when you are looking for a custom integration.
64
69
 
65
70
  ### CarrierWave
66
71
 
@@ -68,9 +73,9 @@ To backup CarrierWave add an `avatar` column (`string`) to every ActiveRecord mo
68
73
 
69
74
  The `avatar` attribute can be used like any other CarrierWave instance.
70
75
 
71
- Avatari makes some assumptions about your CarrierWave integration. For Avatari to work properly, [Fog](https://github.com/carrierwaveuploader/carrierwave#fog) and [MiniMagick](https://github.com/minimagick/minimagick) have to be set up.
76
+ Avatari makes some assumptions about your CarrierWave integration. For Avatari to work properly after installation, [Fog](https://github.com/carrierwaveuploader/carrierwave#fog) and [MiniMagick](https://github.com/minimagick/minimagick) have to be set up. However they can be unrequired when [configuring](#configuration) Avatari.
72
77
 
73
- Avatari preprocesses uploaded avatars:
78
+ Avatari preprocesses uploaded avatars by default:
74
79
 
75
80
  * `raw` (800x800)
76
81
  * `big` (350x350)
@@ -79,8 +84,6 @@ Avatari preprocesses uploaded avatars:
79
84
  * `tiny` (50x50)
80
85
  * `mini` (40x40)
81
86
 
82
- It accepts images in `jpg`, `jpeg` and `png` format.
83
-
84
87
  ### Models
85
88
 
86
89
  You can add Avatari to an ActiveRecord model:
@@ -98,11 +101,22 @@ class User < ApplicationRecord
98
101
  avatari :initials
99
102
 
100
103
  def initials
101
- self.first_name[0] + self.last_name[0]
104
+ [self.first_name, self.last_name].join(' ')
102
105
  end
103
106
  end
104
107
  ```
105
108
 
109
+ By default Avatari manipulates the passed initials: only the first characters of the first two word will be used as initials:
110
+
111
+ ```
112
+ 'Jonas Hübotter'
113
+ => 'JH'
114
+ 'jonas hübotter'
115
+ => 'JH'
116
+ 'jonas hübotter and someone else'
117
+ => 'JH'
118
+ ```
119
+
106
120
  **Note:** When [rendering](#views) an avatar and you did not provide the `initials` option + no avatar has been uploaded yet, the helper method will return `false`.
107
121
 
108
122
  To add custom background colors to your placeholder avatars, pass the `colors` option:
@@ -117,6 +131,21 @@ For every instance of the class, Avatari will pick a sample of the array of prov
117
131
 
118
132
  To attach a picked color to a given record permanently, you will also have to add an `avatar_color` string column to your database tables.
119
133
 
134
+ There is a variety of other options you can pass to further configure Avatari. Here are the defaults:
135
+
136
+ ```ruby
137
+ class User < ApplicationRecord
138
+ avatari colors: ['#000000'], versions: {
139
+ mini: [40,40],
140
+ tiny: [50,50],
141
+ small: [75,75],
142
+ medium: [100,100],
143
+ big: [350,350],
144
+ raw: [800,800]
145
+ }, default_version: nil, process: [800,800], extension_whitelist: ['jpg','jpeg','png']
146
+ end
147
+ ```
148
+
120
149
  ### Views
121
150
 
122
151
  To render an avatar in your view, utilize the `avatari` helper method:
@@ -131,7 +160,23 @@ You can also specify a size:
131
160
  = avatari current_user, :tiny
132
161
  ```
133
162
 
134
- The `size` of the rendered avatar defaults to `:medium`.
163
+ ---
164
+
165
+ ## Configuration
166
+
167
+ You can configure Avatari by passing a block to `configure`. This can be done in `config/initializers/avatari.rb`:
168
+
169
+ ```ruby
170
+ Avatari.configure do |config|
171
+ config.default_version = :medium
172
+ end
173
+ ```
174
+
175
+ **`default_version`** Default version to be used when rendering avatars. Takes a symbol. Defaults to `:medium`.
176
+
177
+ **`mini_magick`** Whether or not to include MiniMagick. Required for processing file versions. Takes a boolean. Defaults to `true`.
178
+
179
+ **`storage`** Choose you default storage option for CarrierWave. Accepts `:file` or `:fog`. Defaults to `:fog`.
135
180
 
136
181
  ---
137
182
 
@@ -1,11 +1,14 @@
1
1
  module Avatari
2
2
  module AvatarHelper
3
3
 
4
- def avatari object, version = :medium
4
+ def avatari object, version = nil
5
+ version ||= object.class.avatari_instance.default_version || ::Avatari.configuration.default_version
6
+ version = version.to_s
7
+
5
8
  if object.avatar?
6
- image_tag object.avatar.url, class: 'avatari avatari--' + version.to_s
7
- elsif !object.class.avatari_initials.nil?
8
- render partial: 'avatari/avatar', locals: { object: object, version: version.to_s }
9
+ image_tag object.avatar.url, class: 'avatari avatari--' + version
10
+ elsif !object.class.avatari_instance.initials(object).nil?
11
+ render partial: 'avatari/avatar', locals: { object: object, version: version }
9
12
  else
10
13
  false
11
14
  end
@@ -5,31 +5,20 @@ module Avatari
5
5
 
6
6
  def avatari_color
7
7
  if defined?(self.avatar_color)
8
- self.update_attributes(avatar_color: self.class.avatari_colors.sample) if self.avatar_color.nil?
8
+ self.update_attributes(avatar_color: self.class.avatari_instance.colors.sample) if self.avatar_color.nil?
9
9
  self.avatar_color
10
10
  else
11
- self.class.avatari_colors.sample
11
+ self.class.avatari_instance.colors.sample
12
12
  end
13
13
  end
14
14
 
15
15
  module ClassMethods
16
+ attr_accessor :avatari_instance
16
17
 
17
- attr_accessor :avatari_colors
18
- attr_accessor :avatari_initials
19
-
20
- def avatari initials = nil, options = {}
21
- defaults = {
22
- colors: nil
23
- }
24
- defaults.merge! options
25
-
26
- self.avatari_colors = options[:colors] || ['#000000']
27
- self.avatari_initials = initials
28
-
29
- serialize :avatar_color, Array
18
+ def avatari initials_method = nil, options = {}
19
+ self.avatari_instance = ::Avatari::Avatar.new initials_method, options
30
20
  mount_uploader :avatar, ::Avatari::AvatarUploader
31
21
  end
32
-
33
22
  end
34
23
 
35
24
  end
@@ -1,48 +1,29 @@
1
1
  class Avatari::AvatarUploader < CarrierWave::Uploader::Base
2
2
 
3
- # Include MiniMagick support:
4
- include CarrierWave::MiniMagick
3
+ include CarrierWave::MiniMagick if ::Avatari.configuration&.mini_magick
5
4
 
6
- # Choose AWS as file storage:
7
- storage :fog
5
+ storage ::Avatari.configuration&.storage || :fog
8
6
 
9
- # Override the directory where uploaded files will be stored.
10
- # This is a sensible default for uploaders that are meant to be mounted:
11
7
  def store_dir
12
8
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
13
9
  end
14
-
15
- # Process files as they are uploaded:
16
- process scale: [800, 800]
17
-
18
- # Create different versions of your uploaded files:
19
- version :raw do
20
- process resize_to_fill: [800, 800]
21
- end
22
-
23
- version :big, from_version: :raw do
24
- process resize_to_fill: [350, 350]
25
- end
26
-
27
- version :medium, from_version: :big do
28
- process resize_to_fill: [100, 100]
29
- end
30
-
31
- version :small, from_version: :medium do
32
- process resize_to_fill: [75, 75]
10
+ def extension_whitelist
11
+ model.class.avatari_instance.extension_whitelist
33
12
  end
34
13
 
35
- version :tiny, from_version: :small do
36
- process resize_to_fill: [50, 50]
37
- end
14
+ process :avatari_processing
15
+ process :avatari_versioning
38
16
 
39
- version :mini, from_version: :tiny do
40
- process resize_to_fill: [40, 40]
17
+ def avatari_processing
18
+ process(scale: model.class.avatari_instance.process) unless model.class.avatari_instance.process.nil?
41
19
  end
42
-
43
- # White list of extensions which are allowed to be uploaded:
44
- def extension_whitelist
45
- %w(jpg jpeg png)
20
+ def avatari_versioning
21
+ return if model.class.avatari_instance.versions.nil?
22
+ model.class.avatari_instance.versions.each do |name, size|
23
+ version name.to_sym do
24
+ process resize_to_fill: size.to_a
25
+ end
26
+ end
46
27
  end
47
28
 
48
29
  end
@@ -1,3 +1,3 @@
1
- <div class="<%= 'avatari avatari--' + version %>" style="background: <%= object.avatari_color %>;">
2
- <span class="initials"><%= object.send(object.class.avatari_initials) %></span>
1
+ <div class="<%= 'avatari avatari--' + version %>" style="width: <%= object.class.avatari_instance.versions[version.to_sym].first / 16 %>rem; height: <%= object.class.avatari_instance.versions[version.to_sym].last / 16 %>rem; background: <%= object.avatari_color %>;">
2
+ <span class="initials" style="font-size: <%= object.class.avatari_instance.versions[version.to_sym].first / 50 %>rem;"><%= object.class.avatari_instance.initials(object) %></span>
3
3
  </div>
@@ -0,0 +1,43 @@
1
+ module Avatari
2
+ class Avatar
3
+
4
+ attr_accessor :initials_method
5
+ attr_accessor :colors
6
+ attr_accessor :versions
7
+ attr_accessor :default_version
8
+ attr_accessor :process
9
+ attr_accessor :extension_whitelist
10
+
11
+ def initialize initials_method = nil, options = {}
12
+ defaults = {
13
+ colors: ['#000000'],
14
+ versions: {
15
+ mini: [40,40],
16
+ tiny: [50,50],
17
+ small: [75,75],
18
+ medium: [100,100],
19
+ big: [350,350],
20
+ raw: [800,800]
21
+ },
22
+ default_version: nil,
23
+ process: [800,800],
24
+ extension_whitelist: ['jpg','jpeg','png']
25
+ }
26
+ options = defaults.merge! options
27
+
28
+ @initials_method = initials_method
29
+ @colors = options[:colors]
30
+ @versions = options[:versions]
31
+ @default_version = options[:default_version]
32
+ @process = options[:process]
33
+ @extension_whitelist = options[:extension_whitelist]
34
+ end
35
+
36
+ def initials object
37
+ raw = object.send(self.initials_method)
38
+ r = ''; raw.nil? ? nil : raw.split(' ').first(2).each { |n| r += n[0].upcase }
39
+ return r
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,26 @@
1
+ module Avatari
2
+
3
+ class << self
4
+ attr_accessor :configuration
5
+ end
6
+
7
+ def self.configure
8
+ self.configuration ||= Configuration.new
9
+ yield configuration
10
+ end
11
+
12
+ class Configuration
13
+
14
+ attr_accessor :default_version
15
+ attr_accessor :mini_magick
16
+ attr_accessor :storage
17
+
18
+ def initialize
19
+ @default_version = :medium
20
+ @mini_magick = true
21
+ @storage = :fog
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -1,5 +1,5 @@
1
1
  module Avatari
2
2
 
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
 
5
5
  end
data/lib/avatari.rb CHANGED
@@ -2,6 +2,10 @@ require 'avatari/version'
2
2
 
3
3
  module Avatari
4
4
 
5
+ require 'avatari/configuration'
6
+
7
+ autoload :Avatar, 'avatari/avatar'
8
+
5
9
  require 'avatari/engine'
6
10
  require 'avatari/railtie'
7
11
 
@@ -0,0 +1,15 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class AvatariGenerator < Rails::Generators::Base
5
+
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.join File.dirname(__FILE__), 'templates'
9
+ desc 'Install avatari'
10
+
11
+ def create_initializer
12
+ template 'initializer.rb', 'config/initializers/avatari.rb'
13
+ end
14
+
15
+ end
@@ -0,0 +1,12 @@
1
+ Avatari.configure do |config|
2
+
3
+ # Default version to be used when rendering avatars. Takes a symbol.
4
+ # config.default_version = :medium
5
+
6
+ # Whether or not to include MiniMagick. Required for processing file versions. Takes a boolean.
7
+ # config.mini_magick = true
8
+
9
+ # Choose you default storage option for CarrierWave. Accepts `:file` or `:fog`.
10
+ # config.storage = :fog
11
+
12
+ end
@@ -1 +1 @@
1
- .avatari--mini{width:px(40);height:px(40);display:flex;justify-content:center;align-items:center;border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%}.avatari--mini>.initials{font-size:px(16);line-height:1;color:white;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";font-weight:600}.avatari--tiny{width:px(50);height:px(50);display:flex;justify-content:center;align-items:center;border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%}.avatari--tiny>.initials{font-size:px(16);line-height:1;color:white;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";font-weight:600}.avatari--small{width:px(75);height:px(75);display:flex;justify-content:center;align-items:center;border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%}.avatari--small>.initials{font-size:px(24);line-height:1;color:white;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";font-weight:600}.avatari{width:px(100);height:px(100);display:flex;justify-content:center;align-items:center;border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%}.avatari>.initials{font-size:px(32);line-height:1;color:white;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";font-weight:600}.avatari--big{width:px(350);height:px(350);display:flex;justify-content:center;align-items:center;border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%}.avatari--big>.initials{font-size:px(74);line-height:1;color:white;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";font-weight:600}.avatari--raw{width:px(800);height:px(800);display:flex;justify-content:center;align-items:center;border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%}.avatari--raw>.initials{font-size:px(160);line-height:1;color:white;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";font-weight:600}
1
+ .avatari{display:flex;justify-content:center;align-items:center;border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%}.avatari>.initials{line-height:1;color:white;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";font-weight:600}
@@ -1,6 +1,4 @@
1
- @mixin avatar($size, $font)
2
- width: $size
3
- height: $size
1
+ .avatari
4
2
  display: flex
5
3
  justify-content: center
6
4
  align-items: center
@@ -8,21 +6,7 @@
8
6
  -webkit-border-radius: 50%
9
7
  -moz-border-radius: 50%
10
8
  & > .initials
11
- font-size: $font
12
9
  line-height: 1
13
10
  color: white
14
11
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"
15
12
  font-weight: 600
16
-
17
- .avatari--mini
18
- @include avatar(px(40), px(16))
19
- .avatari--tiny
20
- @include avatar(px(50), px(16))
21
- .avatari--small
22
- @include avatar(px(75), px(24))
23
- .avatari--medium
24
- @include avatar(px(100), px(32))
25
- .avatari--big
26
- @include avatar(px(350), px(74))
27
- .avatari--raw
28
- @include avatar(px(800), px(160))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avatari
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Hübotter
@@ -11,7 +11,7 @@ cert_chain: []
11
11
  date: 2018-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activerecord
14
+ name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: activesupport
28
+ name: carrierwave
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '5.0'
33
+ version: '1.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '5.0'
40
+ version: '1.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -94,9 +94,13 @@ files:
94
94
  - app/uploaders/avatari/avatar_uploader.rb
95
95
  - app/views/avatari/_avatar.html.erb
96
96
  - lib/avatari.rb
97
+ - lib/avatari/avatar.rb
98
+ - lib/avatari/configuration.rb
97
99
  - lib/avatari/engine.rb
98
100
  - lib/avatari/railtie.rb
99
101
  - lib/avatari/version.rb
102
+ - lib/generators/avatari_generator.rb
103
+ - lib/generators/templates/initializer.rb
100
104
  - vendor/assets/stylesheets/avatari.min.css
101
105
  - vendor/assets/stylesheets/avatari.sass
102
106
  homepage: https://github.com/jonhue/avatari