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

The first argument should be the text of the link. If it's an array, it'll send it directly into I18n.t



58
59
60
61
62
# File 'lib/super/link.rb', line 58

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



64
65
66
# File 'lib/super/link.rb', line 64

def options
  @options
end

Class Method Details

.find(link) ⇒ Object



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

def self.find(link)
  if registry.key?(link)
    found = registry[link]

    if found.is_a?(LinkBuilder)
      return found.dup
    else
      return found
    end
  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



51
52
53
54
# File 'lib/super/link.rb', line 51

def self.polymorphic_parts(*parts_tail)
  parts_head = Super.configuration.path.strip.gsub(%r{\A/+}, "").gsub(%r{/+\z}, "").strip.split("/")
  parts_head.map { |part| part.is_a?(String) ? part.to_sym : part } + parts_tail
end

.registryObject



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
# File 'lib/super/link.rb', line 25

def self.registry
  @registry ||= {}.tap do |reg|
    reg[:new] = LinkBuilder.new
      .text { |params:| Super::Useful::I19.i18n_with_fallback("super", params[:controller].split("/"), "actions.new") }
      .href { |params:| { controller: params[:controller], action: :new, only_path: true } }
      .freeze
    reg[:index] = LinkBuilder.new
      .text { |params:| Super::Useful::I19.i18n_with_fallback("super", params[:controller].split("/"), "actions.index") }
      .href { |params:| { controller: params[:controller], action: :index, only_path: true } }
      .freeze
    reg[:show] = LinkBuilder.new
      .text { |params:, **| Super::Useful::I19.i18n_with_fallback("super", params[:controller].split("/"), "actions.show") }
      .href { |params:, record:| { controller: params[:controller], action: :show, id: record, only_path: true } }
      .freeze
    reg[:edit] = LinkBuilder.new
      .text { |params:, **| Super::Useful::I19.i18n_with_fallback("super", params[:controller].split("/"), "actions.edit") }
      .href { |params:, record:| { controller: params[:controller], action: :edit, id: record, only_path: true } }
      .freeze
    reg[:destroy] = LinkBuilder.new
      .text { |params:, **| Super::Useful::I19.i18n_with_fallback("super", params[:controller].split("/"), "actions.destroy") }
      .href { |params:, record:| { controller: params[:controller], action: :destroy, id: record, only_path: true } }
      .options { |**| { method: :delete, data: { confirm: "Really delete?" } } }
      .freeze
  end
end

Instance Method Details

#==(other) ⇒ Object



98
99
100
# File 'lib/super/link.rb', line 98

def ==(other)
  self.class == other.class && text == other.text && href == other.href && options == other.options
end

#hrefObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/super/link.rb', line 81

def href
  if @href.is_a?(String)
    return @href
  end

  if @href.is_a?(Hash)
    @href = Rails.application.routes.url_for(**@href)
    return @href
  end

  @href = Super::Compatability.polymorphic_path_container.polymorphic_path(@href)
end

#textObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/super/link.rb', line 66

def text
  if @text.is_a?(Array)
    *head, tail = @text
    if !tail.is_a?(Hash)
      head.push(tail)
      tail = {}
    end

    @text = I18n.t(*head, **tail)
    return @text
  end

  @text
end

#to_partial_pathObject



94
95
96
# File 'lib/super/link.rb', line 94

def to_partial_path
  "link"
end