Class: Biovision::Components::BaseComponent
- Inherits:
-
Object
- Object
- Biovision::Components::BaseComponent
- Extended by:
- Biovision::Components::Base::ComponentSettings
- Includes:
- Biovision::Components::Base::ComponentParameters, Biovision::Components::Base::ComponentPrivileges, Biovision::Components::Base::EntityLinks, Biovision::Components::Base::ImageHandling
- Defined in:
- app/lib/biovision/components/base_component.rb
Overview
Base biovision component
Direct Known Subclasses
ContactComponent, ContentComponent, TrackComponent, UsersComponent
Instance Attribute Summary collapse
-
#component ⇒ Object
readonly
Returns the value of attribute component.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
-
.[](user = nil) ⇒ Object
Receive component-specific handler by class name for component.
- .active? ⇒ Boolean
- .create ⇒ Object
-
.dependent_models ⇒ Object
Model list for automatic role creation.
- .form_options(entity, scope = :admin, options = {}) ⇒ Object
-
.handler(input, user = nil) ⇒ BaseComponent
Receive component-specific handler by component slug.
- .handler_class(slug) ⇒ Object
- .slug ⇒ Object
Instance Method Summary collapse
- #data_value(key, default_value = '') ⇒ Object
- #find_or_create_code(user, code_type, quantity = 1) ⇒ Object
-
#initialize(component, user = nil) ⇒ BaseComponent
constructor
A new instance of BaseComponent.
- #new_entity(model_class, parameters) ⇒ Object
- #register_metric(name, quantity = 1) ⇒ Object
- #settings ⇒ Object
- #settings=(data) ⇒ Object
- #update_data_value(key, new_value) ⇒ Object
- #update_entity(entity, new_attributes) ⇒ Object
- #use_settings? ⇒ Boolean
Methods included from Biovision::Components::Base::ComponentSettings
default_settings, normalize_settings, reset_settings, settings_flags, settings_numbers, settings_strings
Methods included from Biovision::Components::Base::ImageHandling
Methods included from Biovision::Components::Base::EntityLinks
#entity_link, #rest_entity_link, #text_for_link
Methods included from Biovision::Components::Base::ComponentParameters
#[], #[]=, #manage_settings?, #receive, #use_files?, #use_parameters?
Methods included from Biovision::Components::Base::ComponentPrivileges
#administrative_parts, #create_roles, #crud_table_names, #model_from_context, #owner?, #permit?, #role?, #role_tree
Constructor Details
#initialize(component, user = nil) ⇒ BaseComponent
Returns a new instance of BaseComponent.
18 19 20 21 22 23 |
# File 'app/lib/biovision/components/base_component.rb', line 18 def initialize(component, user = nil) @component = component @slug = component&.slug || 'base' self.user = user @name = I18n.t("biovision.components.#{slug}.name", default: slug) end |
Instance Attribute Details
#component ⇒ Object (readonly)
Returns the value of attribute component.
13 14 15 |
# File 'app/lib/biovision/components/base_component.rb', line 13 def component @component end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'app/lib/biovision/components/base_component.rb', line 13 def name @name end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
13 14 15 |
# File 'app/lib/biovision/components/base_component.rb', line 13 def slug @slug end |
#user ⇒ Object
Returns the value of attribute user.
14 15 16 |
# File 'app/lib/biovision/components/base_component.rb', line 14 def user @user end |
Class Method Details
.[](user = nil) ⇒ Object
Receive component-specific handler by class name for component.
48 49 50 |
# File 'app/lib/biovision/components/base_component.rb', line 48 def self.[](user = nil) new(BiovisionComponent[slug], user) end |
.active? ⇒ Boolean
39 40 41 |
# File 'app/lib/biovision/components/base_component.rb', line 39 def self.active? BiovisionComponent[slug]&.active? end |
.create ⇒ Object
63 64 65 |
# File 'app/lib/biovision/components/base_component.rb', line 63 def self.create BiovisionComponent.create(slug: slug, settings: default_settings) end |
.dependent_models ⇒ Object
Model list for automatic role creation
59 60 61 |
# File 'app/lib/biovision/components/base_component.rb', line 59 def self.dependent_models [] end |
.form_options(entity, scope = :admin, options = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'app/lib/biovision/components/base_component.rb', line 70 def self.(entity, scope = :admin, = {}) table_name = entity.class.table_name prefix = scope.nil? ? '' : "/#{scope}" { model: scope.nil? ? entity : [scope, entity], id: "#{entity.class.to_s.underscore}-form", data: { check_url: "#{prefix}/#{table_name}/check" } }.merge() end |
.handler(input, user = nil) ⇒ BaseComponent
Receive component-specific handler by component slug
30 31 32 33 |
# File 'app/lib/biovision/components/base_component.rb', line 30 def self.handler(input, user = nil) type = input.is_a?(String) ? input : input&.slug handler_class(type)[user] end |
.handler_class(slug) ⇒ Object
53 54 55 56 |
# File 'app/lib/biovision/components/base_component.rb', line 53 def self.handler_class(slug) handler_name = "biovision/components/#{slug}_component".classify handler_name.safe_constantize || BaseComponent end |
.slug ⇒ Object
35 36 37 |
# File 'app/lib/biovision/components/base_component.rb', line 35 def self.slug to_s.demodulize.to_s.underscore.gsub('_component', '') end |
Instance Method Details
#data_value(key, default_value = '') ⇒ Object
119 120 121 122 |
# File 'app/lib/biovision/components/base_component.rb', line 119 def data_value(key, default_value = '') data = user.component_data(slug) data.key?(key.to_s) ? data[key.to_s] : default_value end |
#find_or_create_code(user, code_type, quantity = 1) ⇒ Object
113 114 115 |
# File 'app/lib/biovision/components/base_component.rb', line 113 def find_or_create_code(user, code_type, quantity = 1) @component.find_or_create_code(user, code_type, quantity) end |
#new_entity(model_class, parameters) ⇒ Object
140 141 142 |
# File 'app/lib/biovision/components/base_component.rb', line 140 def new_entity(model_class, parameters) model_class.new(parameters) end |
#register_metric(name, quantity = 1) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/lib/biovision/components/base_component.rb', line 96 def register_metric(name, quantity = 1) metric = Metric[name] if metric.nil? attributes = { biovision_component: @component, name: name, incremental: !name.end_with?('.hit') } metric = Metric.create(attributes) end metric << quantity end |
#settings ⇒ Object
90 91 92 |
# File 'app/lib/biovision/components/base_component.rb', line 90 def settings @component.settings end |
#settings=(data) ⇒ Object
85 86 87 88 |
# File 'app/lib/biovision/components/base_component.rb', line 85 def settings=(data) @component.settings.merge!(self.class.normalize_settings(data)) @component.save! end |
#update_data_value(key, new_value) ⇒ Object
126 127 128 129 130 |
# File 'app/lib/biovision/components/base_component.rb', line 126 def update_data_value(key, new_value) data = user.component_data(slug) data[key.to_s] = new_value user.new_component_data(data) end |
#update_entity(entity, new_attributes) ⇒ Object
134 135 136 |
# File 'app/lib/biovision/components/base_component.rb', line 134 def update_entity(entity, new_attributes) entity.update(new_attributes) end |
#use_settings? ⇒ Boolean
80 81 82 |
# File 'app/lib/biovision/components/base_component.rb', line 80 def use_settings? use_parameters? || @component.settings.any? end |