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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Link.



77
78
79
80
81
# File 'lib/super/link.rb', line 77

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

Instance Attribute Details

#hrefObject (readonly)

Returns the value of attribute href.



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

def href
  @href
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

Class Method Details

.registryObject



17
18
19
20
21
22
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
# File 'lib/super/link.rb', line 17

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

.resolve(link) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/super/link.rb', line 5

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

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

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