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.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
- .allow?(user, privilege_name = '') ⇒ Boolean
-
.handler(input, user = nil) ⇒ BaseComponent
Receive component-specific handler by component slug.
- .handler_class(slug) ⇒ Object
-
.privilege_names ⇒ Object
Privilege names for using in biovision_component_user.data.
Instance Method Summary collapse
-
#[](key) ⇒ String
Receive parameter value or nil.
-
#[]=(key, value) ⇒ Object
Set parameter.
- #administrator? ⇒ Boolean
- #allow?(*privilege_names) ⇒ Boolean
-
#initialize(component, user = nil) ⇒ BaseComponent
constructor
A new instance of BaseComponent.
- #privilege?(privilege_name) ⇒ Boolean
-
#receive(key, default = '') ⇒ String
Receive parameter value with default.
- #register_metric(name, quantity = 1) ⇒ Object
- #settings ⇒ Object
- #settings=(data) ⇒ Object
- #update_privileges(user, data = nil) ⇒ 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 |
#role ⇒ Object (readonly)
Returns the value of attribute role.
7 8 9 |
# File 'app/services/biovision/components/base_component.rb', line 7 def role @role 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, privilege_name = '') ⇒ Boolean
46 47 48 49 50 51 52 53 54 |
# File 'app/services/biovision/components/base_component.rb', line 46 def self.allow?(user, privilege_name = '') return false if user.nil? return true if user.super_user? slug = self.class.to_s.demodulize.underscore.gsub('component', '') component = BiovisionComponent.find_by(slug: slug) self.class.new(component, user).allow?(privilege_name) 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 |
.privilege_names ⇒ Object
Privilege names for using in biovision_component_user.data
40 41 42 |
# File 'app/services/biovision/components/base_component.rb', line 40 def self.privilege_names [] end |
Instance Method Details
#[](key) ⇒ String
Receive parameter value or nil
Returns value of component’s parameter of nil when it’s not found
123 124 125 |
# File 'app/services/biovision/components/base_component.rb', line 123 def [](key) @component.get(key) end |
#[]=(key, value) ⇒ Object
Set parameter
131 132 133 |
# File 'app/services/biovision/components/base_component.rb', line 131 def []=(key, value) @component[key] = value unless key.blank? end |
#administrator? ⇒ Boolean
72 73 74 75 76 |
# File 'app/services/biovision/components/base_component.rb', line 72 def administrator? return false if user.nil? user.super_user? || @role&.administrator? end |
#allow?(*privilege_names) ⇒ Boolean
79 80 81 82 83 84 85 86 |
# File 'app/services/biovision/components/base_component.rb', line 79 def allow?(*privilege_names) return false if user.nil? return true if administrator? || privilege_names.blank? return false if @role.nil? privilege_names.flatten.each { |slug| return true if privilege?(slug) } false end |
#privilege?(privilege_name) ⇒ Boolean
89 90 91 92 93 |
# File 'app/services/biovision/components/base_component.rb', line 89 def privilege?(privilege_name) return false if @role.nil? @role.data[privilege_name.to_s] end |
#receive(key, default = '') ⇒ String
Receive parameter value with default
Returns value of component’s parameter or default value when it’s not found
113 114 115 |
# File 'app/services/biovision/components/base_component.rb', line 113 def receive(key, default = '') @component.get(key, default) end |
#register_metric(name, quantity = 1) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/services/biovision/components/base_component.rb', line 137 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
101 102 103 |
# File 'app/services/biovision/components/base_component.rb', line 101 def settings @component.settings end |
#settings=(data) ⇒ Object
96 97 98 99 |
# File 'app/services/biovision/components/base_component.rb', line 96 def settings=(data) @component.settings.merge!(normalize_settings(data)) @component.save! end |
#update_privileges(user, data = nil) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/services/biovision/components/base_component.rb', line 153 def update_privileges(user, data = nil) criteria = { user: user, biovision_component: @component } link = BiovisionComponentUser.find_or_create_by(criteria) link&.update(data: data) unless data.nil? link end |
#use_parameters? ⇒ Boolean
68 69 70 |
# File 'app/services/biovision/components/base_component.rb', line 68 def use_parameters? true end |