Class: Scrapbook::HelperForView

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/scrapbook/helper_for_view.rb

Overview

View helpers for the Scrapbook gem. Doesn’t use standard Rail’s helper modules to avoid any conflicts with host app

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ HelperForView

Returns a new instance of HelperForView.



7
8
9
# File 'app/helpers/scrapbook/helper_for_view.rb', line 7

def initialize(view)
  self.view = view
end

Instance Method Details



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/scrapbook/helper_for_view.rb', line 16

def nav_link_for(scrapbook:, pathname:, is_current: false, depth: 0, **kwargs)
  link_name = pathname == scrapbook.pages_pathname ? scrapbook.name : pathname.basename
  link_attrs = {
    data: {'turbo-frame': 'page_content'},
    class: %w[block w-100],
    aria: {}
  }
  link_attrs[:style] = "padding-left: #{depth}rem;" if depth != 0
  link_attrs[:aria][:current] = 'page' if is_current
  link_attrs[:class].concat(Array(kwargs.delete(:class))) if kwargs.include?(:class)
  link_attrs[:data].merge!(kwargs.delete(:data)) if kwargs.include?(:data)
  link_attrs[:aria].merge!(kwargs.delete(:aria)) if kwargs.include?(:aria)
  link_attrs.merge!(kwargs)

  view.link_to(link_name, short_path_to(pathname, scrapbook), **link_attrs)
end

#remove_handler_exts_from(pathname) ⇒ Object



33
34
35
36
37
# File 'app/helpers/scrapbook/helper_for_view.rb', line 33

def remove_handler_exts_from(pathname)
  pathname.dirname.join(
    pathname.basename.sub(/(?:.#{view.lookup_context.handlers.join('|.')})+\z/, '')
  )
end

#short_path_to(pathname, scrapbook = nil) ⇒ Object



11
12
13
14
# File 'app/helpers/scrapbook/helper_for_view.rb', line 11

def short_path_to(pathname, scrapbook = nil)
  scrapbook ||= Scrapbook.find_scrapbook_for(pathname)
  view.short_page_path(scrapbook.relative_page_path_for(pathname))
end