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.
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
The first argument should be the text of the link.
- #text ⇒ Object
- #to_link(template, local_assigns) ⇒ Object
- #to_partial_path ⇒ Object
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, **) @text = text @href = href @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
64 65 66 |
# File 'lib/super/link.rb', line 64 def @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 |
.registry ⇒ Object
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} } . { |**| {method: :delete, data: {confirm: "Really delete?"}} } .freeze end end |
Instance Method Details
#==(other) ⇒ Object
107 108 109 |
# File 'lib/super/link.rb', line 107 def ==(other) self.class == other.class && text == other.text && href == other.href && == other. end |
#href ⇒ Object
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 |
#text ⇒ Object
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_link(template, local_assigns) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/super/link.rb', line 94 def to_link(template, local_assigns) = local_assigns.fetch(:default_options, {}) template.link_to( text, href, .deep_merge() ) end |
#to_partial_path ⇒ Object
103 104 105 |
# File 'lib/super/link.rb', line 103 def to_partial_path "link" end |