has_placeholder_image 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6ff5f16da8449225f6d79715b19a076995be630580359e16006894048a0dc17
4
- data.tar.gz: 7368e88ab1b04045d2ad2193266212fe17581034cd89871645b8f0787a50d2f1
3
+ metadata.gz: b83d9217534be1d8483da2bad6743d788324a6b3f6eeebccb207a5d6c2f7a030
4
+ data.tar.gz: a75dde1de13a45e8e561fcb8019bc18a06f261837f2e345e679b1147f7274bde
5
5
  SHA512:
6
- metadata.gz: d3cbd3018eba18812ba1d0c18bf9efbafeca5583c283298088b2d9797bfd4eb8136774e2b1149c2fe7e410d51cc8ab27da352000753ac06d74ecfdf806d4b9e8
7
- data.tar.gz: 05fa8285da159042181f8e0f606a924f5210ec26519c6b8f1ed165d5048c00c502542c070cbee1c3d46b315d575f8af5e28c244034239e50d3b29ffa117afade
6
+ metadata.gz: 5ead6a83aee65394d8d3a00f6903e6ef67edcc5630ce0bd1aa0c7d499d26933820ef3d43c2fb1bc6bb258b7ef9f7dad36c59034d5f2158a74322cf7d4d1f0042
7
+ data.tar.gz: 20c2dc76145c5a1b9a614b3f7efc0110acd7866f3b5c42e00ee21ba9b8d35444fd77a16b55444c42b225e082331c9fa92191a790cb04772252c360e7c595ce1d
data/README.md CHANGED
@@ -2,9 +2,10 @@
2
2
  ![Test](https://github.com/buck-ai/has-placeholder-image/workflows/Test/badge.svg)
3
3
  [![Maintainability](https://api.codeclimate.com/v1/badges/d7ff2ca5f9ceb238c353/maintainability)](https://codeclimate.com/github/buck-ai/has_placeholder_image/maintainability)
4
4
  [![Test Coverage](https://api.codeclimate.com/v1/badges/d7ff2ca5f9ceb238c353/test_coverage)](https://codeclimate.com/github/buck-ai/has_placeholder_image/test_coverage)
5
+ [![Gem Version](https://badge.fury.io/rb/has_placeholder_image.svg)](https://badge.fury.io/rb/has_placeholder_image)
5
6
 
6
7
 
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
+ Has Placeholder Image is a Ruby on Rails plugin that allows developers to generate placeholder images for models that has ActiveStorage fields.
8
9
 
9
10
  ## Installation
10
11
  Add this line to your application's Gemfile:
@@ -7,7 +7,7 @@ HasPlaceholderImage.setup do |config|
7
7
  config.transformer = 'two_word_first_letter_upcase'
8
8
  config.source = 'name'
9
9
  config.target = 'photo'
10
- config.output_path = Rails.root.join('tmp/placeholders')
10
+ config.output_path = 'tmp/placeholders'
11
11
  config.height = 300
12
12
  config.width = 300
13
13
  end
@@ -2,9 +2,10 @@
2
2
 
3
3
  # HasPlaceholderImage Plugin
4
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'
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
+ autoload :ImageGenerateJob, 'has_placeholder_image/image_generate_job'
8
9
 
9
10
  mattr_accessor :background_color
10
11
  @background_color = '#000000'
@@ -14,7 +14,7 @@ module HasPlaceholderImage
14
14
  mattr_accessor :placeholder_image_options
15
15
  self.placeholder_image_options = merged_options
16
16
 
17
- after_validation :generate_placeholder_image, if: :need_placeholder?
17
+ after_commit :generate_placeholder_image, if: :need_placeholder?
18
18
 
19
19
  include HasPlaceholderImage::ActiveRecord::InstanceMethods
20
20
  end
@@ -25,18 +25,17 @@ module HasPlaceholderImage
25
25
 
26
26
  def generate_placeholder_image
27
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
28
 
32
- @placeholder_image_target.attach(io: File.open(image.file), filename: File.basename(image.file))
29
+ ImageGenerateJob.perform_later(source_class: self.class.name,
30
+ id: id,
31
+ options: options)
33
32
  end
34
33
 
35
34
  def need_placeholder?
36
35
  @placeholder_image_target = send(self.class.placeholder_image_options[:target])
37
36
  @placeholder_image_source = send(self.class.placeholder_image_options[:source])
38
37
 
39
- !@placeholder_image_target.attached? && !@placeholder_image_source.blank?
38
+ !@placeholder_image_target.attached? && @placeholder_image_source.present?
40
39
  end
41
40
  end
42
41
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HasPlaceholderImage
4
+ class ImageGenerateJob < ApplicationJob
5
+ queue_as :default
6
+
7
+ # rubocop:disable Metrics/AbcSize
8
+ def perform(source_class:, id:, options:)
9
+ model = source_class.constantize
10
+ record = model.find(id)
11
+
12
+ source_attr = record.send(options[:source])
13
+ target_attr = record.send(options[:target])
14
+
15
+ text = TextGenerator.send(options[:transformer], source_attr)
16
+ image = ImageGenerator.new(text, **options)
17
+
18
+ target_attr.attach(io: File.open(image.file), filename: File.basename(image.file))
19
+ end
20
+ # rubocop:enable Metrics/AbcSize
21
+ end
22
+ end
@@ -5,11 +5,12 @@ module HasPlaceholderImage
5
5
  module TextGenerator
6
6
  # This method take text field and parse with delimiter and take first word_count number word
7
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)
8
+ def self.two_word_first_letter_upcase(value)
9
+ value.split
11
10
  .map(&:first)
11
+ .select { |l| l =~ /[[:alpha:]]/ }
12
12
  .join
13
+ .first(2)
13
14
  .upcase
14
15
  end
15
16
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HasPlaceholderImage
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_placeholder_image
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muhammet Dilmaç
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-07-05 00:00:00.000000000 Z
12
+ date: 2020-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -84,6 +84,7 @@ files:
84
84
  - lib/generators/templates/has_placeholder_image.rb
85
85
  - lib/has_placeholder_image.rb
86
86
  - lib/has_placeholder_image/active_record.rb
87
+ - lib/has_placeholder_image/image_generate_job.rb
87
88
  - lib/has_placeholder_image/image_generator.rb
88
89
  - lib/has_placeholder_image/railtie.rb
89
90
  - lib/has_placeholder_image/text_generator.rb