Module: Superlink::Phlex

Defined in:
lib/superlink.rb

Instance Method Summary collapse

Instance Method Details

#xcreate(*segments, &content) ⇒ Object



113
114
115
# File 'lib/superlink.rb', line 113

def xcreate(*segments, **, &content)
  xlink_to(*segments.push(:new), **, &content)
end

#xdestroy(*segments, confirm: "Are you sure?", &content) ⇒ Object



117
118
119
# File 'lib/superlink.rb', line 117

def xdestroy(*segments, confirm: "Are you sure?", **, &content)
  xlink_to(*segments, data_turbo: { method: :delete, confirm: }, **, &content)
end

#xedit(*segments, &content) ⇒ Object



109
110
111
# File 'lib/superlink.rb', line 109

def xedit(*segments, **, &content)
  xlink_to(*segments.push(:edit), **, &content)
end


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/superlink.rb', line 85

def xlink_to(target, *segments, **, &content)
  if target.nil? and segments.empty?
    return # Render nothing if we link to nothing
  elsif target.is_a? URI::Builder::DSL and segments.empty?
    builder = target
    url = builder.uri
    model = builder.model
    href = helpers.request.host == url.host ? url.path : url.to_s

    a(href:, **) {
      if content&.lambda? and model
        render content.call model
      elsif content
        render content
      else
        render url.to_s
      end
    }
  else
    xlink_to(helpers.url.join(target, *segments), **, &content)
  end
end