rails_com 1.2.4 → 1.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/Rakefile +4 -17
- data/app/assets/images/image-square.png +0 -0
- data/app/assets/javascripts/rails_com/application.js +43 -1
- data/{lib/assets/javascripts/input-attachment.js → app/assets/javascripts/rails_com/attachment.js} +24 -150
- data/app/assets/javascripts/rails_com/picture.js +17 -0
- data/app/controllers/{active_storage → active_storage_ext/admin}/attachments_controller.rb +2 -2
- data/app/controllers/active_storage_ext/admin/base_controller.rb +4 -0
- data/app/controllers/active_storage_ext/admin/blob_defaults_controller.rb +54 -0
- data/app/controllers/active_storage_ext/admin/blobs_controller.rb +48 -0
- data/app/controllers/active_storage_ext/audios_controller.rb +24 -0
- data/app/controllers/concerns/rails_common_api.rb +50 -0
- data/app/controllers/concerns/rails_common_xhr.rb +15 -0
- data/app/helpers/rails_com/active_helper.rb +21 -9
- data/app/helpers/rails_com/assets_helper.rb +57 -23
- data/app/helpers/rails_com/format_helper.rb +2 -2
- data/app/helpers/rails_com/time_helper.rb +2 -1
- data/app/models/active_storage/blob_default.rb +5 -0
- data/app/models/application_record.rb +3 -0
- data/app/models/{state_machine.rb → concerns/state_machine.rb} +0 -0
- data/app/views/{active_storage → active_storage_ext/admin}/attachments/destroy.js.erb +0 -0
- data/app/views/active_storage_ext/admin/blob_defaults/_form.html.erb +7 -0
- data/app/views/active_storage_ext/admin/blob_defaults/_search_form.html.erb +7 -0
- data/app/views/active_storage_ext/admin/blob_defaults/edit.html.erb +9 -0
- data/app/views/active_storage_ext/admin/blob_defaults/index.html.erb +48 -0
- data/app/views/active_storage_ext/admin/blob_defaults/new.html.erb +9 -0
- data/app/views/active_storage_ext/admin/blob_defaults/show.html.erb +10 -0
- data/app/views/active_storage_ext/admin/blobs/_search_form.html.erb +9 -0
- data/app/views/active_storage_ext/admin/blobs/destroy.js.erb +1 -0
- data/app/views/active_storage_ext/admin/blobs/index.html.erb +51 -0
- data/app/views/active_storage_ext/admin/blobs/new.html.erb +13 -0
- data/app/views/active_storage_ext/admin/blobs/show.html.erb +10 -0
- data/app/views/active_storage_ext/attachments/_default_image_item.html.erb +6 -0
- data/app/views/active_storage_ext/attachments/_image_item.html.erb +6 -0
- data/app/views/{active_storage → active_storage_ext}/attachments/_list.html.erb +1 -4
- data/app/views/{active_storage → active_storage_ext}/attachments/_list_edit.html.erb +1 -1
- data/app/views/{active_storage → active_storage_ext}/attachments/_list_form.html.erb +8 -8
- data/app/views/{active_storage → active_storage_ext}/attachments/_video_item.html.erb +0 -0
- data/app/views/active_storage_ext/audios/show.html.erb +5 -0
- data/config/locales/en.yml +2 -1
- data/config/locales/zh.yml +2 -1
- data/config/routes.rb +13 -6
- data/db/migrate/20181012025833_rails_com_init.rb +13 -0
- data/lib/mina/sidekiq.rb +3 -1
- data/lib/rails_com.rb +3 -1
- data/lib/rails_com/config.rb +2 -1
- data/lib/rails_com/core_ext/array.rb +2 -2
- data/lib/rails_com/core_ext/date.rb +3 -2
- data/lib/rails_com/engine.rb +3 -5
- data/lib/rails_com/helpers/model_helper.rb +10 -8
- data/lib/rails_com/rails_ext/activestorage_attached.rb +1 -1
- data/lib/rails_com/rails_ext/attached_macros.rb +21 -0
- data/lib/rails_com/rails_ext/attachment_transfer.rb +3 -1
- data/lib/rails_com/rails_ext/errors.rb +4 -0
- data/lib/rails_com/rails_ext/template_renderer.rb +15 -0
- data/lib/rails_com/utils/babel.rb +38 -0
- data/lib/rails_com/utils/time_helper.rb +9 -4
- data/lib/rails_com/version.rb +2 -2
- data/lib/templates/erb/scaffold/edit.html.erb.tt +2 -2
- data/lib/templates/erb/scaffold/new.html.erb.tt +2 -2
- data/lib/templates/erb/scaffold/show.html.erb.tt +2 -2
- metadata +39 -14
- data/app/assets/javascripts/rails_com/common.js +0 -38
- data/app/controllers/concerns/the_common_api.rb +0 -41
- data/app/views/active_storage/attachments/_image_item.html.erb +0 -6
@@ -0,0 +1,24 @@
|
|
1
|
+
class ActiveStorageExt::AudiosController < RailsCom.config.app_class.constantize
|
2
|
+
before_action :set_video, only: [:show, :transfer]
|
3
|
+
before_action do
|
4
|
+
ActiveStorage::Current.host = request.base_url
|
5
|
+
end
|
6
|
+
|
7
|
+
def show
|
8
|
+
expires_in ActiveStorage::Blob.service.url_expires_in
|
9
|
+
end
|
10
|
+
|
11
|
+
def transfer
|
12
|
+
attached = @attachment.transfer_faststart
|
13
|
+
@video = attached.blob
|
14
|
+
|
15
|
+
flash[:notice] = 'well done!'
|
16
|
+
render 'show'
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def set_video
|
21
|
+
@audio = ActiveStorage::Blob.find(params[:id])
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module RailsCommonApi
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
rescue_from 'StandardError' do |exp|
|
6
|
+
puts nil, exp.full_message(highlight: true, order: :top)
|
7
|
+
render json: { code: 500, error: { class: exp.class.inspect }, message: exp.message }, status: 500
|
8
|
+
end
|
9
|
+
|
10
|
+
rescue_from 'ActiveRecord::RecordNotFound' do |exp|
|
11
|
+
puts nil, exp.full_message(highlight: true, order: :top)
|
12
|
+
render json: { code: 404, error: { class: exp.class.inspect }, message: exp.message }, status: 404
|
13
|
+
end
|
14
|
+
|
15
|
+
rescue_from 'ActionController::ForbiddenError' do |exp|
|
16
|
+
puts nil, exp.full_message(highlight: true, order: :top)
|
17
|
+
render json: { code: 403, error: { class: exp.class.inspect }, message: exp.message }, status: 403
|
18
|
+
end
|
19
|
+
|
20
|
+
rescue_from 'ActionController::UnauthorizedError' do |exp|
|
21
|
+
puts nil, exp.full_message(highlight: true, order: :top)
|
22
|
+
render json: { code: 401, error: { class: exp.class.inspect }, message: exp.message }, status: 401
|
23
|
+
end
|
24
|
+
|
25
|
+
rescue_from 'ActionController::ParameterMissing' do |exp|
|
26
|
+
puts nil, exp.full_message(highlight: true, order: :top)
|
27
|
+
render json: { code: 400, error: { class: exp.class.inspect }, message: exp.message }, status: 400
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def process_errors(model)
|
32
|
+
render json: {
|
33
|
+
code: 406,
|
34
|
+
error: model.errors.as_json(full_messages: true),
|
35
|
+
message: model.errors.full_messages.join("\n")
|
36
|
+
}, status: 200
|
37
|
+
end
|
38
|
+
|
39
|
+
def render *args
|
40
|
+
options = args.extract_options!
|
41
|
+
|
42
|
+
if options[:json]
|
43
|
+
options[:json].merge! code: 200
|
44
|
+
end
|
45
|
+
|
46
|
+
args << options
|
47
|
+
super *args
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -1,7 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module RailsCom::ActiveHelper
|
3
3
|
|
4
|
-
#
|
4
|
+
#
|
5
|
+
# return by assert return
|
6
|
+
# active_assert('notice' == 'notice', expected: 'ui info message', unexpected: 'ui negative message')
|
7
|
+
# #=> 'ui info message'
|
5
8
|
def active_assert(assert, expected: 'item active', unexpected: 'item')
|
6
9
|
if assert
|
7
10
|
expected
|
@@ -10,8 +13,10 @@ module RailsCom::ActiveHelper
|
|
10
13
|
end
|
11
14
|
end
|
12
15
|
|
13
|
-
#
|
14
|
-
|
16
|
+
# return value by each keys which is true
|
17
|
+
# active_asserts(active: true, item: false)
|
18
|
+
# #=> 'active'
|
19
|
+
def active_asserts(join = true, **options)
|
15
20
|
keys = options.select { |_, v| v }.keys
|
16
21
|
|
17
22
|
if join
|
@@ -21,12 +26,17 @@ module RailsCom::ActiveHelper
|
|
21
26
|
end
|
22
27
|
end
|
23
28
|
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
29
|
+
# See examples bellow:
|
30
|
+
# paths:
|
31
|
+
# active_helper paths: '/work/employees' or active_helper paths: ['/work/employees']
|
32
|
+
# controllers:
|
33
|
+
# active_helper controllers: 'xxx' or active_helper controllers: ['xxx1', 'admin/xxx2']
|
34
|
+
# modules:
|
35
|
+
# active_helper modules: 'admin/oa'
|
36
|
+
# action:
|
37
|
+
# active_helper 'work/employee': ['index', 'show']
|
38
|
+
# params:
|
39
|
+
# active_helper controller: 'users', action: 'show', id: 371
|
30
40
|
def active_helper(paths: [], controllers: [], modules: [], active_class: 'item active', item_class: 'item', **options)
|
31
41
|
check_parameters = options.delete(:check_parameters)
|
32
42
|
|
@@ -56,6 +66,8 @@ module RailsCom::ActiveHelper
|
|
56
66
|
item_class
|
57
67
|
end
|
58
68
|
|
69
|
+
# return value by params
|
70
|
+
# active_params state: 'xxx'
|
59
71
|
def active_params(active_class: 'item active', item_class: 'item', **options)
|
60
72
|
options.select { |_, v| v.present? }.each do |k, v|
|
61
73
|
if params[k].to_s == v.to_s
|
@@ -1,42 +1,76 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module RailsCom::AssetsHelper
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
# Assets path: app/assets/javascripts/controllers
|
5
|
+
# Packs Path: app/javascript/packs/javascripts
|
6
|
+
def js_load(**options)
|
7
|
+
ext = ['.js', '.js.erb'] + Array(options.delete(:ext))
|
8
|
+
suffix = options.delete(:suffix)
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
+
asset_filename = "controllers/#{controller_path}/#{action_name}"
|
11
|
+
if suffix
|
12
|
+
asset_filename += ['.', suffix].join
|
10
13
|
end
|
11
|
-
|
12
|
-
|
13
|
-
def css_load(filename = nil, **options)
|
14
|
-
filename ||= "controllers/#{controller_path}/#{action_name}"
|
15
|
-
paths = assets_load_path(filename, relative_path: 'app/assets/stylesheets', ext: ['.css', '.css.erb'])
|
14
|
+
asset_paths = assets_load_path(asset_filename, relative_path: 'app/assets/javascripts', ext: ext)
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
pack_filename ||= "javascripts/#{controller_path}/#{action_name}"
|
17
|
+
if suffix
|
18
|
+
pack_filename += ['-', suffix].join
|
19
19
|
end
|
20
|
-
|
20
|
+
pack_paths = assets_load_path(pack_filename, relative_path: 'app/javascript/packs', ext: ext)
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
r = []
|
23
|
+
if asset_paths.any? { |path| File.exist?(path) }
|
24
|
+
r << javascript_include_tag(asset_filename, options)
|
25
|
+
end
|
25
26
|
|
26
|
-
if
|
27
|
-
javascript_pack_tag
|
27
|
+
if pack_paths.any? { |path| File.exist?(path) }
|
28
|
+
r << javascript_pack_tag(pack_filename, options)
|
28
29
|
end
|
30
|
+
|
31
|
+
r.join("\n ").html_safe
|
29
32
|
end
|
30
33
|
|
31
34
|
def js_ready(**options)
|
32
|
-
js_load(
|
35
|
+
js_load(suffix: 'ready', **options)
|
33
36
|
end
|
34
37
|
|
35
|
-
|
36
|
-
|
38
|
+
# Assets path: app/assets/stylesheets/controllers
|
39
|
+
# Packs Path: app/javascript/packs/stylesheets
|
40
|
+
def css_load(**options)
|
41
|
+
ext = ['.css', '.css.erb'] + Array(options.delete(:ext))
|
42
|
+
|
43
|
+
asset_filename = "controllers/#{controller_path}/#{action_name}"
|
44
|
+
asset_paths = assets_load_path(asset_filename, relative_path: 'app/assets/stylesheets', ext: ext )
|
45
|
+
|
46
|
+
pack_filename = "stylesheets/#{controller_path}/#{action_name}"
|
47
|
+
pack_paths = assets_load_path(pack_filename, relative_path: 'app/javascript/packs', ext: ext)
|
48
|
+
|
49
|
+
r = []
|
50
|
+
if asset_paths.any? { |path| File.exist?(path) }
|
51
|
+
r << stylesheet_link_tag(asset_filename, options)
|
52
|
+
end
|
53
|
+
|
54
|
+
if pack_paths.any? { |path| File.exist?(path) }
|
55
|
+
|
56
|
+
begin
|
57
|
+
rs = stylesheet_pack_tag(pack_filename, options)
|
58
|
+
rescue
|
59
|
+
rs = nil
|
60
|
+
end
|
61
|
+
|
62
|
+
if rs.is_a?(String)
|
63
|
+
r << rs
|
64
|
+
else
|
65
|
+
r << javascript_pack_tag(pack_filename, options)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
r.join("\n ").html_safe
|
37
70
|
end
|
38
71
|
|
39
|
-
|
72
|
+
private
|
73
|
+
def assets_load_path(filename, relative_path:, ext:)
|
40
74
|
paths = []
|
41
75
|
|
42
76
|
file_path = Pathname.new(relative_path).join filename
|
@@ -54,4 +88,4 @@ module RailsCom::AssetsHelper
|
|
54
88
|
paths
|
55
89
|
end
|
56
90
|
|
57
|
-
end
|
91
|
+
end
|
@@ -25,7 +25,7 @@ module RailsCom::FormatHelper
|
|
25
25
|
hash_text = text.to_h
|
26
26
|
return simple_format_hash(hash_text, html_options, options)
|
27
27
|
rescue TypeError
|
28
|
-
return content_tag(:
|
28
|
+
return text.map { |t| content_tag(:p, t, html_options) }.join("\n").html_safe
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -34,4 +34,4 @@ module RailsCom::FormatHelper
|
|
34
34
|
super
|
35
35
|
end
|
36
36
|
|
37
|
-
end
|
37
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,48 @@
|
|
1
|
+
<div class="ui top attached borderless menu">
|
2
|
+
<div class="header item">Blob Defaults</div>
|
3
|
+
<div class="right menu">
|
4
|
+
<div class="item">
|
5
|
+
<%= link_to 'New Blob Default', new_rails_ext_blob_default_path, class: 'ui teal button' %>
|
6
|
+
</div>
|
7
|
+
</div>
|
8
|
+
</div>
|
9
|
+
|
10
|
+
<div class="ui attached segment">
|
11
|
+
<%= render 'search_form' %>
|
12
|
+
</div>
|
13
|
+
|
14
|
+
<table class="ui bottom attached table">
|
15
|
+
<thead>
|
16
|
+
<tr>
|
17
|
+
<th><%= ActiveStorage::BlobDefault.human_attribute_name(:id) %></th>
|
18
|
+
<th><%= ActiveStorage::BlobDefault.human_attribute_name(:record_class) %></th>
|
19
|
+
<th><%= ActiveStorage::BlobDefault.human_attribute_name(:name) %></th>
|
20
|
+
<th><%= ActiveStorage::BlobDefault.human_attribute_name(:file) %></th>
|
21
|
+
<th></th>
|
22
|
+
<th>Actions</th>
|
23
|
+
</tr>
|
24
|
+
</thead>
|
25
|
+
<tbody>
|
26
|
+
<% @blob_defaults.each do |blob_default| %>
|
27
|
+
<tr>
|
28
|
+
<td><%= blob_default.id %></td>
|
29
|
+
<td><%= blob_default.record_class %></td>
|
30
|
+
<td><%= blob_default.name %></td>
|
31
|
+
<td><%= blob_default.file_blob.key if blob_default.file_blob %></td>
|
32
|
+
<td>
|
33
|
+
<%= render 'active_storage_ext/attachments/list', target: blob_default.file %>
|
34
|
+
</td>
|
35
|
+
<td class="ui labels">
|
36
|
+
<%= link_to edit_rails_ext_blob_default_path(blob_default), data: { tooltip: t('.edit') }, class: 'ui pink mini icon button' do %>
|
37
|
+
<i class="pencil alternate icon"></i>
|
38
|
+
<% end %>
|
39
|
+
<%= link_to rails_ext_blob_default_path(blob_default), method: :delete, data: { tooltip: t('.destroy'), confirm: 'Are you sure?' }, class: 'ui red mini icon button' do %>
|
40
|
+
<i class="times icon"></i>
|
41
|
+
<% end %>
|
42
|
+
</td>
|
43
|
+
</tr>
|
44
|
+
<% end %>
|
45
|
+
</tbody>
|
46
|
+
</table>
|
47
|
+
|
48
|
+
<%= paginate @blob_defaults %>
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<div class="ui segment breadcrumb">
|
2
|
+
<%= link_to 'Back', rails_ext_blob_defaults_path(blob_id: params[:blob_id]), class: 'section' %>
|
3
|
+
<div class="divider"> / </div>
|
4
|
+
<div class="active section">Add</div>
|
5
|
+
</div>
|
6
|
+
|
7
|
+
<div class="ui segment">
|
8
|
+
<%= render 'form' %>
|
9
|
+
</div>
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<%= search_form_with do |f| %>
|
2
|
+
<div class="fields">
|
3
|
+
<%= f.text_field :key, placeholder: ActiveStorage::Blob.human_attribute_name(:key) %>
|
4
|
+
<%= f.text_field :filename, placeholder: ActiveStorage::Blob.human_attribute_name(:filename) %>
|
5
|
+
<%= f.submit 'Search' %>
|
6
|
+
</div>
|
7
|
+
<% end %>
|
8
|
+
|
9
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
$('#attachment_<%= @attachment.id %>').remove();
|
@@ -0,0 +1,51 @@
|
|
1
|
+
<div class="ui top attached borderless menu">
|
2
|
+
<%= link_to 'All', rails_ext_blobs_path, class: active_helper(action: 'index') %>
|
3
|
+
<%= link_to 'Unattached', unattached_rails_ext_blobs_path, class: active_helper(action: 'unattached') %>
|
4
|
+
<div class="right menu">
|
5
|
+
<div class="item">
|
6
|
+
<%= link_to 'New ActiveStorage::Blob', new_rails_ext_blob_path, class: 'ui teal button' %>
|
7
|
+
</div>
|
8
|
+
</div>
|
9
|
+
</div>
|
10
|
+
|
11
|
+
<div class="ui attached segment">
|
12
|
+
<%= render 'search_form' %>
|
13
|
+
</div>
|
14
|
+
|
15
|
+
<table class="ui bottom attached table">
|
16
|
+
<thead>
|
17
|
+
<tr>
|
18
|
+
<th><%= ActiveStorage::Blob.human_attribute_name(:id) %></th>
|
19
|
+
<th><%= ActiveStorage::Blob.human_attribute_name(:key) %></th>
|
20
|
+
<th><%= ActiveStorage::Blob.human_attribute_name(:filename) %></th>
|
21
|
+
<th><%= ActiveStorage::Blob.human_attribute_name(:content_type) %></th>
|
22
|
+
<th><%= ActiveStorage::Blob.human_attribute_name(:metadata) %></th>
|
23
|
+
<th><%= ActiveStorage::Blob.human_attribute_name(:size) %></th>
|
24
|
+
<th><%= ActiveStorage::Blob.human_attribute_name(:created_at) %></th>
|
25
|
+
<th>Actions</th>
|
26
|
+
</tr>
|
27
|
+
</thead>
|
28
|
+
<tbody>
|
29
|
+
<% @blobs.each do |blob| %>
|
30
|
+
<tr>
|
31
|
+
<td><%= blob.id %></td>
|
32
|
+
<td><%= blob.key %></td>
|
33
|
+
<td><%= blob.filename %></td>
|
34
|
+
<td><%= blob.content_type %></td>
|
35
|
+
<td><%= simple_format(blob.metadata) %></td>
|
36
|
+
<td><%= number_to_human_size(blob.byte_size) %></td>
|
37
|
+
<td><time><%= blob.created_at.to_s(:rfc822) %></time></td>
|
38
|
+
<td class="ui labels">
|
39
|
+
<%= link_to rails_ext_blob_path(blob), method: :delete, data: { tooltip: t('.destroy'), confirm: 'Are you sure?' }, class: 'ui red mini icon button' do %>
|
40
|
+
<i class="times icon"></i>
|
41
|
+
<% end %>
|
42
|
+
<%= link_to rails_ext_audio_path(blob.id), target: '_blank', method: :delete, data: { tooltip: '默认' }, class: 'ui blue mini icon button' do %>
|
43
|
+
<i class="play icon"></i>
|
44
|
+
<% end %>
|
45
|
+
</td>
|
46
|
+
</tr>
|
47
|
+
<% end %>
|
48
|
+
</tbody>
|
49
|
+
</table>
|
50
|
+
|
51
|
+
<%= paginate @blobs %>
|