Class: Super::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/super/link.rb

Overview

Links have three required attributes that are passed directly into Rails' link_to helper

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, href, **options) ⇒ Link

Returns a new instance of Link.



88
89
90
91
92
# File 'lib/super/link.rb', line 88

def initialize(text, href, **options)
  @text = text
  @href = href
  @options = options
end

Class Method Details

.find(link) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/super/link.rb', line 11

def self.find(link)
  if link.kind_of?(self)
    return link
  end

  if registry.key?(link)
    return registry[link]
  end

  raise Error::LinkNotRegistered, "Unknown link `#{link}`"
end

.find_all(*links) ⇒ Object



7
8
9
# File 'lib/super/link.rb', line 7

def self.find_all(*links)
  links.map { |link| find(link) }
end

.polymorphic_parts(*parts_tail) ⇒ Object



83
84
85
86
# File 'lib/super/link.rb', line 83

def self.polymorphic_parts(*parts_tail)
  parts_head = Super.configuration.path.strip.gsub(%r{\A/+}, "").gsub(%r{/+\z}, "").strip.split("/")
  parts_head + parts_tail
end

.registryObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/super/link.rb', line 23

def self.registry
  @registry ||= {
    new: new(
      "New",
      -> (params:) {
        Rails.application.routes.url_for(
          controller: params[:controller],
          action: :new,
          only_path: true
        )
      }
    ),
    index: new(
      "Index",
      -> (params:) {
        Rails.application.routes.url_for(
          controller: params[:controller],
          action: :index,
          only_path: true
        )
      }
    ),
    show: new(
      "View",
      -> (record:, params:) {
        Rails.application.routes.url_for(
          controller: params[:controller],
          action: :show,
          id: record,
          only_path: true
        )
      }
    ),
    edit: new(
      "Edit",
      -> (record:, params:) {
        Rails.application.routes.url_for(
          controller: params[:controller],
          action: :edit,
          id: record,
          only_path: true
        )
      }
    ),
    destroy: new(
      "Delete",
      -> (record:, params:) {
        Rails.application.routes.url_for(
          controller: params[:controller],
          action: :destroy,
          id: record,
          only_path: true
        )
      },
      method: :delete,
      data: { confirm: "Really delete?" }
    ),
  }
end

Instance Method Details

#to_s(default_options: nil, **proc_arguments) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/super/link.rb', line 94

def to_s(default_options: nil, **proc_arguments)
  default_options ||= {}
  ActionController::Base.helpers.link_to(
    value(text, proc_arguments),
    value(href, proc_arguments),
    default_options.deep_merge(value(options, proc_arguments))
  )
end