Class: Biovision::Components::BaseComponent

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

#upload_image, #use_images?

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.

Parameters:



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

#componentObject (readonly)

Returns the value of attribute component.



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

def component
  @component
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#slugObject (readonly)

Returns the value of attribute slug.



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

def slug
  @slug
end

#userObject

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.

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

Parameters:

  • user (User) (defaults to: nil)


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

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

.active?Boolean

Returns:

  • (Boolean)


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

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

.createObject



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_modelsObject

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

Parameters:

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


70
71
72
73
74
75
76
77
78
# File 'app/lib/biovision/components/base_component.rb', line 70

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:



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

Parameters:

  • slug (String)


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

.slugObject



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

Parameters:

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


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

Parameters:

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


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

Parameters:

  • model_class (Class)
  • parameters (Hash)


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

Parameters:

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


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

#settingsObject



90
91
92
# File 'app/lib/biovision/components/base_component.rb', line 90

def settings
  @component.settings
end

#settings=(data) ⇒ Object

Parameters:

  • data (Hash)


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

Parameters:

  • key (String|Symbol)
  • new_value


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

Parameters:

  • entity (ApplicationRecord)
  • new_attributes (Hash)


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

Returns:

  • (Boolean)


80
81
82
# File 'app/lib/biovision/components/base_component.rb', line 80

def use_settings?
  use_parameters? || @component.settings.any?
end