Class: Biovision::Components::BaseComponent
Overview
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
default_settings, normalize_settings, reset_settings, settings_flags, settings_numbers, settings_strings
#entity_link, #text_for_link
#[], #[]=, #manage_settings?, #receive, #use_images?, #use_parameters?
#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.
16
17
18
19
20
21
22
|
# File 'app/lib/biovision/components/base_component.rb', line 16
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
Returns the value of attribute component.
12
13
14
|
# File 'app/lib/biovision/components/base_component.rb', line 12
def component
@component
end
|
#name ⇒ Object
Returns the value of attribute name.
12
13
14
|
# File 'app/lib/biovision/components/base_component.rb', line 12
def name
@name
end
|
#slug ⇒ Object
Returns the value of attribute slug.
12
13
14
|
# File 'app/lib/biovision/components/base_component.rb', line 12
def slug
@slug
end
|
#user ⇒ Object
Returns the value of attribute user.
12
13
14
|
# File 'app/lib/biovision/components/base_component.rb', line 12
def user
@user
end
|
#user_link ⇒ Object
Returns the value of attribute user_link.
12
13
14
|
# File 'app/lib/biovision/components/base_component.rb', line 12
def user_link
@user_link
end
|
Class Method Details
.[](user = nil) ⇒ Object
47
48
49
|
# File 'app/lib/biovision/components/base_component.rb', line 47
def self.[](user = nil)
new(BiovisionComponent[slug], user)
end
|
.active? ⇒ Boolean
38
39
40
|
# File 'app/lib/biovision/components/base_component.rb', line 38
def self.active?
BiovisionComponent[slug]&.active?
end
|
.create ⇒ Object
62
63
64
|
# File 'app/lib/biovision/components/base_component.rb', line 62
def self.create
BiovisionComponent.create(slug: slug, settings: default_settings)
end
|
.dependent_models ⇒ Object
Model list for automatic role creation
58
59
60
|
# File 'app/lib/biovision/components/base_component.rb', line 58
def self.dependent_models
[]
end
|
69
70
71
72
73
74
75
76
77
|
# File 'app/lib/biovision/components/base_component.rb', line 69
def self.form_options(entity, scope = :admin, options = {})
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(options)
end
|
.handler(input, user = nil) ⇒ BaseComponent
Receive component-specific handler by component slug
29
30
31
32
|
# File 'app/lib/biovision/components/base_component.rb', line 29
def self.handler(input, user = nil)
type = input.is_a?(String) ? input : input&.slug
handler_class(type)[user]
end
|
.handler_class(slug) ⇒ Object
52
53
54
55
|
# File 'app/lib/biovision/components/base_component.rb', line 52
def self.handler_class(slug)
handler_name = "biovision/components/#{slug}_component".classify
handler_name.safe_constantize || BaseComponent
end
|
.slug ⇒ Object
34
35
36
|
# File 'app/lib/biovision/components/base_component.rb', line 34
def self.slug
to_s.demodulize.to_s.underscore.gsub('_component', '')
end
|
Instance Method Details
#data_value(key, default_value = '') ⇒ Object
140
141
142
143
|
# File 'app/lib/biovision/components/base_component.rb', line 140
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
134
135
136
|
# File 'app/lib/biovision/components/base_component.rb', line 134
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
161
162
163
|
# File 'app/lib/biovision/components/base_component.rb', line 161
def new_entity(model_class, parameters)
model_class.new(parameters)
end
|
#register_metric(name, quantity = 1) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'app/lib/biovision/components/base_component.rb', line 117
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
111
112
113
|
# File 'app/lib/biovision/components/base_component.rb', line 111
def settings
@component.settings
end
|
#settings=(data) ⇒ Object
106
107
108
109
|
# File 'app/lib/biovision/components/base_component.rb', line 106
def settings=(data)
@component.settings.merge!(self.class.normalize_settings(data))
@component.save!
end
|
#update_data_value(key, new_value) ⇒ Object
147
148
149
150
151
|
# File 'app/lib/biovision/components/base_component.rb', line 147
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
155
156
157
|
# File 'app/lib/biovision/components/base_component.rb', line 155
def update_entity(entity, new_attributes)
entity.update(new_attributes)
end
|
#use_settings? ⇒ Boolean
101
102
103
|
# File 'app/lib/biovision/components/base_component.rb', line 101
def use_settings?
use_parameters? || @component.settings.any?
end
|
#user_link!(force_create = false) ⇒ Object
91
92
93
94
95
96
97
98
99
|
# File 'app/lib/biovision/components/base_component.rb', line 91
def user_link!(force_create = false)
if @user_link.nil?
criteria = { biovision_component: @component, user: user }
@user_link = BiovisionComponentUser.new(criteria)
@user_link.save if force_create
end
@user_link
end
|