Class: Biovision::Components::BaseComponent

Inherits:
Object
  • Object
show all
Extended by:
Biovision::Components::Base::ComponentSettings
Includes:
Biovision::Components::Base::ComponentParameters, Biovision::Components::Base::ComponentPrivileges, Biovision::Components::Base::EntityLinks
Defined in:
app/lib/biovision/components/base_component.rb

Overview

Base biovision component

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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::EntityLinks

#entity_link, #text_for_link

Methods included from Biovision::Components::Base::ComponentParameters

#[], #[]=, #manage_settings?, #receive, #use_images?, #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.

Parameters:



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

#componentObject (readonly)

Returns the value of attribute component.



12
13
14
# File 'app/lib/biovision/components/base_component.rb', line 12

def component
  @component
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'app/lib/biovision/components/base_component.rb', line 12

def name
  @name
end

#slugObject (readonly)

Returns the value of attribute slug.



12
13
14
# File 'app/lib/biovision/components/base_component.rb', line 12

def slug
  @slug
end

#userObject

Returns the value of attribute user.



12
13
14
# File 'app/lib/biovision/components/base_component.rb', line 12

def user
  @user
end

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

Receive component-specific handler by class name for component.

e.g.: Biovision::Components::UsersComponent

Parameters:

  • user (User) (defaults to: nil)


47
48
49
# File 'app/lib/biovision/components/base_component.rb', line 47

def self.[](user = nil)
  new(BiovisionComponent[slug], user)
end

.active?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/lib/biovision/components/base_component.rb', line 38

def self.active?
  BiovisionComponent[slug]&.active?
end

.createObject



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_modelsObject

Model list for automatic role creation



58
59
60
# File 'app/lib/biovision/components/base_component.rb', line 58

def self.dependent_models
  []
end

.form_options(entity, scope = :admin, options = {}) ⇒ Object

Parameters:

  • entity (ApplicationRecord)
  • scope (Symbol|nil) (defaults to: :admin)
  • options (Hash) (defaults to: {})


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

Parameters:

Returns:



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

Parameters:

  • slug (String)


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

.slugObject



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

Parameters:

  • key (String|Symbol)
  • default_value (defaults to: '')


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

Parameters:

  • user (User)
  • code_type (String)
  • quantity (Integer) (defaults to: 1)


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

Parameters:

  • model_class (Class)
  • parameters (Hash)


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

Parameters:

  • name (String)
  • quantity (Integer) (defaults to: 1)


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

#settingsObject



111
112
113
# File 'app/lib/biovision/components/base_component.rb', line 111

def settings
  @component.settings
end

#settings=(data) ⇒ Object

Parameters:

  • data (Hash)


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

Parameters:

  • key (String|Symbol)
  • new_value


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

Parameters:

  • entity (ApplicationRecord)
  • new_attributes (Hash)


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

Returns:

  • (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