Class: Satis::ApplicationComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
ActionView::Helpers::TranslationHelper, ViewComponent::SlotableV2
Defined in:
app/components/satis/application_component.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#original_view_contextObject

Returns the value of attribute original_view_context.



8
9
10
# File 'app/components/satis/application_component.rb', line 8

def original_view_context
  @original_view_context
end

Class Method Details

.add_helper(name, component) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/components/satis/application_component.rb', line 57

def self.add_helper(name, component)
  if respond_to?(name)
    Satis.config.logger.warn("Helper #{name} already defined, skipping.")
    return
  end
  define_method(name) do |*args, **kwargs, &block|
    original_args = args.dup
    options = args.extract_options!
    instance = if options.key? :variant
      variant_component = component.to_s.sub(/::Component$/, "::#{options[:variant].to_s.camelize}::Component").safe_constantize
      (variant_component || component).new(*original_args, **kwargs)
    else
      kwargs[component_name.to_sym] = self
      component.new(*original_args, **kwargs)
    end
    original_view_context.render(instance, &block)
  end
end

Instance Method Details

#component_nameObject



53
54
55
# File 'app/components/satis/application_component.rb', line 53

def component_name
  self.class.name.sub(/::Component$/, "").sub(/^Satis::/, "").underscore
end

#ct(key = nil, **options) ⇒ Object

This provides us with a translation helper which scopes into the original view and thereby conveniently scopes the translations.

In your component.html.slim you can use: “‘slim

ct(“.#Satis::ApplicationComponent.tabtab.name”, scope: [group.to_sym])

““

It’ll then try and find a translation with scope: en.admin.spaces.edit.tabs.main.admin_versions



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/components/satis/application_component.rb', line 24

def ct(key = nil, **options)
  scope = Array.wrap(options.delete(:scope))

  scope = if scope
            scope.unshift(i18n_scope)
          else
            [i18n_scope]
          end

  scope = original_i18n_scope.concat(scope)

  key = key&.to_s unless key.is_a?(String)
  key = "#{scope.join('.')}#{key}" if key.start_with?('.')

  original_view_context.t(key, **options)
end

#i18n_scopeObject



49
50
51
# File 'app/components/satis/application_component.rb', line 49

def i18n_scope
  self.class.name.split('::').second.underscore.to_sym
end

#original_i18n_scopeObject



45
46
47
# File 'app/components/satis/application_component.rb', line 45

def original_i18n_scope
  original_virtual_path.sub(%r{^/}, '').gsub(%r{/_?}, '.').split('.')
end

#original_virtual_pathObject



41
42
43
# File 'app/components/satis/application_component.rb', line 41

def original_virtual_path
  original_view_context.instance_variable_get(:@virtual_path)
end