rails_com 1.2.4 → 1.2.5

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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/Rakefile +4 -17
  4. data/app/assets/images/image-square.png +0 -0
  5. data/app/assets/javascripts/rails_com/application.js +43 -1
  6. data/{lib/assets/javascripts/input-attachment.js → app/assets/javascripts/rails_com/attachment.js} +24 -150
  7. data/app/assets/javascripts/rails_com/picture.js +17 -0
  8. data/app/controllers/{active_storage → active_storage_ext/admin}/attachments_controller.rb +2 -2
  9. data/app/controllers/active_storage_ext/admin/base_controller.rb +4 -0
  10. data/app/controllers/active_storage_ext/admin/blob_defaults_controller.rb +54 -0
  11. data/app/controllers/active_storage_ext/admin/blobs_controller.rb +48 -0
  12. data/app/controllers/active_storage_ext/audios_controller.rb +24 -0
  13. data/app/controllers/concerns/rails_common_api.rb +50 -0
  14. data/app/controllers/concerns/rails_common_xhr.rb +15 -0
  15. data/app/helpers/rails_com/active_helper.rb +21 -9
  16. data/app/helpers/rails_com/assets_helper.rb +57 -23
  17. data/app/helpers/rails_com/format_helper.rb +2 -2
  18. data/app/helpers/rails_com/time_helper.rb +2 -1
  19. data/app/models/active_storage/blob_default.rb +5 -0
  20. data/app/models/application_record.rb +3 -0
  21. data/app/models/{state_machine.rb → concerns/state_machine.rb} +0 -0
  22. data/app/views/{active_storage → active_storage_ext/admin}/attachments/destroy.js.erb +0 -0
  23. data/app/views/active_storage_ext/admin/blob_defaults/_form.html.erb +7 -0
  24. data/app/views/active_storage_ext/admin/blob_defaults/_search_form.html.erb +7 -0
  25. data/app/views/active_storage_ext/admin/blob_defaults/edit.html.erb +9 -0
  26. data/app/views/active_storage_ext/admin/blob_defaults/index.html.erb +48 -0
  27. data/app/views/active_storage_ext/admin/blob_defaults/new.html.erb +9 -0
  28. data/app/views/active_storage_ext/admin/blob_defaults/show.html.erb +10 -0
  29. data/app/views/active_storage_ext/admin/blobs/_search_form.html.erb +9 -0
  30. data/app/views/active_storage_ext/admin/blobs/destroy.js.erb +1 -0
  31. data/app/views/active_storage_ext/admin/blobs/index.html.erb +51 -0
  32. data/app/views/active_storage_ext/admin/blobs/new.html.erb +13 -0
  33. data/app/views/active_storage_ext/admin/blobs/show.html.erb +10 -0
  34. data/app/views/active_storage_ext/attachments/_default_image_item.html.erb +6 -0
  35. data/app/views/active_storage_ext/attachments/_image_item.html.erb +6 -0
  36. data/app/views/{active_storage → active_storage_ext}/attachments/_list.html.erb +1 -4
  37. data/app/views/{active_storage → active_storage_ext}/attachments/_list_edit.html.erb +1 -1
  38. data/app/views/{active_storage → active_storage_ext}/attachments/_list_form.html.erb +8 -8
  39. data/app/views/{active_storage → active_storage_ext}/attachments/_video_item.html.erb +0 -0
  40. data/app/views/active_storage_ext/audios/show.html.erb +5 -0
  41. data/config/locales/en.yml +2 -1
  42. data/config/locales/zh.yml +2 -1
  43. data/config/routes.rb +13 -6
  44. data/db/migrate/20181012025833_rails_com_init.rb +13 -0
  45. data/lib/mina/sidekiq.rb +3 -1
  46. data/lib/rails_com.rb +3 -1
  47. data/lib/rails_com/config.rb +2 -1
  48. data/lib/rails_com/core_ext/array.rb +2 -2
  49. data/lib/rails_com/core_ext/date.rb +3 -2
  50. data/lib/rails_com/engine.rb +3 -5
  51. data/lib/rails_com/helpers/model_helper.rb +10 -8
  52. data/lib/rails_com/rails_ext/activestorage_attached.rb +1 -1
  53. data/lib/rails_com/rails_ext/attached_macros.rb +21 -0
  54. data/lib/rails_com/rails_ext/attachment_transfer.rb +3 -1
  55. data/lib/rails_com/rails_ext/errors.rb +4 -0
  56. data/lib/rails_com/rails_ext/template_renderer.rb +15 -0
  57. data/lib/rails_com/utils/babel.rb +38 -0
  58. data/lib/rails_com/utils/time_helper.rb +9 -4
  59. data/lib/rails_com/version.rb +2 -2
  60. data/lib/templates/erb/scaffold/edit.html.erb.tt +2 -2
  61. data/lib/templates/erb/scaffold/new.html.erb.tt +2 -2
  62. data/lib/templates/erb/scaffold/show.html.erb.tt +2 -2
  63. metadata +39 -14
  64. data/app/assets/javascripts/rails_com/common.js +0 -38
  65. data/app/controllers/concerns/the_common_api.rb +0 -41
  66. data/app/views/active_storage/attachments/_image_item.html.erb +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e44eabdf535b6568792758d0e46b070ebe47f924a477a24cc65f2f30c41b0be
4
- data.tar.gz: b43af789f129ad25e2d580d600c6e54132e6e462f4a3c38d1665d52da6f62d22
3
+ metadata.gz: d89a903e9f829ba767afbef9f90d9a7ddf635ce8d108223ca745f9dc6578f48f
4
+ data.tar.gz: 389b8a92919cda43adc6024090f3c8aa67bfb33aaa3d2aaf710e27e0235b31e5
5
5
  SHA512:
6
- metadata.gz: f3d55a1073296dbdae0858561c7569a567ce5769c6e3c2381f50c9e14a7d6de7e142419a69a7178bb0eaf7cf818e02b74fa1b33a4d1d4087452bee2ad7e4758e
7
- data.tar.gz: b1eb52db37b960b9f3bf20cca2429d64b7d7e35ab842c22398c69fb8ca4186012b3dde71de9be3625c95ec7ce2e16ac4334b571617186090c94a063f6ce661ff
6
+ metadata.gz: 1d0616448bc813c6da2eb8794f2a5894bb671bf2e2a47cf0231bd46d30814046b25d7737ecb7b26f5df89cc40e013fed5731b1ab80722ec8c84bcf0b9570af34
7
+ data.tar.gz: 85f715d2616e82f7fba685963964524c71b49e6575a217ea6b1022bf7ee3c4e10640578fe14210f0818d399187a3432582f1bc612d05db5c35e950aac1fd309f
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # RailsCom
2
2
  Rails 通用基础库
3
3
 
4
- ## 说明
4
+ ## 功能
5
5
  * examples: puma, configs etc.
6
6
  * rails and ruby core extension;
7
7
  * methods deal rails model/controller/routes etc.
data/Rakefile CHANGED
@@ -1,9 +1,5 @@
1
- begin
2
- require 'bundler/setup'
3
- rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
- end
6
-
1
+ require 'bundler/setup'
2
+ require 'pry'
7
3
  require 'sdoc'
8
4
  require 'rdoc/task'
9
5
 
@@ -13,22 +9,13 @@ RDoc::Task.new(:sdoc) do |rdoc|
13
9
  rdoc.options << '--format=sdoc'
14
10
  rdoc.template = 'rails'
15
11
  rdoc.rdoc_files.include('README.md')
12
+ rdoc.rdoc_files.include('app/**/*.rb')
16
13
  rdoc.rdoc_files.include('lib/**/*.rb')
17
14
  end
18
15
 
19
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
16
+ APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
20
17
  load 'rails/tasks/engine.rake'
21
18
  load 'rails/tasks/statistics.rake'
22
19
 
23
20
  require 'bundler/gem_tasks'
24
- require 'rake/testtask'
25
-
26
- Rake::TestTask.new(:test) do |t|
27
- t.libs << 'lib'
28
- t.libs << 'test'
29
- t.pattern = 'test/**/*_test.rb'
30
- t.verbose = false
31
- end
32
-
33
21
 
34
- task default: :test
@@ -1,3 +1,45 @@
1
1
  //= require rails-ujs
2
2
  //= require turbolinks
3
- //= require_self
3
+ //= require stimulus
4
+ //= require activestorage
5
+ //= require_self
6
+
7
+ function timeForLocalized(){
8
+ $('time[data-localized!="true"]').each(function(){
9
+ if (this.textContent.length > 0) {
10
+ var format = this.dataset['format'] || 'YYYY-MM-DD HH:mm:ss';
11
+ this.textContent = moment.utc(this.textContent).local().format(format);
12
+ this.dataset['localized'] = 'true'
13
+ }
14
+ })
15
+ }
16
+
17
+ function clickCallback(e) {
18
+ if (e.target.tagName !== 'A') {
19
+ return;
20
+ }
21
+ (new Date).getTimezoneOffset();
22
+ }
23
+ //document.addEventListener('click', clickCallback, false);
24
+
25
+ function htmlToElement(html_str) {
26
+ var template = document.createElement('template');
27
+ template.innerHTML = html_str.trim();
28
+ return template.content.firstChild;
29
+ }
30
+
31
+ document.addEventListener('DOMContentLoaded', function() {
32
+ timeForLocalized()
33
+ });
34
+ document.addEventListener('turbolinks:load', function() {
35
+ timeForLocalized()
36
+ });
37
+ document.addEventListener('turbolinks:visit', function() {
38
+ timeForLocalized()
39
+ });
40
+
41
+ document.addEventListener('turbolinks:request-start', function(event) {
42
+ var xhr = event.data.xhr;
43
+ var offset = (new Date).getTimezoneOffset();
44
+ xhr.setRequestHeader('Utc-Offset', offset);
45
+ });
@@ -3,129 +3,19 @@
3
3
 
4
4
  /*
5
5
  * Input Field with Attachment
6
- * Fork and Inspired by inlineAttachment(https://github.com/Rovak/InlineAttachment)
7
- * Author: mingyuan.qin
8
- * Contact: mingyuan0715@foxmail.com
6
+ * based on https://github.com/Rovak/InlineAttachment/blob/master/LICENSE
9
7
  */
10
8
 
11
9
  (function(document, window) {
12
10
  'use strict';
13
11
 
14
12
  var InputAttachment = function(options) {
15
- this.settings = InputAttachment.util.merge(options, InputAttachment.defaults);
16
- this.editor = InputAttachment.editors.initEditor(this.settings['editor']);
13
+ this.settings = Object.assign(options, InputAttachment.defaults);
14
+ this.editor = this.settings['editor'];
17
15
  this.filenameTag = '{filename}';
18
16
  this.lastValue = null;
19
17
  };
20
18
 
21
- /**
22
- * Will holds the available editors
23
- *
24
- * @type {Object}
25
- */
26
- InputAttachment.editors = {
27
-
28
- initEditor: function(input) {
29
-
30
- return {
31
- getValue: function() {
32
- return input.value;
33
- },
34
-
35
- insertValue: function(val) {
36
- InputAttachment.util.insertTextAtCursor(input, val);
37
- },
38
-
39
- setValue: function(val) {
40
- input.value = val;
41
- }
42
- }
43
- }
44
- };
45
-
46
- /**
47
- * Utility functions
48
- */
49
- InputAttachment.util = {
50
-
51
- /**
52
- * Simple function to merge the given objects
53
- *
54
- * @param {Object[]} object Multiple object parameters
55
- * @returns {Object}
56
- */
57
- merge: function() {
58
- var result = {};
59
- for (var i = arguments.length - 1; i >= 0; i--) {
60
- var obj = arguments[i];
61
- for (var k in obj) {
62
- if (obj.hasOwnProperty(k)) {
63
- result[k] = obj[k];
64
- }
65
- }
66
- }
67
- return result;
68
- },
69
-
70
- /**
71
- * Append a line of text at the bottom, ensuring there aren't unnecessary newlines
72
- *
73
- * @param {String} appended Current content
74
- * @param {String} previous Value which should be appended after the current content
75
- */
76
- appendInItsOwnLine: function(previous, appended) {
77
- return (previous + "\n\n[[D]]" + appended)
78
- .replace(/(\n{2,})\[\[D\]\]/, "\n\n")
79
- .replace(/^(\n*)/, "");
80
- },
81
-
82
- /**
83
- * Inserts the given value at the current cursor position of the textarea element
84
- *
85
- * @param {HTMLElement} el
86
- * @param {String} text Text which will be inserted at the cursor position
87
- */
88
- insertTextAtCursor: function(el, text) {
89
- var scrollPos = el.scrollTop,
90
- strPos = 0,
91
- browser = false,
92
- range;
93
-
94
- if ((el.selectionStart || el.selectionStart === '0')) {
95
- browser = 'ff';
96
- } else if (document.selection) {
97
- browser = 'ie';
98
- }
99
-
100
- if (browser === 'ie') {
101
- el.focus();
102
- range = document.selection.createRange();
103
- range.moveStart('character', -el.value.length);
104
- strPos = range.text.length;
105
- } else if (browser === 'ff') {
106
- strPos = el.selectionStart;
107
- }
108
-
109
- var front = (el.value).substring(0, strPos);
110
- var back = (el.value).substring(strPos, el.value.length);
111
- el.value = front + text + back;
112
- strPos = strPos + text.length;
113
- if (browser === 'ie') {
114
- el.focus();
115
- range = document.selection.createRange();
116
- range.moveStart('character', -el.value.length);
117
- range.moveStart('character', strPos);
118
- range.moveEnd('character', 0);
119
- range.select();
120
- } else if (browser === "ff") {
121
- el.selectionStart = strPos;
122
- el.selectionEnd = strPos;
123
- el.focus();
124
- }
125
- el.scrollTop = scrollPos;
126
- }
127
- };
128
-
129
19
  /**
130
20
  * Default configuration options
131
21
  *
@@ -210,7 +100,9 @@
210
100
  /**
211
101
  * Triggers when a file is dropped or pasted
212
102
  */
213
- onFileReceived: function() {},
103
+ onFileReceived: function() {
104
+
105
+ },
214
106
 
215
107
  /**
216
108
  * Custom upload handler
@@ -271,7 +163,7 @@
271
163
  formData.append(settings.uploadFieldName, file, remoteFilename);
272
164
 
273
165
  // Append the extra parameters to the form data
274
- if (typeof settings.extraParams === "object") {
166
+ if (typeof settings.extraParams === 'object') {
275
167
  for (var key in settings.extraParams) {
276
168
  if (settings.extraParams.hasOwnProperty(key)) {
277
169
  formData.append(key, settings.extraParams[key]);
@@ -305,36 +197,18 @@
305
197
  };
306
198
 
307
199
  InputAttachment.prototype.previewFile = function(file, obj) {
308
- var fileList = document.getElementById(this.settings.previewDiv),
309
- img = new Image();
310
- img.classList.add('ui', 'small', 'image');
311
-
312
- if(window.URL){
313
- //File API
314
- img.src = window.URL.createObjectURL(file); //创建一个object URL,并不是你的本地路径
315
- img.onload = function(e) {
316
- window.URL.revokeObjectURL(this.src); //图片加载后,释放object URL
317
- }
318
- } else if(window.FileReader){
319
- //opera不支持createObjectURL/revokeObjectURL方法。我们用FileReader对象来处理
320
- var reader = new FileReader();
321
- reader.readAsDataURL(file);
322
- reader.onload = function(e){
323
- img.src = this.result;
324
- }
325
- }else{
326
- //ie
327
- obj.select();
328
- obj.blur();
329
- var nfile = document.selection.createRange().text;
330
- document.selection.empty();
331
- img.src = nfile;
332
- img.onload=function(e){
333
-
334
- }
200
+ var fileList = document.getElementById(this.settings.previewDiv)
201
+ var template = document.createElement('template');
202
+ template.innerHTML = fileList.firstElementChild.outerHTML.trim();
203
+ var img_div = template.content.firstChild;
204
+ var img = img_div.lastElementChild;
205
+
206
+ img.src = window.URL.createObjectURL(file); //创建一个object URL,并不是你的本地路径
207
+ img.onload = function(e) {
208
+ window.URL.revokeObjectURL(this.src); //图片加载后,释放object URL
335
209
  }
336
210
 
337
- fileList.appendChild(img);
211
+ fileList.appendChild(img_div);
338
212
  };
339
213
 
340
214
  /**
@@ -369,8 +243,7 @@
369
243
  } else {
370
244
  newValue = this.settings.urlText.replace(this.filenameTag, filename);
371
245
  }
372
- var text = this.editor.getValue().replace(this.lastValue, newValue);
373
- this.editor.setValue(text);
246
+ this.editor.input.value.replace(this.lastValue, newValue);
374
247
  this.settings.onFileUploaded.call(this, filename, result);
375
248
  }
376
249
  }
@@ -385,8 +258,7 @@
385
258
  */
386
259
  InputAttachment.prototype.onFileUploadError = function(xhr) {
387
260
  if (this.settings.onFileUploadError.call(this, xhr) !== false) {
388
- var text = this.editor.getValue().replace(this.lastValue, "");
389
- this.editor.setValue(text);
261
+ this.editor.input.value.replace(this.lastValue, '');
390
262
  }
391
263
  };
392
264
 
@@ -399,7 +271,10 @@
399
271
  InputAttachment.prototype.onFileInserted = function(file) {
400
272
  if (this.settings.onFileReceived.call(this, file) !== false) {
401
273
  this.lastValue = this.settings.progressText;
402
- this.editor.insertValue(this.lastValue);
274
+
275
+ var scrollPos = el.scrollTop
276
+ el.value = this.lastValue;
277
+ el.scrollTop = scrollPos;
403
278
  }
404
279
  };
405
280
 
@@ -538,5 +413,4 @@
538
413
 
539
414
  window.attachToInput = attachToInput;
540
415
  window.attachToPreview = attachToPreview;
541
-
542
- })(document, window);
416
+ })(document, window);
@@ -0,0 +1,17 @@
1
+ //= require ./attachment
2
+
3
+ class PictureController extends Stimulus.Controller {
4
+ greet() {
5
+ console.log('Hellos, Stimulus!', this.element)
6
+ }
7
+
8
+ connect() {
9
+ console.log('Connects, Stimulus!', this.element)
10
+ attachToPreview({
11
+ fileInput: 'picture_file'
12
+ });
13
+ }
14
+ }
15
+ PictureController.targets = ['src']
16
+ const application = Stimulus.Application.start()
17
+ application.register('picture', PictureController)
@@ -1,4 +1,4 @@
1
- class ActiveStorage::AttachmentsController < ActiveStorage::BaseController
1
+ class ActiveStorageExt::Admin::AttachmentsController < ActiveStorageExt::Admin::BaseController
2
2
  before_action :set_attachment, only: [:destroy]
3
3
 
4
4
  def destroy
@@ -10,4 +10,4 @@ class ActiveStorage::AttachmentsController < ActiveStorage::BaseController
10
10
  @attachment = ActiveStorage::Attachment.find(params[:id])
11
11
  end
12
12
 
13
- end
13
+ end
@@ -0,0 +1,4 @@
1
+ class ActiveStorageExt::Admin::BaseController < RailsCom.config.admin_class.constantize
2
+
3
+
4
+ end
@@ -0,0 +1,54 @@
1
+ class ActiveStorageExt::Admin::BlobDefaultsController < ActiveStorageExt::Admin::BaseController
2
+ before_action :set_blob_default, only: [:show, :edit, :update, :destroy]
3
+
4
+ def index
5
+ @blob_defaults = ActiveStorage::BlobDefault.page(params[:page])
6
+ end
7
+
8
+ def new
9
+ @blob_default = ActiveStorage::BlobDefault.new
10
+ end
11
+
12
+ def create
13
+ @blob_default = ActiveStorage::BlobDefault.new(blob_default_params)
14
+
15
+ if @blob_default.save
16
+ redirect_to rails_ext_blob_defaults_url, notice: 'Blob default was successfully created.'
17
+ else
18
+ render :new
19
+ end
20
+ end
21
+
22
+ def show
23
+ end
24
+
25
+ def edit
26
+ end
27
+
28
+ def update
29
+ if @blob_default.update(blob_default_params)
30
+ redirect_to rails_ext_blob_defaults_url, notice: 'Blob default was successfully updated.'
31
+ else
32
+ render :edit
33
+ end
34
+ end
35
+
36
+ def destroy
37
+ @blob_default.destroy
38
+ redirect_to rails_ext_blob_defaults_url, notice: 'Blob default was successfully destroyed.'
39
+ end
40
+
41
+ private
42
+ def set_blob_default
43
+ @blob_default = ActiveStorage::BlobDefault.find(params[:id])
44
+ end
45
+
46
+ def blob_default_params
47
+ params.fetch(:blob_default, {}).permit(
48
+ :record_class,
49
+ :name,
50
+ :file
51
+ )
52
+ end
53
+
54
+ end
@@ -0,0 +1,48 @@
1
+ class ActiveStorageExt::Admin::BlobsController < ActiveStorageExt::Admin::BaseController
2
+ before_action :set_blob, only: [:destroy]
3
+
4
+ def index
5
+ q_params = {}.with_indifferent_access
6
+ q_params.merge params.fetch(:q, {}).permit(:key, :filename)
7
+ @blobs = ActiveStorage::Blob.default_where(q_params).order(id: :desc).page(params[:page])
8
+ end
9
+
10
+ def unattached
11
+ q_params = {}.with_indifferent_access
12
+ q_params.merge params.fetch(:q, {}).permit(:key, :filename)
13
+ @blobs = ActiveStorage::Blob.unattached.default_where(q_params).order(id: :desc).page(params[:page])
14
+ render :index
15
+ end
16
+
17
+ def new
18
+ @blob = ActiveStorage::Blob.new(filename: '')
19
+ end
20
+
21
+ def create
22
+ @blob = ActiveStorage::Blob.build_after_upload(io: blob_params[:io].tempfile, filename: blob_params[:io].original_filename)
23
+
24
+ if @blob.save
25
+ redirect_to rails_ext_blobs_url, notice: 'Blob was successfully created.'
26
+ else
27
+ render :new
28
+ end
29
+ end
30
+
31
+ def destroy
32
+ @blob.purge
33
+ redirect_to rails_ext_blobs_url, notice: 'Blob was successfully destroyed.'
34
+ end
35
+
36
+ private
37
+ def set_blob
38
+ @blob = ActiveStorage::Blob.find(params[:id])
39
+ end
40
+
41
+ def blob_params
42
+ params.fetch(:blob, {}).permit(
43
+ :key,
44
+ :io
45
+ )
46
+ end
47
+
48
+ end