Class: Biovision::Components::BaseComponent
- Inherits:
-
Object
- Object
- Biovision::Components::BaseComponent
- Defined in:
- app/services/biovision/components/base_component.rb
Overview
Base biovision component
Direct Known Subclasses
ContactComponent, ContentComponent, RegistrationComponent, 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
- .allow?(user, options = {}) ⇒ Boolean
- .default_privilege_name ⇒ Object
-
.handler(input, user = nil) ⇒ BaseComponent
Receive component-specific handler by component slug.
- .handler_class(slug) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ String
Receive parameter value or nil.
-
#[]=(key, value) ⇒ Object
Set parameter.
- #allow?(options = {}) ⇒ Boolean
-
#initialize(component, user = nil) ⇒ BaseComponent
constructor
A new instance of BaseComponent.
-
#receive(key, default = '') ⇒ String
Receive parameter value with default.
- #register_metric(name, quantity = 1) ⇒ Object
- #settings ⇒ Object
- #settings=(data) ⇒ Object
- #use_parameters? ⇒ Boolean
Constructor Details
#initialize(component, user = nil) ⇒ BaseComponent
Returns a new instance of BaseComponent.
11 12 13 14 15 16 17 |
# File 'app/services/biovision/components/base_component.rb', line 11 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.
7 8 9 |
# File 'app/services/biovision/components/base_component.rb', line 7 def component @component end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'app/services/biovision/components/base_component.rb', line 7 def name @name end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
7 8 9 |
# File 'app/services/biovision/components/base_component.rb', line 7 def slug @slug end |
#user ⇒ Object
Returns the value of attribute user.
7 8 9 |
# File 'app/services/biovision/components/base_component.rb', line 7 def user @user end |
Class Method Details
.allow?(user, options = {}) ⇒ Boolean
45 46 47 48 49 50 51 |
# File 'app/services/biovision/components/base_component.rb', line 45 def self.allow?(user, = {}) return false if user.nil? privilege = [:privilege] || default_privilege_name UserPrivilege.user_has_privilege?(user, privilege) end |
.default_privilege_name ⇒ Object
39 40 41 |
# File 'app/services/biovision/components/base_component.rb', line 39 def self.default_privilege_name self.class.to_s.demodulize.underscore.gsub('component', 'manager') end |
.handler(input, user = nil) ⇒ BaseComponent
Receive component-specific handler by component slug
24 25 26 27 28 29 30 31 |
# File 'app/services/biovision/components/base_component.rb', line 24 def self.handler(input, user = nil) if input.is_a?(BiovisionComponent) handler_class(input.slug).new(input, user) else entity = BiovisionComponent.find_by!(slug: input) handler_class(input).new(entity, user) end end |
.handler_class(slug) ⇒ Object
34 35 36 37 |
# File 'app/services/biovision/components/base_component.rb', line 34 def self.handler_class(slug) handler_name = "biovision/components/#{slug}_component".classify handler_name.safe_constantize || BaseComponent end |
Instance Method Details
#[](key) ⇒ String
Receive parameter value or nil
Returns value of component’s parameter of nil when it’s not found
109 110 111 |
# File 'app/services/biovision/components/base_component.rb', line 109 def [](key) @component.get(key) end |
#[]=(key, value) ⇒ Object
Set parameter
117 118 119 |
# File 'app/services/biovision/components/base_component.rb', line 117 def []=(key, value) @component[key] = value unless key.blank? end |
#allow?(options = {}) ⇒ Boolean
70 71 72 73 74 75 76 77 78 79 |
# File 'app/services/biovision/components/base_component.rb', line 70 def allow?( = {}) return false if user.nil? return true if user.super_user? || @role&.administrator? if .key?(:action) @role.data[[:action].to_s] else !@role.nil? end end |
#receive(key, default = '') ⇒ String
Receive parameter value with default
Returns value of component’s parameter or default value when it’s not found
99 100 101 |
# File 'app/services/biovision/components/base_component.rb', line 99 def receive(key, default = '') @component.get(key, default) end |
#register_metric(name, quantity = 1) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/services/biovision/components/base_component.rb', line 123 def register_metric(name, quantity = 1) metric = Metric.find_by(name: 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
87 88 89 |
# File 'app/services/biovision/components/base_component.rb', line 87 def settings @component.settings end |
#settings=(data) ⇒ Object
82 83 84 85 |
# File 'app/services/biovision/components/base_component.rb', line 82 def settings=(data) @component.settings.merge!(normalize_settings(data)) @component.save! end |
#use_parameters? ⇒ Boolean
65 66 67 |
# File 'app/services/biovision/components/base_component.rb', line 65 def use_parameters? true end |