rails_execution 0.1.2 → 0.1.5

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: 7d04b62e7461cf38c3519551e7c96a9792418b5769d1e1782f317957c34bf617
4
- data.tar.gz: cf09e00dfb2b487fae01323230b337f02b2867729ff05a6887d7dce0494cddbb
3
+ metadata.gz: cb0ab6279226fbab64ea8e0a6619202528381361c6c63500dad575b5620f67b5
4
+ data.tar.gz: 97d51f2050d6a9f8ea2ba465eabe86a2c5b6d26b4b16c9f1fd209f22619ee42b
5
5
  SHA512:
6
- metadata.gz: 8ad2c6d2a14b9c6c3cb3be1e2728afbd9c691d5db1bd9d91f66a48d43219af9f334e4b9d23bf81d348e37422e7651d1d84469369845267d64b9ebf0aac4922de
7
- data.tar.gz: b2e17748bdc9cb169ed742cb19fbe4ff80fc759c5465b25448f1d2ab47043d0bc40005b6b1c643cdd4f81c61944751f98108109e7f1ea1fcb086b7c220a8eabe
6
+ metadata.gz: 82365cd8b77f73cd78e8caf3672cdbf50dc3093a6a0aee83b8db77e7977a1d87e1414c5248aefec4533f0f59f0fa046403d591dd95f238de813e8d0837f5cc96
7
+ data.tar.gz: fac5f09675ff272f0dc65f1be8b8c0b56c69504c8ac1396d8c7281b255b11b34f8f1c1662fb1d7a8a714c8b836cec464647c2e4afaea6ab7eed5393c18de72d8
data/README.md CHANGED
@@ -119,3 +119,68 @@ Default value: `20`
119
119
  ```ruby
120
120
  config.per_page = 10
121
121
  ```
122
+
123
+ #### Pro-tips
124
+ ##### Logging
125
+ - In editor, you can call the `log` method with `message` to log the message to logfile
126
+
127
+ Example:
128
+ ```ruby
129
+ log('Read upload file successful')
130
+ ```
131
+ Output:
132
+ ```
133
+ ==================== Wed, 22 Mar 2023 00:31:48
134
+ Read upload file successful
135
+ ```
136
+
137
+ And within the label liked it
138
+ ```ruby
139
+ log('Success', 'Read upload file')
140
+ ```
141
+ Output:
142
+ ```
143
+ ==================== Success
144
+ Read upload file
145
+ ```
146
+
147
+ ##### Show object Information
148
+
149
+ - If you want to show the object information in loop or in the step by step, you can use the `info` method to show **Model name** and **id**.
150
+ ```ruby
151
+ ids = [1, 2]
152
+ Product.where(id: ids).each do |product|
153
+ info(product)
154
+ # Your code to process the product
155
+ end
156
+ ```
157
+
158
+ Output:
159
+ ```
160
+ ==================== Product#1
161
+ ==================== Product#2
162
+ ```
163
+
164
+ ##### Force to stop the process
165
+ If you want to add the message to log and **STOP** process, you can use the `error!` method
166
+
167
+ Example:
168
+ ```ruby
169
+ error!('Bad-data', 'Unmatched value!') if var_x != var_y
170
+ ```
171
+ Output:
172
+ ```
173
+ ==================== Bad-data
174
+ Unmatched value!
175
+ ==================== Rolling back...
176
+ ```
177
+ OR you can use the `stop!` method to Stop the process
178
+
179
+ Example:
180
+ ```ruby
181
+ stop!
182
+ ```
183
+ or
184
+ ```ruby
185
+ stop!('bad-data')
186
+ ```
@@ -6,6 +6,22 @@
6
6
  Use
7
7
  %code Rails.logger.info 'message...'
8
8
  to add a message into log file
9
+ %span.tip
10
+ Use
11
+ %code info(object)
12
+ to add a Object info into log file
13
+ %span.tip
14
+ Use
15
+ %code log('Tag', 'message...')
16
+ to add a message into log file
17
+ %span.tip
18
+ Use
19
+ %code error!('Tag', 'message...')
20
+ to Stop the execution with the message and Tag in log file
21
+ %span.tip
22
+ Use
23
+ %code stop!('message...')
24
+ to Stop the execution with the message
9
25
  - if RailsExecution.configuration.file_upload
10
26
  %span.tip
11
27
  Call
@@ -33,15 +33,19 @@ module RailsExecution
33
33
  attr_reader :task
34
34
 
35
35
  def save_to_tempfile(file_name, url)
36
- file_extension = URI(url).path.split('.').last
37
- file_extension = ".#{file_extension}" if file_extension
38
- tmp_file = Tempfile.new([file_name, file_extension])
39
- tmp_file.binmode
40
- open(url) do |url_file|
41
- tmp_file.write(url_file.read)
42
- end
36
+ file = open(url)
37
+ return file if file.is_a?(Tempfile)
38
+
39
+ raise "Unsupported the Filetype #{file.class.name}" unless file.is_a?(StringIO)
43
40
 
44
- return tmp_file
41
+ file_extension = file.base_uri.path.split('.').last
42
+ file_extension = ".#{file_extension}" if file_extension
43
+ tempfile = Tempfile.new([file_name, file_extension])
44
+ tempfile.binmode
45
+ tempfile.write(file.string)
46
+ tempfile.rewind
47
+ tempfile.close
48
+ tempfile
45
49
  end
46
50
 
47
51
  end
@@ -2,6 +2,8 @@ module RailsExecution
2
2
  module Services
3
3
  class Executor
4
4
 
5
+ HIGHLIGHT = '=' * 20
6
+
5
7
  def initialize(task)
6
8
  @task = task
7
9
  @file_reader = ::RailsExecution.configuration.file_reader.new(task)
@@ -19,6 +21,29 @@ module RailsExecution
19
21
  @file_reader.get_file(name)
20
22
  end
21
23
 
24
+ def info(object)
25
+ model_name = object.respond_to?(:model_name) ? object.model_name.name : object.to_s
26
+ message = model_name + '#' + object&.id.to_s
27
+ log(message, nil)
28
+ end
29
+
30
+ def log(label = nil, message)
31
+ label ||= Time.current
32
+
33
+ Rails.logger.info("#{HIGHLIGHT} #{label}")
34
+ Rails.logger.info(message) if message
35
+ end
36
+
37
+ def error!(label = nil, message)
38
+ log(label, message)
39
+ stop!
40
+ end
41
+
42
+ def stop!(message = nil)
43
+ log('Rolling back...', message)
44
+ raise :rollback
45
+ end
46
+
22
47
  end
23
48
  end
24
49
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsExecution
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_execution
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Khoa Nguyen
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-05 00:00:00.000000000 Z
11
+ date: 2023-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml
@@ -135,7 +135,7 @@ metadata:
135
135
  homepage_uri: https://github.com/ThanhKhoaIT/rails_execution
136
136
  source_code_uri: https://github.com/ThanhKhoaIT/rails_execution
137
137
  changelog_uri: https://github.com/ThanhKhoaIT/rails_execution
138
- post_install_message:
138
+ post_install_message:
139
139
  rdoc_options: []
140
140
  require_paths:
141
141
  - lib
@@ -150,8 +150,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  - !ruby/object:Gem::Version
151
151
  version: '0'
152
152
  requirements: []
153
- rubygems_version: 3.1.2
154
- signing_key:
153
+ rubygems_version: 3.1.6
154
+ signing_key:
155
155
  specification_version: 4
156
156
  summary: 'Rails Engine: Execute the script for migration, cleanup data and specially
157
157
  case'