has_placeholder_image 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
+ SHA256:
3
+ metadata.gz: a6ff5f16da8449225f6d79715b19a076995be630580359e16006894048a0dc17
4
+ data.tar.gz: 7368e88ab1b04045d2ad2193266212fe17581034cd89871645b8f0787a50d2f1
5
+ SHA512:
6
+ metadata.gz: d3cbd3018eba18812ba1d0c18bf9efbafeca5583c283298088b2d9797bfd4eb8136774e2b1149c2fe7e410d51cc8ab27da352000753ac06d74ecfdf806d4b9e8
7
+ data.tar.gz: 05fa8285da159042181f8e0f606a924f5210ec26519c6b8f1ed165d5048c00c502542c070cbee1c3d46b315d575f8af5e28c244034239e50d3b29ffa117afade
@@ -0,0 +1,20 @@
1
+ Copyright 2020 Muhammet Dilmaç
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.
@@ -0,0 +1,95 @@
1
+ # HasPlaceholderImage
2
+ ![Test](https://github.com/buck-ai/has-placeholder-image/workflows/Test/badge.svg)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/d7ff2ca5f9ceb238c353/maintainability)](https://codeclimate.com/github/buck-ai/has_placeholder_image/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/d7ff2ca5f9ceb238c353/test_coverage)](https://codeclimate.com/github/buck-ai/has_placeholder_image/test_coverage)
5
+
6
+
7
+ Has Placeholder Image is a Ruby on Rails gem that allows developers to generate placeholder images for models depending on the title or name attributes of model.
8
+
9
+ ## Installation
10
+ Add this line to your application's Gemfile:
11
+ ```ruby
12
+ gem 'has_placeholder_image'
13
+ ```
14
+
15
+ And then execute:
16
+ ```bash
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it yourself as:
21
+ ```bash
22
+ $ gem install has_placeholder_image
23
+ ```
24
+
25
+ after generate configuration file with this command;
26
+ ```shell script
27
+ rails g has_placeholder_image:install
28
+ ```
29
+
30
+ ## Usage
31
+ Insert this method to your model which will have placeholder images:
32
+
33
+ `has_placeholder_image`
34
+
35
+ You can customize default configuration with `config/initializers/has_placeholder_image.rb` or you can customize placeholder image generation on model basis by adding custom attributes to placeholder callback.
36
+
37
+ For example;
38
+ ```ruby
39
+ has_placeholder_image source: :title,
40
+ target: :logo,
41
+ background_color: '#335eea'
42
+ ```
43
+
44
+ ## Configuration
45
+
46
+ The default configuration lies in `config/initializers/has_placeholder_image.rb` and can be customized according to your liking.
47
+
48
+ ```ruby
49
+ HasPlaceholderImage.setup do |config|
50
+ config.background_color = '#000000'
51
+ config.font_color = '#FFFFFF'
52
+ config.font_size = 200
53
+ config.transformer = 'two_word_first_letter_upcase'
54
+ config.source = 'name'
55
+ config.target = 'photo'
56
+ config.output_path = Rails.root.join('tmp/placeholders')
57
+ config.height = 300
58
+ config.width = 300
59
+ end
60
+ ```
61
+
62
+ | Key | Description |
63
+ | --- | ----------- |
64
+ | `background_color` | For image background color. |
65
+ | `font_color` | For text color. |
66
+ | `font_size` | For font size. |
67
+ | `transformer` | How to generate your image title. This time only have `'two_word_first_letter_upcase'` method. |
68
+ | `source` | Placeholder image text generate with this field. |
69
+ | `target` | Your active storage attribute on your model. |
70
+ | `output_path` | Where to store generated images path. |
71
+ | `height` | Generated image height. |
72
+ | `width` | Generated image width. |
73
+
74
+ ## Example
75
+ app/models/company.rb
76
+ ```ruby
77
+ # frozen_string_literal: true
78
+
79
+ class Company < ApplicationRecord
80
+ has_one_attached :logo
81
+ has_placeholder_image source: :title, target: :logo,
82
+ background_color: '#335eea',
83
+ font_size: 200
84
+
85
+ validates :title, presence: true
86
+ end
87
+ ```
88
+ ![Output](docs/example.png)
89
+
90
+
91
+ ## Contributing
92
+ See the contributing guide.
93
+
94
+ ## License
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'HasPlaceholderImage'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'bundler/gem_tasks'
20
+
21
+ require 'rake/testtask'
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = false
27
+ end
28
+
29
+ task default: :test
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HasPlaceholderImage
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __dir__)
7
+
8
+ desc 'Creates a Has Placeholder Image initializer to your application.'
9
+
10
+ def copy_initializer
11
+ template 'has_placeholder_image.rb', 'config/initializers/has_placeholder_image.rb'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ HasPlaceholderImage.setup do |config|
4
+ config.background_color = '#000000'
5
+ config.font_color = '#FFFFFF'
6
+ config.font_size = 200
7
+ config.transformer = 'two_word_first_letter_upcase'
8
+ config.source = 'name'
9
+ config.target = 'photo'
10
+ config.output_path = Rails.root.join('tmp/placeholders')
11
+ config.height = 300
12
+ config.width = 300
13
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # HasPlaceholderImage Plugin
4
+ module HasPlaceholderImage
5
+ autoload :ActiveRecord, 'has_placeholder_image/active_record'
6
+ autoload :TextGenerator, 'has_placeholder_image/text_generator'
7
+ autoload :ImageGenerator, 'has_placeholder_image/image_generator'
8
+
9
+ mattr_accessor :background_color
10
+ @background_color = '#000000'
11
+
12
+ mattr_accessor :font_color
13
+ @font_color = :'#FFFFFF'
14
+
15
+ mattr_accessor :font_size
16
+ @font_size = 50
17
+
18
+ mattr_accessor :transformer
19
+ @transformer = 'two_word_first_letter_upcase'
20
+
21
+ mattr_accessor :source
22
+ @source = :name
23
+
24
+ mattr_accessor :target
25
+ @target = :photo
26
+
27
+ mattr_accessor :output_path
28
+ @output_path = 'tmp/placeholders'
29
+
30
+ mattr_accessor :height
31
+ @height = 300
32
+
33
+ mattr_accessor :width
34
+ @width = 300
35
+
36
+ def self.default_options
37
+ variables = {}
38
+
39
+ class_variables.each do |variable|
40
+ variable_name = variable.to_s.delete('@').to_sym
41
+ variables[variable_name] = class_variable_get(variable)
42
+ end
43
+
44
+ variables
45
+ end
46
+
47
+ def self.setup
48
+ yield self
49
+ end
50
+ end
51
+
52
+ require 'has_placeholder_image/railtie'
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HasPlaceholderImage
4
+ module ActiveRecord
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def has_placeholder_image(**options)
11
+ default_options = HasPlaceholderImage.default_options
12
+ merged_options = default_options.update(options)
13
+
14
+ mattr_accessor :placeholder_image_options
15
+ self.placeholder_image_options = merged_options
16
+
17
+ after_validation :generate_placeholder_image, if: :need_placeholder?
18
+
19
+ include HasPlaceholderImage::ActiveRecord::InstanceMethods
20
+ end
21
+ end
22
+
23
+ module InstanceMethods
24
+ private
25
+
26
+ def generate_placeholder_image
27
+ options = self.class.placeholder_image_options
28
+ text = HasPlaceholderImage::TextGenerator.send(options[:transformer],
29
+ @placeholder_image_source)
30
+ image = HasPlaceholderImage::ImageGenerator.new(text, **options)
31
+
32
+ @placeholder_image_target.attach(io: File.open(image.file), filename: File.basename(image.file))
33
+ end
34
+
35
+ def need_placeholder?
36
+ @placeholder_image_target = send(self.class.placeholder_image_options[:target])
37
+ @placeholder_image_source = send(self.class.placeholder_image_options[:source])
38
+
39
+ !@placeholder_image_target.attached? && !@placeholder_image_source.blank?
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rmagick'
4
+
5
+ module HasPlaceholderImage
6
+ class ImageGenerator
7
+ attr_reader :options, :canvas, :draw, :file, :text
8
+
9
+ def initialize(text, **params)
10
+ @options = params
11
+ @text = text
12
+ return if exist?
13
+
14
+ generate_canvas
15
+ generate_draw
16
+ generate_result
17
+ end
18
+
19
+ private
20
+
21
+ def exist?
22
+ base_folder = ::Rails.root.join(@options[:output_path])
23
+ size_folder = base_folder.join("#{@options[:height]}x#{@options[:width]}")
24
+ font_folder = size_folder.join((@options[:font_size]).to_s)
25
+ file_name = "bg_#{@options[:background_color]}-cl_#{@options[:font_color]}-#{@text}.png"
26
+ @file = font_folder.join(file_name)
27
+
28
+ File.exist?(@file)
29
+ end
30
+
31
+ def generate_canvas
32
+ @canvas = ::Magick::Image.new(@options[:height], @options[:width])
33
+ @canvas.color_reset!(@options[:background_color])
34
+ end
35
+
36
+ def generate_draw
37
+ @draw = ::Magick::Draw.new
38
+ @draw.pointsize = @options[:font_size]
39
+ @draw.fill = @options[:font_color]
40
+ @draw.gravity = ::Magick::CenterGravity
41
+ @draw.annotate(@canvas, 0, 0, 0, 0, @text)
42
+ end
43
+
44
+ def generate_result
45
+ FileUtils.mkdir_p(File.dirname(@file))
46
+ @canvas.write(@file)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record/railtie'
4
+
5
+ module HasPlaceholderImage
6
+ class Railtie < ::Rails::Railtie
7
+ initializer 'has_placeholder_image.initialize' do |app|
8
+ railtie_collection = app.railties
9
+ activerecord_railtie = railtie_collection.select { |railtie| railtie.class.to_s == 'ActiveRecord::Railtie' }
10
+ ::ActiveRecord::Base.include(HasPlaceholderImage::ActiveRecord) if activerecord_railtie
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HasPlaceholderImage
4
+ # Text generation methods
5
+ module TextGenerator
6
+ # This method take text field and parse with delimiter and take first word_count number word
7
+ # first letter upcase
8
+ def self.two_word_first_letter_upcase(value, delimiter: ' ', word_count: 2)
9
+ value.split(delimiter)
10
+ .first(word_count)
11
+ .map(&:first)
12
+ .join
13
+ .upcase
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HasPlaceholderImage
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_placeholder_image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Muhammet Dilmaç
8
+ - Utku Kaynar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2020-07-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 5.0.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 5.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rmagick
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rubocop
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: sqlite3
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: Has Placeholder Image is a Ruby on Rails gem that allows developers to
71
+ generate placeholder images for models depending on the title or name attributes
72
+ of model.
73
+ email:
74
+ - iletisim@muhammetdilmac.com.tr
75
+ - utku@buck.ai
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - MIT-LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - lib/generators/has_placeholder_image/install_generator.rb
84
+ - lib/generators/templates/has_placeholder_image.rb
85
+ - lib/has_placeholder_image.rb
86
+ - lib/has_placeholder_image/active_record.rb
87
+ - lib/has_placeholder_image/image_generator.rb
88
+ - lib/has_placeholder_image/railtie.rb
89
+ - lib/has_placeholder_image/text_generator.rb
90
+ - lib/has_placeholder_image/version.rb
91
+ homepage: https://www.github.com/buck-ai/has-placeholder-image
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.0.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A Ruby gem for generating string based placeholder images in Rails.
114
+ test_files: []