Class: RenderTurboStream::Libs

Inherits:
Object
  • Object
show all
Defined in:
lib/render_turbo_stream/libs.rb

Class Method Summary collapse

Class Method Details

.debug_save_errors(object, controller_action_name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/render_turbo_stream/libs.rb', line 91

def self.debug_save_errors(object, controller_action_name)
  if object.id.present?
    Rails.logger.debug("#{controller_action_name} #{object.class}:#{object.id} FAILED:")
  else
    Rails.logger.debug("#{controller_action_name} #{object.class} FAILED:")
  end
  object.errors.full_messages.each do |e|
    Rails.logger.debug("    => #{e}")
  end
end

.fetch_arguments_from_rendered_string(rendered_string) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/render_turbo_stream/libs.rb', line 40

def self.fetch_arguments_from_rendered_string(rendered_string)
  noko = Nokogiri::HTML(rendered_string)
  frame = noko.at_css('turbo-frame')
  ele = noko.at_css('turbo-target')
  if frame.present?
    {
      target_id: frame[:id],
      target: target_id_to_target(frame[:id])
    }
  elsif ele.present?
    {
      target_id: ele[:id],
      target: target_id_to_target(ele[:id])
    }
  else
    {}
  end
end

.partial_path(controller_path, partial) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/render_turbo_stream/libs.rb', line 24

def self.partial_path(controller_path, partial)
  if partial && partial.to_s.include?('/')
    partial
  elsif partial.present?
    [controller_path, partial].join('/')
  end
end

.target_id(virt_view_path, id) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/render_turbo_stream/libs.rb', line 59

def self.target_id(virt_view_path, id)

  if id.is_a?(String)
    id
  elsif id
    (
      (id.is_a?(Array) ? id : [id]).map do |_id|

        if _id.is_a?(String)
          _id
        elsif _id.methods.include?(:id)
          (
            _id.id ?
              [_id.class.to_s.downcase, _id.id] :
              ['new', _id.class.to_s.downcase]
          ).join('-')
        else
          _id.to_s
        end
      end +
        [
          virt_view_path.split('/').last.sub(/^_/, '')
        ]
    ).join('-')

  else
    [
      virt_view_path.gsub('/', '-').gsub('-_', '-')
    ].join('-')
  end
end

.target_id_to_target(target_id) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/render_turbo_stream/libs.rb', line 4

def self.target_id_to_target(target_id)
  if target_id.present?
    if target_id[0] == '#'
      target_id
    else
      "##{target_id}"
    end
  else
    nil
  end
end

.target_to_target_id(target) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/render_turbo_stream/libs.rb', line 16

def self.target_to_target_id(target)
  if target.present? && target.to_s[0] == '#'
    target[1..-1]
  else
    nil
  end
end

.template_path(controller_path, template) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/render_turbo_stream/libs.rb', line 32

def self.template_path(controller_path, template)
  if template && template.to_s.include?('/')
    template
  elsif template.present?
    [controller_path, template].join('/')
  end
end