Class: Satis::ApplicationComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Satis::ApplicationComponent
- Includes:
- ActionView::Helpers::TranslationHelper, ViewComponent::SlotableV2
- Defined in:
- app/components/satis/application_component.rb
Direct Known Subclasses
Satis::AppearanceSwitcher::Component, Satis::Avatar::Component, Breadcrumbs::Component, Card::Component, DateTimePicker::Component, FlashMessages::Component, FlashMessages::Message, Info::Component, InfoItem::Component, Input::Component, Map::Component, Menu::Component, MenuItem::Component, Page::Component, SidebarMenu::Component, SidebarMenuItem::Component, Switch::Component, Tab::Component, Tabs::Component
Instance Attribute Summary collapse
-
#original_view_context ⇒ Object
Returns the value of attribute original_view_context.
Class Method Summary collapse
Instance Method Summary collapse
- #component_name ⇒ Object
-
#ct(key = nil, **options) ⇒ Object
This provides us with a translation helper which scopes into the original view and thereby conveniently scopes the translations.
- #i18n_scope ⇒ Object
- #original_i18n_scope ⇒ Object
- #original_virtual_path ⇒ Object
Instance Attribute Details
#original_view_context ⇒ Object
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 = args. instance = if .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_name ⇒ Object
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, **) scope = Array.wrap(.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, **) end |
#i18n_scope ⇒ Object
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_scope ⇒ Object
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_path ⇒ Object
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 |