biovision 0.4.210512.0 → 0.12.211124.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -5
  3. data/app/assets/stylesheets/biovision/admin/layout.scss +34 -0
  4. data/app/assets/stylesheets/biovision/biovision.scss +56 -26
  5. data/app/assets/stylesheets/biovision/components/filters.scss +0 -2
  6. data/app/assets/stylesheets/biovision/components/forms.scss +40 -10
  7. data/app/assets/stylesheets/biovision/themes/default_theme/layout/header.scss +10 -0
  8. data/app/controllers/admin/biovision_components_controller.rb +10 -0
  9. data/app/controllers/admin/components_controller.rb +9 -5
  10. data/app/controllers/admin/users_controller.rb +2 -1
  11. data/app/controllers/admin_controller.rb +2 -22
  12. data/app/controllers/concerns/component_stories.rb +22 -0
  13. data/app/controllers/concerns/crud_entities.rb +12 -4
  14. data/app/controllers/concerns/my_crud_entities.rb +146 -0
  15. data/app/controllers/concerns/processed_forms.rb +28 -0
  16. data/app/controllers/concerns/restricted_access.rb +37 -0
  17. data/app/controllers/errors_controller.rb +37 -0
  18. data/app/controllers/my/components_controller.rb +21 -0
  19. data/app/controllers/my/index_controller.rb +1 -3
  20. data/app/controllers/my/profiles_controller.rb +2 -0
  21. data/app/controllers/oembed_controller.rb +12 -0
  22. data/app/controllers/profile_controller.rb +2 -0
  23. data/app/controllers/users_controller.rb +2 -0
  24. data/app/helpers/biovision_helper.rb +16 -1
  25. data/app/helpers/entity_helper.rb +77 -0
  26. data/app/helpers/my_helper.rb +34 -0
  27. data/app/lib/biovision/components/base/component_parameters.rb +6 -3
  28. data/app/lib/biovision/components/base/component_privileges.rb +4 -3
  29. data/app/lib/biovision/components/base/component_stories.rb +30 -0
  30. data/app/lib/biovision/components/base/entity_links.rb +16 -2
  31. data/app/lib/biovision/components/base/image_handling.rb +33 -0
  32. data/app/lib/biovision/components/base_component.rb +6 -27
  33. data/app/lib/biovision/components/content/oembed/receiver.rb +98 -0
  34. data/app/lib/biovision/components/content/oembed/twitter_receiver.rb +20 -0
  35. data/app/lib/biovision/components/content/oembed/vimeo_receiver.rb +20 -0
  36. data/app/lib/biovision/components/content/oembed/youtube_receiver.rb +20 -0
  37. data/app/lib/biovision/components/content_component.rb +14 -3
  38. data/app/lib/biovision/components/users_component.rb +6 -0
  39. data/app/lib/biovision/helpers/data_helper.rb +31 -4
  40. data/app/lib/biovision/helpers/export_helper.rb +97 -0
  41. data/app/lib/biovision/migrations/component_migration.rb +3 -1
  42. data/app/lib/biovision/stories/component_story.rb +55 -0
  43. data/app/models/biovision_component.rb +14 -1
  44. data/app/models/code.rb +3 -3
  45. data/app/models/concerns/checkable.rb +2 -1
  46. data/app/models/concerns/has_uploaded_file.rb +26 -0
  47. data/app/models/concerns/simple_tag.rb +30 -0
  48. data/app/models/concerns/toggleable.rb +2 -1
  49. data/app/models/concerns/tree_structure.rb +2 -0
  50. data/app/models/metric.rb +4 -0
  51. data/app/models/oembed_domain.rb +25 -0
  52. data/app/models/oembed_link.rb +19 -0
  53. data/app/models/oembed_receiver.rb +15 -0
  54. data/app/models/role.rb +4 -12
  55. data/app/models/simple_image.rb +29 -2
  56. data/app/models/simple_image_tag.rb +1 -16
  57. data/app/models/uploaded_file.rb +62 -0
  58. data/app/models/uploaded_file_tag.rb +15 -0
  59. data/app/models/uploaded_file_tag_file.rb +13 -0
  60. data/app/models/user.rb +10 -0
  61. data/app/models/user_role.rb +0 -1
  62. data/app/uploaders/simple_file_uploader.rb +2 -6
  63. data/app/uploaders/simple_image_uploader.rb +3 -20
  64. data/app/uploaders/uploaders/path_slug.rb +22 -0
  65. data/app/views/admin/agents/index.html.erb +1 -1
  66. data/app/views/admin/biovision_components/_nav_item.html.erb +6 -0
  67. data/app/views/admin/biovision_components/entity/_in_list.html.erb +12 -0
  68. data/app/views/admin/biovision_components/index.html.erb +11 -0
  69. data/app/views/admin/components/links/_base.html.erb +1 -1
  70. data/app/views/admin/dynamic_blocks/_form.html.erb +1 -1
  71. data/app/views/admin/dynamic_blocks/entity/_in_list.html.erb +1 -1
  72. data/app/views/admin/dynamic_blocks/index.html.erb +1 -1
  73. data/app/views/admin/dynamic_blocks/show.html.erb +3 -3
  74. data/app/views/admin/dynamic_pages/entity/_in_list.html.erb +1 -1
  75. data/app/views/admin/dynamic_pages/index.html.erb +1 -6
  76. data/app/views/admin/dynamic_pages/show.html.erb +1 -1
  77. data/app/views/admin/index/index.html.erb +1 -1
  78. data/app/views/admin/ip_addresses/index.html.erb +2 -2
  79. data/app/views/admin/navigation_groups/index.html.erb +1 -6
  80. data/app/views/admin/tokens/entity/_in_list.html.erb +1 -1
  81. data/app/views/admin/tokens/index.html.erb +1 -6
  82. data/app/views/admin/tokens/show.html.erb +1 -1
  83. data/app/views/admin/users/entity/_in_list.html.erb +2 -4
  84. data/app/views/admin/users/index.html.erb +1 -1
  85. data/app/views/admin/users/show.html.erb +18 -12
  86. data/app/views/admin/widgets/_filters.html.erb +7 -2
  87. data/app/views/admin/widgets/filters/_text.html.erb +7 -0
  88. data/app/views/errors/error.html.erb +1 -0
  89. data/app/views/layouts/admin/_header.html.erb +7 -2
  90. data/app/views/layouts/application/header/_authentication.html.erb +4 -1
  91. data/app/views/my/components/index.html.erb +25 -0
  92. data/app/views/my/components/show.html.erb +21 -0
  93. data/app/views/my/index/_cards.html.erb +15 -0
  94. data/app/views/my/index/_email.html.erb +14 -0
  95. data/app/views/my/index/_navigation.html.erb +33 -0
  96. data/app/views/my/index/index.html.erb +3 -50
  97. data/app/views/my/profiles/show.html.erb +13 -0
  98. data/app/views/shared/admin/_list.html.erb +10 -19
  99. data/app/views/shared/admin/_list_with_priority.html.erb +10 -19
  100. data/app/views/shared/admin/_priority.html.erb +6 -5
  101. data/app/views/shared/admin/_toggle.html.erb +5 -10
  102. data/app/views/shared/entity/_date_field.html.erb +6 -0
  103. data/app/views/shared/entity/_linked_entity.html.erb +1 -1
  104. data/app/views/shared/entity/_list.html.erb +22 -0
  105. data/app/views/shared/entity/_list_with_priority.html.erb +22 -0
  106. data/app/views/shared/entity/_priority_icons.html.erb +8 -0
  107. data/app/views/shared/entity/_toggle.html.erb +12 -0
  108. data/app/views/shared/forms/_field.html.erb +5 -1
  109. data/app/views/shared/forms/_field_with_search.html.erb +17 -0
  110. data/app/views/shared/forms/_meta_texts.html.erb +1 -1
  111. data/app/views/shared/forms/_simple_entity_link.html.erb +14 -0
  112. data/app/views/shared/my/_list.html.erb +10 -19
  113. data/app/views/shared/my/_list_with_priority.html.erb +10 -19
  114. data/app/views/shared/my/entity/edit.html.erb +25 -0
  115. data/app/views/shared/my/entity/new.html.erb +18 -0
  116. data/app/views/simple_images/_simple_image.jbuilder +13 -0
  117. data/config/locales/biovision-ru.yml +4 -0
  118. data/config/locales/components-ru.yml +23 -2
  119. data/config/locales/content-ru.yml +8 -0
  120. data/config/locales/users-ru.yml +4 -1
  121. data/config/routes.rb +21 -0
  122. data/db/migrate/20200224000010_create_users_component.rb +0 -11
  123. data/db/migrate/20200404000000_create_simple_images.rb +1 -0
  124. data/db/migrate/20210405000000_create_acl.rb +0 -1
  125. data/db/migrate/20210421000000_create_content_component.rb +21 -0
  126. data/db/migrate/20210616000000_create_uploaded_files.rb +52 -0
  127. data/db/migrate/amends/20210816060606_create_oembed_receivers.rb +21 -0
  128. data/db/migrate/amends/20210907070707_add_checksum_to_simple_images.rb +13 -0
  129. data/lib/biovision/base_methods.rb +0 -27
  130. data/lib/biovision/version.rb +1 -1
  131. data/lib/tasks/components.rake +51 -0
  132. metadata +53 -4
  133. data/app/models/biovision_component_user.rb +0 -21
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adds method for restricting access
4
+ module RestrictedAccess
5
+ extend ActiveSupport::Concern
6
+
7
+ private
8
+
9
+ # Restrict access for anonymous users
10
+ def restrict_anonymous_access
11
+ return unless current_user.nil?
12
+
13
+ handle_http_401(t('application.errors.restricted_access'))
14
+ end
15
+
16
+ def restrict_access
17
+ user_action = role_end_from_action
18
+ role_name = "#{component_handler.slug}.#{user_action}"
19
+ error = t('admin.errors.unauthorized.missing_role', role: role_name)
20
+
21
+ handle_http_401(error) unless component_handler.permit?(user_action)
22
+ end
23
+
24
+ def role_end_from_action
25
+ role = action_to_role_map.select { |k| k.include?(action_name) }.values.last
26
+ role || 'default'
27
+ end
28
+
29
+ def action_to_role_map
30
+ view = %w[index show search]
31
+ edit = %w[create destroy edit new priority toggle update]
32
+ {
33
+ view => "#{controller_name}.view",
34
+ edit => "#{controller_name}.edit"
35
+ }
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Errors
4
+ class ErrorsController < ApplicationController
5
+ before_action :set_message
6
+
7
+ # get /400
8
+ def bad_request
9
+ render :error
10
+ end
11
+
12
+ # get /401
13
+ def unauthorized
14
+ render :error
15
+ end
16
+
17
+ # get /403
18
+ def forbidden
19
+ render :error
20
+ end
21
+
22
+ # get /422
23
+ def unprocessable_entity
24
+ render :error
25
+ end
26
+
27
+ # get /500
28
+ def internal_server_error
29
+ render :error
30
+ end
31
+
32
+ private
33
+
34
+ def set_message
35
+ @message = t("application.errors.#{action_name}")
36
+ end
37
+ end
@@ -2,4 +2,25 @@
2
2
 
3
3
  # User-side component handling
4
4
  class My::ComponentsController < ProfileController
5
+ before_action :restrict_component_access, except: :index
6
+
7
+ # get /my/components
8
+ def index
9
+ @collection = BiovisionComponent.list_for_user
10
+ end
11
+
12
+ # get /my/components/:slug
13
+ def show
14
+ end
15
+
16
+ private
17
+
18
+ def restrict_component_access
19
+ slug = params[:slug]
20
+ @handler = Biovision::Components::BaseComponent.handler(slug, current_user)
21
+ role_name = "#{@handler.slug}.default"
22
+ error = t('admin.errors.unauthorized.missing_role', role: role_name)
23
+
24
+ handle_http_401(error) unless @handler.permit?(role_name)
25
+ end
5
26
  end
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Main page for user
4
- class My::IndexController < ApplicationController
5
- before_action :restrict_anonymous_access
6
-
4
+ class My::IndexController < ProfileController
7
5
  # get /my
8
6
  def index
9
7
  end
@@ -3,6 +3,8 @@
3
3
  # Managing profile for current user
4
4
  class My::ProfilesController < ApplicationController
5
5
  include Authentication
6
+ include ProcessedForms
7
+ include RestrictedAccess
6
8
 
7
9
  before_action :redirect_authorized_user, only: %i[new create]
8
10
  before_action :restrict_anonymous_access, except: %i[check new create]
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Receiving oembed data by URLs
4
+ class OembedController < ApplicationController
5
+ # get /oembed?url=
6
+ def code
7
+ url = param_from_request(:url)
8
+ receiver = Biovision::Components::Content::Oembed::Receiver[url]
9
+
10
+ render json: { meta: { code: receiver.code } }
11
+ end
12
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  # Controllers for logged-in users only
4
4
  class ProfileController < ApplicationController
5
+ include RestrictedAccess
6
+
5
7
  before_action :restrict_access
6
8
 
7
9
  protected
@@ -2,6 +2,8 @@
2
2
 
3
3
  # Users for visitors
4
4
  class UsersController < ApplicationController
5
+ include RestrictedAccess
6
+
5
7
  before_action :restrict_anonymous_access, only: %i[ban follow unban unfollow]
6
8
  before_action :set_socialization, only: %i[ban follow unban unfollow]
7
9
  before_action :set_entity
@@ -8,7 +8,7 @@ module BiovisionHelper
8
8
  def admin_entity_link(entity, handler: nil, **options)
9
9
  return '∅' if entity.nil?
10
10
 
11
- component = (handler || Biovision::Components::BaseComponent[])
11
+ component = (handler || component_handler)
12
12
  text = options.delete(:text) { component.text_for_link(entity) }
13
13
 
14
14
  if handler.nil? || handler.permit?('view', entity)
@@ -35,6 +35,7 @@ module BiovisionHelper
35
35
  # @param [Hash] options
36
36
  def entity_link(entity, **options)
37
37
  return '' if entity.nil?
38
+ return '' if entity.respond_to?(:visible?) && !entity.visible?
38
39
 
39
40
  handler = Biovision::Components::BaseComponent[]
40
41
  text = options.delete(:text) { handler.text_for_link(entity) }
@@ -53,6 +54,8 @@ module BiovisionHelper
53
54
  # @param [Hash] options
54
55
  def world_icon(path, title = t(:view_as_visitor), options = {})
55
56
  if path.is_a? ApplicationRecord
57
+ return '' if path.respond_to?(:visible?) && !path.visible?
58
+
56
59
  table_name = path.class.table_name
57
60
  path = path.respond_to?(:world_url) ? path.world_url : "/#{table_name}/#{path.id}"
58
61
  end
@@ -152,4 +155,16 @@ module BiovisionHelper
152
155
  def email_link(email, options = {})
153
156
  link_to(email, "mailto:#{email}", options)
154
157
  end
158
+
159
+ # @param [ApplicationRecord] entity
160
+ # @deprecated use #entity_priority_icons
161
+ def admin_priority_icons(entity)
162
+ render(partial: 'shared/admin/priority', locals: { entity: entity })
163
+ end
164
+
165
+ # @param [ApplicationRecord] entity
166
+ # @deprecated use #entity_toggle
167
+ def admin_toggle_block(entity)
168
+ render(partial: 'shared/admin/toggle', locals: { entity: entity })
169
+ end
155
170
  end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Helpers for displaying common entity-related blocks
4
+ module EntityHelper
5
+ # @param [Enumerable] collection
6
+ # @param [Symbol|String|nil] scope
7
+ # @param [Biovision::Components::BaseComponent|nil] handler
8
+ # @param [TrueClass|FalseClass] with_priority
9
+ def entity_list(collection, scope: nil, handler: component_handler, with_priority: false)
10
+ scope = controller.class.module_parent_name.to_s.downcase if scope.nil?
11
+ suffix = with_priority ? '_with_priority' : ''
12
+
13
+ render(
14
+ partial: "shared/entity/list#{suffix}",
15
+ locals: { collection: collection, handler: handler, scope: scope.to_sym }
16
+ )
17
+ end
18
+
19
+ # @param [ApplicationRecord] entity
20
+ # @param [Symbol|String|nil] scope
21
+ def entity_toggle(entity, scope: nil)
22
+ scope = controller.class.module_parent_name.to_s.downcase if scope.nil?
23
+
24
+ render(
25
+ partial: 'shared/entity/toggle',
26
+ locals: { entity: entity, scope: scope.to_sym }
27
+ )
28
+ end
29
+
30
+ # @param [ApplicationRecord] entity
31
+ # @param [Symbol|String|nil] scope
32
+ def entity_priority_icons(entity, scope: nil)
33
+ scope = controller.class.module_parent_name.to_s.downcase if scope.nil?
34
+
35
+ render(
36
+ partial: 'shared/entity/priority_icons',
37
+ locals: { entity: entity, scope: scope.to_sym }
38
+ )
39
+ end
40
+
41
+ # @param [ApplicationRecord] entity
42
+ # @param [String] text
43
+ def linked_entity_block(entity, text: nil)
44
+ return '' if entity.blank?
45
+
46
+ render(
47
+ partial: 'shared/entity/linked_entity',
48
+ locals: { entity: entity, text: text }
49
+ )
50
+ end
51
+
52
+ # @param [ApplicationRecord] entity
53
+ # @param [String|Symbol] types
54
+ def entity_partial_block(entity, *types)
55
+ permitted = %i[
56
+ priority uuid slug timestamps language simple_image meta_texts track
57
+ ]
58
+
59
+ buffer = ''
60
+ types.select { |i| permitted.include?(i.to_sym)}.each do |type|
61
+ buffer += render(partial: "shared/entity/#{type}", locals: { entity: entity })
62
+ end
63
+
64
+ raw buffer
65
+ end
66
+
67
+ def entity_form_block(f, *types)
68
+ permitted = %i[priority entity_flags simple_image]
69
+
70
+ buffer = ''
71
+ types.select { |i| permitted.include?(i.to_sym)}.each do |type|
72
+ buffer += render partial: "shared/forms/#{type}", locals: { f: f }
73
+ end
74
+
75
+ raw buffer
76
+ end
77
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Helper methods for user context
4
+ module MyHelper
5
+ # @param [ApplicationRecord] entity
6
+ # @param [Hash] options
7
+ def my_edit_icon(entity, options = {})
8
+ title = options.delete(:title) { t(:edit) }
9
+ key = entity.respond_to?(:uuid) ? entity.uuid : entity.id
10
+ path = "/my/#{entity.class.table_name}/#{key}/edit"
11
+ my_icon_with_link('biovision/icons/edit.svg', path, title.to_s, options)
12
+ end
13
+
14
+ # @param [ApplicationRecord] entity
15
+ # @param [Hash] options
16
+ def my_destroy_icon(entity, options = {})
17
+ title = options.delete(:title) { t(:edit) }
18
+ key = entity.respond_to?(:uuid) ? entity.uuid : entity.id
19
+ path = "/my/#{entity.class.table_name}/#{key}"
20
+ default = {
21
+ class: 'danger',
22
+ data: { confirm: t(:are_you_sure) },
23
+ method: :delete,
24
+ }
25
+ my_icon_with_link('biovision/icons/destroy.svg', path, title.to_s, default.merge(options))
26
+ end
27
+
28
+ # @param [String|ApplicationRecord] path
29
+ # @param [String] title
30
+ # @param [Hash] options
31
+ def my_icon_with_link(source, path, title = '', options = {})
32
+ link_to(image_tag(source, alt: title), path, options)
33
+ end
34
+ end
@@ -9,7 +9,7 @@ module Biovision
9
9
  false
10
10
  end
11
11
 
12
- def use_images?
12
+ def use_files?
13
13
  false
14
14
  end
15
15
 
@@ -36,7 +36,7 @@ module Biovision
36
36
  # @param [String] key
37
37
  # @return [String]
38
38
  def [](key)
39
- @component.get(key)
39
+ @component.get(key, nil)
40
40
  end
41
41
 
42
42
  # Set parameter
@@ -44,7 +44,10 @@ module Biovision
44
44
  # @param [String] key
45
45
  # @param [String] value
46
46
  def []=(key, value)
47
- @component[key] = value unless key.blank?
47
+ return if key.blank?
48
+
49
+ @component.parameters[key.to_s] = value
50
+ @component.save!
48
51
  end
49
52
  end
50
53
  end
@@ -14,7 +14,7 @@ module Biovision
14
14
  model = model_from_context(context)
15
15
  parts << model.table_name if model.respond_to?(:table_name)
16
16
  parts << action
17
- owner?(context) || role?(parts.join('.'))
17
+ owner?(context) || role?(parts.join('.')) || role?("#{slug}.all")
18
18
  end
19
19
 
20
20
  # @param [ApplicationRecord|nil] entity
@@ -42,13 +42,14 @@ module Biovision
42
42
  end
43
43
 
44
44
  def crud_table_names
45
- tables = self.class.dependent_models.map(&:table_name)
45
+ tables = administrative_parts
46
46
  tables << 'simple_images' if use_images?
47
+ tables << 'uploaded_files' if use_files?
47
48
  tables
48
49
  end
49
50
 
50
51
  def role_tree
51
- tree = { nil => %w[default view] }
52
+ tree = { nil => %w[all default view] }
52
53
  tree['settings'] = %w[view edit] if use_settings?
53
54
  crud_table_names.each do |table_name|
54
55
  tree[table_name] = %w[view edit]
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Biovision
4
+ module Components
5
+ module Base
6
+ # Methods for handling component stories
7
+ module ComponentStories
8
+ # @param [String] story_slug
9
+ def story_class(story_slug)
10
+ namespace = "biovision/components/#{self.class.slug}"
11
+ handler_name = "#{namespace}/stories/#{story_slug}_story".classify
12
+ handler_name.safe_constantize || Biovision::Stories::ComponentStory
13
+ end
14
+
15
+ # @param [String] story_slug
16
+ # @param [ApplicationRecord|nil] entity
17
+ def story(story_slug, entity = nil)
18
+ story_class(story_slug).new(self, entity)
19
+ end
20
+
21
+ # @param [String] story_slug
22
+ # @param [Hash] story_parameters
23
+ # @param [ApplicationRecord|String|nli] entity
24
+ def perform_story(story_slug, story_parameters, entity = nil)
25
+ story(story_slug, entity).perform(story_parameters)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -13,10 +13,24 @@ module Biovision
13
13
  # @param [ApplicationRecord] entity
14
14
  # @param [Symbol|String] scope
15
15
  def entity_link(entity, scope = '')
16
- default = "#{scope}/#{entity.class.table_name}/#{entity.id}"
17
16
  prefix = %i[admin my].include?(scope.to_sym) ? scope : 'world'
18
17
  message = "#{prefix}_url".to_sym
19
- entity.respond_to?(message) ? entity.send(message) : "/#{default}"
18
+ if entity.respond_to?(message)
19
+ entity.send(message)
20
+ else
21
+ rest_entity_link(entity, scope.to_sym)
22
+ end
23
+ end
24
+
25
+ # @param [ApplicationRecord] entity
26
+ # @param [Symbol] scope
27
+ def rest_entity_link(entity, scope)
28
+ collection = "/#{scope}/#{entity.class.table_name}".gsub('//', '/')
29
+ if entity.attributes.key?('uuid') && scope != :admin
30
+ "#{collection}/#{entity.uuid}"
31
+ else
32
+ "#{collection}/#{entity.id}"
33
+ end
20
34
  end
21
35
  end
22
36
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Biovision
4
+ module Components
5
+ module Base
6
+ # Handling simple images
7
+ module ImageHandling
8
+ def use_images?
9
+ false
10
+ end
11
+
12
+ # @param [Hash] parameters
13
+ def upload_image(parameters)
14
+ return if parameters[:image].blank?
15
+
16
+ checksum = Digest::SHA256.file(parameters[:image].path).hexdigest
17
+ SimpleImage[checksum] || create_image(parameters, checksum)
18
+ end
19
+
20
+ private
21
+
22
+ # @param [Hash] parameters
23
+ # @param [String] checksum
24
+ def create_image(parameters, checksum)
25
+ image = component.simple_images.new(parameters)
26
+ image.data[SimpleImage::ORIGINAL_CHECKSUM] = checksum
27
+ image.save
28
+ image
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -8,17 +8,18 @@ module Biovision
8
8
  include Base::ComponentPrivileges
9
9
  include Base::ComponentParameters
10
10
  include Base::EntityLinks
11
+ include Base::ImageHandling
11
12
 
12
- attr_reader :component, :slug, :name, :user, :user_link
13
+ attr_reader :component, :slug, :name
14
+ attr_accessor :user
13
15
 
14
16
  # @param [BiovisionComponent] component
15
17
  # @param [User|nil] user
16
18
  def initialize(component, user = nil)
17
19
  @component = component
18
- @slug = component&.slug || 'base'
20
+ self.slug = component&.slug || 'base'
19
21
  self.user = user
20
-
21
- @name = I18n.t("biovision.components.#{@slug}.name", default: @slug)
22
+ self.name = I18n.t("biovision.components.#{slug}.name", default: slug)
22
23
  end
23
24
 
24
25
  # Receive component-specific handler by component slug
@@ -76,28 +77,6 @@ module Biovision
76
77
  }.merge(options)
77
78
  end
78
79
 
79
- # @param [User] user
80
- def user=(user)
81
- @user = user
82
-
83
- if @user.nil?
84
- @user_link = nil
85
- else
86
- criteria = { biovision_component: @component, user: user }
87
- @user_link = BiovisionComponentUser.find_by(criteria)
88
- end
89
- end
90
-
91
- def user_link!(force_create = false)
92
- if @user_link.nil?
93
- criteria = { biovision_component: @component, user: user }
94
- @user_link = BiovisionComponentUser.new(criteria)
95
- @user_link.save if force_create
96
- end
97
-
98
- @user_link
99
- end
100
-
101
80
  def use_settings?
102
81
  use_parameters? || @component.settings.any?
103
82
  end
@@ -115,7 +94,7 @@ module Biovision
115
94
  # @param [String] name
116
95
  # @param [Integer] quantity
117
96
  def register_metric(name, quantity = 1)
118
- metric = Metric.find_by(name: name)
97
+ metric = Metric[name]
119
98
  if metric.nil?
120
99
  attributes = {
121
100
  biovision_component: @component,
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Receiver for OEmbed-wrapped content
4
+ module Biovision
5
+ module Components
6
+ module Content
7
+ module Oembed
8
+ # Default receiver for embedded media
9
+ class Receiver
10
+ PATTERN = %r{<oembed url="([^"]+)"></oembed>}
11
+
12
+ # @param [String] url
13
+ def initialize(url = '')
14
+ @url = url
15
+ end
16
+
17
+ # @param [String] url
18
+ def self.[](url)
19
+ host = URI.parse(url).host
20
+ slug = OembedDomain[host]&.receiver_slug
21
+ receiver_path = "biovision/components/content/oembed/#{slug}_receiver"
22
+ receiver_class = receiver_path.classify.safe_constantize
23
+
24
+ receiver_class.nil? ? new(url) : receiver_class.new(url)
25
+ end
26
+
27
+ # @param [String] text
28
+ def self.convert(text)
29
+ text.gsub(PATTERN) do |fragment|
30
+ url = fragment.match(PATTERN)[1].to_s
31
+ return '' if url.blank?
32
+
33
+ receiver = self[url]
34
+ receiver.code
35
+ end
36
+ end
37
+
38
+ def self.slug
39
+ to_s.demodulize.to_s.underscore.gsub('_receiver', '')
40
+ end
41
+
42
+ def self.domains
43
+ %w[]
44
+ end
45
+
46
+ def self.seed
47
+ receiver_entity = OembedReceiver.find_or_create_by(slug: slug)
48
+ domains.each do |domain|
49
+ receiver_entity.oembed_domains.create(name: domain)
50
+ end
51
+ end
52
+
53
+ def code
54
+ @link = OembedLink[@url]
55
+ @link.code || receive_and_update
56
+ end
57
+
58
+ def fallback
59
+ attributes = %(rel="external nofollow noreferrer" target="_blank")
60
+ %(<a href="#{@url}" #{attributes}>#{URI.parse(@url).host}</a>)
61
+ end
62
+
63
+ private
64
+
65
+ def receive_and_update
66
+ code = receive(code_url)
67
+ @link.code = code
68
+ @link.save
69
+
70
+ code
71
+ end
72
+
73
+ # @param [String] embed_url
74
+ def receive(embed_url)
75
+ response = RestClient.get(embed_url)
76
+ parse(response.body)
77
+ rescue RestClient::Exception => e
78
+ Rails.logger.warn("Cannot receive data for #{embed_url}: #{e}")
79
+ fallback
80
+ end
81
+
82
+ # @param [String] response
83
+ def parse(response)
84
+ json = JSON.parse(response)
85
+ json['html'] || fallback
86
+ rescue JSON::ParserError => e
87
+ Rails.logger.warn("Cannot parse response #{response}: #{e}")
88
+ fallback
89
+ end
90
+
91
+ def code_url
92
+ "https://#{@host}/oembed?url=#{CGI.escape(@url)}&format=json"
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Biovision
4
+ module Components
5
+ module Content
6
+ module Oembed
7
+ # OEmbed receiver for Twitter
8
+ class TwitterReceiver < Receiver
9
+ def code_url
10
+ "https://publish.twitter.com/oembed?url=#{CGI.escape(@url)}"
11
+ end
12
+
13
+ def self.domains
14
+ %w[twitter.com www.twitter.com]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Biovision
4
+ module Components
5
+ module Content
6
+ module Oembed
7
+ # OEmbed receiver for vimeo.com
8
+ class VimeoReceiver < Receiver
9
+ def code_url
10
+ "https://vimeo.com/api/oembed.json?url=#{CGI.escape(@url)}&responsive=true"
11
+ end
12
+
13
+ def self.domains
14
+ %w[www.vimeo.com vimeo.com]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Biovision
4
+ module Components
5
+ module Content
6
+ module Oembed
7
+ # OEmbed receiver for YouTube
8
+ class YoutubeReceiver < Receiver
9
+ def code_url
10
+ "https://www.youtube.com/oembed?url=#{CGI.escape(@url)}&format=json"
11
+ end
12
+
13
+ def self.domains
14
+ %w[www.youtube.com youtube.com youtu.be]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end