rails_com 1.2.1 → 1.2.2

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 (68) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +165 -0
  3. data/README.md +18 -26
  4. data/Rakefile +6 -9
  5. data/app/assets/config/rails_com_manifest.js +2 -0
  6. data/app/assets/javascripts/controllers/active_storage_ext/videos/show.ready.js +1 -0
  7. data/app/assets/javascripts/rails_com/common.js +3 -0
  8. data/app/assets/javascripts/rails_com/fetch_xhr_script.js +3 -2
  9. data/app/assets/stylesheets/controllers/active_storage_ext/videos/show.css +7 -0
  10. data/app/assets/stylesheets/controllers/active_storage_ext/videos/transfer.css +4 -0
  11. data/app/controllers/active_storage/attachments_controller.rb +13 -0
  12. data/app/controllers/active_storage_ext/videos_controller.rb +25 -0
  13. data/app/controllers/concerns/the_common_api.rb +7 -1
  14. data/app/helpers/rails_com/active_helper.rb +14 -10
  15. data/app/helpers/rails_com/assets_helper.rb +40 -14
  16. data/app/helpers/rails_com/format_helper.rb +37 -0
  17. data/app/models/state_machine.rb +10 -6
  18. data/app/views/active_storage/attachments/_image_item.html.erb +6 -0
  19. data/app/views/active_storage/attachments/_list.html.erb +28 -0
  20. data/app/views/active_storage/attachments/_list_edit.html.erb +25 -0
  21. data/app/views/active_storage/attachments/_list_form.html.erb +28 -0
  22. data/app/views/active_storage/attachments/_video_item.html.erb +6 -0
  23. data/app/views/active_storage/attachments/destroy.js.erb +1 -0
  24. data/app/views/active_storage_ext/videos/show.html.erb +6 -0
  25. data/app/views/kaminari/_next_page.html.erb +1 -1
  26. data/app/views/kaminari/_prev_page.html.erb +1 -1
  27. data/app/views/shared/_error_messages.html.erb +1 -1
  28. data/config/locales/en.yml +5 -0
  29. data/config/locales/zh.yml +5 -0
  30. data/config/routes.rb +10 -5
  31. data/lib/assets/javascripts/input-attachment.js +83 -22
  32. data/lib/mina/git2.rb +59 -0
  33. data/lib/rails_com.rb +22 -12
  34. data/lib/rails_com/config.rb +3 -1
  35. data/lib/rails_com/core_ext/array.rb +35 -17
  36. data/lib/rails_com/core_ext/date.rb +24 -0
  37. data/lib/rails_com/core_ext/numeric.rb +11 -0
  38. data/lib/rails_com/core_ext/string.rb +15 -0
  39. data/lib/rails_com/engine.rb +23 -11
  40. data/lib/rails_com/{controller_helper.rb → helpers/controller_helper.rb} +0 -20
  41. data/lib/rails_com/{model_helper.rb → helpers/model_helper.rb} +3 -3
  42. data/lib/rails_com/{controllers.rb → meta/controllers.rb} +0 -0
  43. data/lib/rails_com/{models.rb → meta/models.rb} +1 -0
  44. data/lib/rails_com/{routes.rb → meta/routes.rb} +9 -0
  45. data/lib/rails_com/rails_ext/activestorage_attached.rb +5 -5
  46. data/lib/rails_com/rails_ext/attachment_transfer.rb +40 -0
  47. data/lib/rails_com/rails_ext/named_base.rb +9 -0
  48. data/lib/rails_com/rails_ext/template_renderer.rb +18 -1
  49. data/lib/rails_com/rails_ext/video_response.rb +28 -0
  50. data/lib/{utils → rails_com/utils}/ip_helper.rb +0 -0
  51. data/lib/rails_com/{helpers → utils}/jobber.rb +0 -0
  52. data/lib/rails_com/utils/num_helper.rb +45 -0
  53. data/lib/rails_com/{setting.rb → utils/setting.rb} +0 -0
  54. data/lib/rails_com/utils/time_helper.rb +58 -0
  55. data/lib/rails_com/{helpers → utils}/uid_helper.rb +0 -0
  56. data/lib/rails_com/version.rb +1 -1
  57. data/lib/templates/erb/scaffold/_form.html.erb.tt +2 -2
  58. data/lib/templates/erb/scaffold/index.html.erb.tt +21 -13
  59. data/lib/templates/erb/scaffold/show.html.erb.tt +14 -5
  60. data/lib/templates/rails/scaffold_controller/api_controller.rb.tt +48 -0
  61. data/lib/templates/rails/scaffold_controller/controller.rb.tt +16 -15
  62. metadata +45 -35
  63. data/MIT-LICENSE +0 -20
  64. data/app/controllers/common_controller.rb +0 -10
  65. data/app/controllers/the_guards_controller.rb +0 -18
  66. data/app/helpers/rails_com/common_helper.rb +0 -13
  67. data/app/views/the_guards/index.html.erb +0 -18
  68. data/lib/utils/time_helper.rb +0 -37
data/lib/mina/git2.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'mina/default'
2
+
3
+ set :branch, 'master'
4
+ set :remove_git_dir, true
5
+ set :remote, 'origin'
6
+ set :git_not_pushed_message, -> { "Your branch #{fetch(:branch)} needs to be pushed to #{fetch(:remote)} before deploying" }
7
+
8
+ namespace :git do
9
+ desc 'Clones the Git repository to the current path.'
10
+ task :clone do
11
+ ensure!(:repository)
12
+ ensure!(:deploy_to)
13
+ if set?(:commit)
14
+ comment %{Using git commit \\"#{fetch(:commit)}\\"}
15
+ command %{git clone "#{fetch(:repository)}" . --recursive}
16
+ command %{git checkout -b current_release "#{fetch(:commit)}" --force}
17
+ else
18
+ command %{
19
+ if [ ! -d "#{fetch(:deploy_to)}/scm" ]; then
20
+ echo "-----> Cloning the Git repository"
21
+ #{echo_cmd %[git clone --recurse-submodules --separate-git-dir #{fetch(:deploy_to)}/scm #{fetch(:repository)} #{fetch(:deploy_to)}/repo]}
22
+ else
23
+ echo "-----> Fetching new git commits"
24
+ #{echo_cmd %[(cd #{fetch(:deploy_to)}/repo && git pull --force #{fetch(:repository)} #{fetch(:branch)}:#{fetch(:branch)})]}
25
+ fi &&
26
+ echo "-----> Using git branch '#{fetch(:branch)}'" &&
27
+ #{echo_cmd %[echo 'gitdir: #{fetch(:deploy_to)}/scm' >> .git]}
28
+ #{echo_cmd %[git checkout --force]}
29
+ #{echo_cmd %[git submodule deinit -all]}
30
+ #{echo_cmd %[git submodule update --init]}
31
+ }, quiet: true
32
+ end
33
+ comment %{Using this git commit}
34
+ command %{git rev-parse HEAD > .mina_git_revision}
35
+ command %{git --no-pager log --format="%aN (%h):%n> %s" -n 1}
36
+ if fetch(:remove_git_dir)
37
+ command %{rm -rf .git}
38
+ end
39
+ end
40
+
41
+ desc 'Prints out current revision'
42
+ task :revision do
43
+ ensure!(:deploy_to)
44
+ command %{cat #{fetch(:current_path)}/.mina_git_revision}
45
+ end
46
+
47
+ desc 'Ensures local repository is pushed to remote'
48
+ task :ensure_pushed do
49
+ run :local do
50
+ comment %{Ensuring everything is pushed to git}
51
+ command %{
52
+ if [ $(git log #{fetch(:remote)}/#{fetch(:branch)}..#{fetch(:branch)} | wc -l) -ne 0 ]; then
53
+ echo "! #{fetch(:git_not_pushed_message)}"
54
+ exit 1
55
+ fi
56
+ }
57
+ end
58
+ end
59
+ end
data/lib/rails_com.rb CHANGED
@@ -1,27 +1,37 @@
1
1
  require 'rails_com/version'
2
+ require 'rails_com/engine'
2
3
  require 'rails_com/config'
3
4
 
4
- require 'rails_com/routes'
5
- require 'rails_com/models'
6
- require 'rails_com/controllers'
7
- require 'rails_com/model_helper'
8
- require 'rails_com/controller_helper'
9
- require 'rails_com/sprockets/non_digest_assets'
5
+ # Meta for Rails
6
+ require 'rails_com/meta/routes'
7
+ require 'rails_com/meta/models'
8
+ require 'rails_com/meta/controllers'
10
9
 
11
- require 'rails_com/helpers/uid_helper'
12
- require 'rails_com/helpers/jobber'
10
+ # Rails Helper
11
+ require 'rails_com/helpers/model_helper'
12
+ require 'rails_com/helpers/controller_helper'
13
13
 
14
+ # Ruby core extension
14
15
  require 'rails_com/core_ext/hash'
15
16
  require 'rails_com/core_ext/nil'
16
17
  require 'rails_com/core_ext/array'
18
+ require 'rails_com/core_ext/date'
19
+ require 'rails_com/core_ext/numeric'
20
+ require 'rails_com/core_ext/string'
17
21
 
22
+ # Rails extension
18
23
  require 'rails_com/rails_ext/template_renderer'
19
24
  require 'rails_com/rails_ext/scaffold_generator'
25
+ require 'rails_com/rails_ext/named_base'
20
26
  require 'rails_com/rails_ext/activestorage_attached'
21
27
  require 'rails_com/rails_ext/persistence_sneakily'
22
28
  require 'rails_com/rails_ext/translation_helper'
29
+ require 'rails_com/rails_ext/video_response'
30
+ require 'rails_com/rails_ext/attachment_transfer'
31
+ require 'rails_com/sprockets/non_digest_assets'
23
32
 
24
- require 'utils/time_helper'
25
-
26
- require 'rails_com/engine'
27
-
33
+ # Utils
34
+ require 'rails_com/utils/time_helper'
35
+ require 'rails_com/utils/num_helper'
36
+ require 'rails_com/utils/uid_helper'
37
+ require 'rails_com/utils/jobber'
@@ -1,11 +1,13 @@
1
+ require 'active_support/configurable'
2
+
1
3
  module RailsCom
2
4
  include ActiveSupport::Configurable
3
- config_accessor :access_denied_method, :default_admin_emails
4
5
 
5
6
  configure do |config|
6
7
  config.ignore_controllers = [
7
8
  'rails/welcome'
8
9
  ]
10
+ config.app_class = 'ApplicationController'
9
11
  end
10
12
 
11
13
  end
@@ -1,42 +1,48 @@
1
1
  class Array
2
2
 
3
- # arr = [1, 2, 3]
4
- # arr.rjust! 5, nil
5
- # => [1, 2, 3, nil, nil]
3
+ # fill an array with the given elements right;
4
+ # arr = [1, 2, 3]
5
+ # arr.rjust! 5, nil
6
+ # # => [1, 2, 3, nil, nil]
6
7
  def rjust!(n, x)
7
8
  return self if n < length
8
9
  insert(0, *Array.new([0, n-length].max, x))
9
10
  end
10
11
 
11
- # arr = [1, 2, 3]
12
- # arr.ljust! 5, nil
13
- # => [nil, nil, 1, 2, 3]
12
+ ##
13
+ # arr = [1, 2, 3]
14
+ # arr.ljust! 5, nil
15
+ # # => [nil, nil, 1, 2, 3]
14
16
  def ljust!(n, x)
15
17
  return self if n < length
16
18
  fill(x, length...n)
17
19
  end
18
20
 
19
- # arr = [1, 2, 3]
20
- # arr.mjust!(5, nil)
21
- # => [1, 2, nil, nil, 3]
21
+ ##
22
+ # arr = [1, 2, 3]
23
+ # arr.mjust!(5, nil)
24
+ # # => [1, 2, nil, nil, 3]
22
25
  def mjust!(n, x)
23
26
  return self if n < length
24
27
  insert (length / 2.0).ceil, *Array.new(n - length, x)
25
28
  end
26
29
 
27
- # raw_data = [
28
- # { a: 1 },
29
- # { a: 2 },
30
- # { b: 2 }
31
- # ]
32
- # raw_data.to_combined_hash
33
- # => { a: [1, 2], b: 2 }
30
+ ##
31
+ # combine the same key hash like array
32
+ # raw_data = [
33
+ # { a: 1 },
34
+ # { a: 2 },
35
+ # { b: 2 }
36
+ # ]
37
+ # raw_data.to_combined_hash
38
+ # #=> { a: [1, 2], b: 2 }
34
39
  def to_combined_hash
35
40
  self.reduce({}) do |memo, index|
36
41
  memo.merge(index) { |_, value, default| [value, default] }
37
42
  end
38
43
  end
39
44
 
45
+ #
40
46
  # raw_data = [
41
47
  # [:a, 1],
42
48
  # [:a, 2],
@@ -44,11 +50,23 @@ class Array
44
50
  # ]
45
51
  # raw_data.to_combined_h
46
52
  # => { a: [1, 2], b: 2 }
47
- # todo nested array bug
53
+ # #todo nested array bug
48
54
  def to_combined_h
49
55
  hash = {}
50
56
  self.each { |x, y| hash[x] = hash[x] ? Array(hash[x]) << y : y }
51
57
  hash
52
58
  end
53
59
 
60
+ # 2D array to csv file
61
+ # data = [
62
+ # [1, 2],
63
+ # [3, 4]
64
+ # ]
65
+ # data.to_csv_file
66
+ def to_csv_file(file = 'export.csv')
67
+ CSV.open(file, 'w') do |csv|
68
+ self.each { |ar| csv << ar }
69
+ end
70
+ end
71
+
54
72
  end
@@ -0,0 +1,24 @@
1
+ class Date
2
+
3
+ # '2018-01-01'.to_date.after(2.month) => '2018-02-31'
4
+ # '2018-01-31'.to_date.after(1.month) => '2018-03-02'
5
+ def after(other)
6
+ if ActiveSupport::Duration === other
7
+ if other.parts.keys == [:months]
8
+ date = self + other
9
+ if date.day == self.day
10
+ r = date - 1.day
11
+ else
12
+ r = date + (self.day - date.day - 1).days
13
+ end
14
+
15
+ r
16
+ else
17
+ self + other
18
+ end
19
+ else
20
+ self + other
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,11 @@
1
+ class Numeric
2
+
3
+ def half_round
4
+ (self * 2).round / 2.0
5
+ end
6
+
7
+ def half_floor
8
+ (self * 2).floor / 2.0
9
+ end
10
+
11
+ end
@@ -0,0 +1,15 @@
1
+ class String
2
+
3
+ def integer?
4
+ Integer(self).is_a?(Integer)
5
+ rescue ArgumentError, TypeError
6
+ false
7
+ end
8
+
9
+ def numeric?
10
+ Float(self).is_a?(Numeric)
11
+ rescue ArgumentError, TypeError
12
+ false
13
+ end
14
+
15
+ end
@@ -1,17 +1,29 @@
1
- module RailsCom
2
- class Engine < ::Rails::Engine
1
+ class RailsCom::Engine < ::Rails::Engine
3
2
 
4
- initializer 'rails_com.assets.precompile' do |app|
5
- app.config.assets.precompile += ['rails_com_manifest.js']
6
- end
3
+ config.generators do |g|
4
+ g.stylesheets false
5
+ g.javasricpts false
6
+ g.javascript_engine false
7
+ g.helper false
8
+ g.jbuilder false
9
+ g.templates.unshift File.expand_path('lib/templates', root)
10
+ end
11
+
12
+ initializer 'rails_com.assets.precompile' do |app|
13
+ app.config.assets.precompile += ['rails_com_manifest.js']
14
+ end
7
15
 
8
- initializer 'rails_com.add_generator_templates' do |app|
9
- app.config.paths['lib/templates'].push File.expand_path('lib/templates', root)
10
- end
16
+ initializer 'rails_com.add_generator_templates' do |app|
17
+ app.config.paths['lib/templates'].unshift File.expand_path('lib/templates', root)
18
+ end
11
19
 
12
- initializer 'rails_com.add_assets_templates' do |app|
13
- app.config.assets.paths.push *Dir[File.expand_path('lib/nondigest_assets/*', root)]
14
- end
20
+ initializer 'rails_com.add_assets_templates' do |app|
21
+ app.config.assets.paths.push(*Dir[File.expand_path('lib/nondigest_assets/*', root)])
22
+ end
15
23
 
24
+ initializer 'rails_com.add_activestorage' do |app|
25
+ ActiveStorage::DiskController.include VideoResponse
26
+ ActiveStorage::Attachment.include AttachmentTransfer
16
27
  end
28
+
17
29
  end
@@ -1,25 +1,5 @@
1
1
  module RailsCom::ControllerHelper
2
2
 
3
- def inc_ip_count
4
- Rails.cache.write "access/#{request.remote_ip}", ip_count + 1, expires_in: 60.seconds
5
- end
6
-
7
- def clear_ip_count
8
- Rails.cache.write "access/#{request.remote_ip}", 0, expires_in: 60.seconds
9
- end
10
-
11
- def ip_count
12
- Rails.cache.read("access/#{request.remote_ip}").to_i
13
- end
14
-
15
- def require_recaptcha
16
- inc_ip_count
17
- if ip_count >= 100
18
- session[:back_to] = request.fullpath
19
- redirect_to '/the_guards'
20
- end
21
- end
22
-
23
3
  def set_locale
24
4
  if params[:locale]
25
5
  session[:locale] = params[:locale]
@@ -1,15 +1,15 @@
1
1
  module RailsCom::ModelHelper
2
2
 
3
- def to_factory_girl
3
+ def to_factory_bot
4
4
  require 'rails/generators'
5
- require 'generators/factory_girl/model/model_generator'
5
+ require 'generators/factory_bot/model/model_generator'
6
6
 
7
7
  args = [
8
8
  self.name.underscore
9
9
  ]
10
10
  cols = columns.map { |col| "#{col.name}:#{col.type}" }
11
11
 
12
- generator = FactoryGirl::Generators::ModelGenerator.new(args + cols, destination_root: Rails.root)
12
+ generator = FactoryBot::Generators::ModelGenerator.new(args + cols, destination_root: Rails.root)
13
13
  generator.invoke_all
14
14
  end
15
15
 
@@ -10,6 +10,7 @@ module RailsCom::Models
10
10
  nil
11
11
  end
12
12
  end
13
+
13
14
  models.compact
14
15
  end
15
16
 
@@ -14,6 +14,15 @@ module RailsCom::Routes
14
14
  _controllers - RailsCom.config.ignore_controllers
15
15
  end
16
16
 
17
+ def modules
18
+ controllers.map do |i|
19
+ x = i.split('/')
20
+ if x.size >= 2
21
+ x[0..-2].join('/')
22
+ end
23
+ end.compact.uniq
24
+ end
25
+
17
26
  def routes_wrapper
18
27
  return @routes_wrapper if @routes_wrapper.present?
19
28
 
@@ -1,11 +1,10 @@
1
1
  module ActiveStorage
2
2
  class Attached
3
3
 
4
- def url_sync(url, filename = 'tmp_file_' + self.record.id.to_s)
5
- tmp_path = File.expand_path 'tmp/storage_migrate'
6
- file_path = File.join tmp_path, filename.to_s
4
+ def url_sync(url)
5
+ filename = File.basename URI(url).path
7
6
 
8
- File.open(file_path, 'w+') do |file|
7
+ Tempfile.open do |file|
9
8
  file.binmode
10
9
  HTTParty.get(url, stream_body: true) do |fragment|
11
10
  file.write fragment
@@ -14,8 +13,8 @@ module ActiveStorage
14
13
  file.rewind
15
14
  self.attach io: file, filename: filename
16
15
  end
17
- end
18
16
 
17
+ end
19
18
 
20
19
  class One
21
20
 
@@ -29,4 +28,5 @@ module ActiveStorage
29
28
 
30
29
  end
31
30
  end
31
+
32
32
  end
@@ -0,0 +1,40 @@
1
+ require 'active_storage/downloading'
2
+ module AttachmentTransfer
3
+ include ActiveStorage::Downloading
4
+
5
+ def copy
6
+ raise 'Only Support mirror service' unless service.is_a?(ActiveStorage::Service::MirrorService)
7
+ download_blob_to_tempfile do |io|
8
+ checksum = blob.send(:compute_checksum_in_chunks, io)
9
+ service.mirrors.map do |service|
10
+ service.upload key, io.tap(&:rewind), checksum: checksum
11
+ end
12
+ end
13
+ end
14
+
15
+ def transfer_faststart
16
+ attach = self.record.send(self.name)
17
+ r = nil
18
+ download_blob_to_tempfile do |input|
19
+ Tempfile.open([ 'ActiveStorage', self.filename.extension_with_delimiter ], Dir.tmpdir) do |file|
20
+ file.binmode
21
+ argv = [ffmpeg_path, '-i', input.path, '-codec', 'copy', '-movflags', 'faststart', '-f', 'mp4', '-y', file.path]
22
+ system(*argv)
23
+ file.rewind
24
+ r = attach.attach io: file, filename: self.filename.to_s, content_type: 'video/mp4'
25
+ end
26
+ end
27
+ self.purge
28
+
29
+ if attach.is_a?(ActiveStorage::Attached::One)
30
+ r
31
+ else
32
+ r.first
33
+ end
34
+ end
35
+
36
+ def ffmpeg_path
37
+ ActiveStorage.paths[:ffmpeg] || 'ffmpeg'
38
+ end
39
+
40
+ end