Class: Super::Link
- Inherits:
-
Object
- Object
- Super::Link
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Class Method Summary collapse
- .find(link) ⇒ Object
- .find_all(*links) ⇒ Object
- .polymorphic_parts(*parts_tail) ⇒ Object
- .registry ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #href ⇒ Object
-
#initialize(text, href, **options) ⇒ Link
constructor
A new instance of Link.
- #to_partial_path ⇒ Object
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, **) @text = text @href = href = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
95 96 97 |
# File 'lib/super/link.rb', line 95 def end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
94 95 96 |
# File 'lib/super/link.rb', line 94 def text @text 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.map { |part| part.is_a?(String) ? part.to_sym : part } + parts_tail end |
.registry ⇒ Object
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: LinkBuilder.new( "New", -> (params:) { { controller: params[:controller], action: :new, only_path: true } } ), index: LinkBuilder.new( "Index", -> (params:) { { controller: params[:controller], action: :index, only_path: true } } ), show: LinkBuilder.new( "View", -> (record:, params:) { { controller: params[:controller], action: :show, id: record, only_path: true } } ), edit: LinkBuilder.new( "Edit", -> (record:, params:) { { controller: params[:controller], action: :edit, id: record, only_path: true } } ), destroy: LinkBuilder.new( "Delete", -> (record:, params:) { { controller: params[:controller], action: :destroy, id: record, only_path: true } }, method: :delete, data: { confirm: "Really delete?" } ), } end |
Instance Method Details
#==(other) ⇒ Object
114 115 116 |
# File 'lib/super/link.rb', line 114 def ==(other) self.class == other.class && text == other.text && href == other.href && == other. end |
#href ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/super/link.rb', line 97 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 |
#to_partial_path ⇒ Object
110 111 112 |
# File 'lib/super/link.rb', line 110 def to_partial_path "link" end |