Class: CafeCar::LinkBuilder
- Inherits:
-
Object
- Object
- CafeCar::LinkBuilder
- Defined in:
- lib/cafe_car/link_builder.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
- #can?(action) ⇒ Boolean
- #cant?(action) ⇒ Boolean
- #code(path = nil) ⇒ Object
- #confirm(key) ⇒ Object
- #destroy ⇒ Object
- #disabled(action, reason) ⇒ Object
- #edit ⇒ Object
- #html_safe? ⇒ Boolean
- #i18n(scope: nil) ⇒ Object
- #index ⇒ Object
-
#initialize(template, object, namespace: template.namespace) ⇒ LinkBuilder
constructor
A new instance of LinkBuilder.
- #link(action, target, label = i18n(action), disabled: false, hide: false, params: nil, **opts) ⇒ Object
- #new ⇒ Object
- #p ⇒ Object
- #show ⇒ Object
- #to_s ⇒ Object
- #turbo!(opts) ⇒ Object
Constructor Details
#initialize(template, object, namespace: template.namespace) ⇒ LinkBuilder
Returns a new instance of LinkBuilder.
8 9 10 11 12 |
# File 'lib/cafe_car/link_builder.rb', line 8 def initialize(template, object, namespace: template.namespace) @template = template @object = object @namespace = namespace end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
3 4 5 |
# File 'lib/cafe_car/link_builder.rb', line 3 def object @object end |
Instance Method Details
#can?(action) ⇒ Boolean
15 |
# File 'lib/cafe_car/link_builder.rb', line 15 def can?(action) = policy.public_send("#{action}?") |
#cant?(action) ⇒ Boolean
16 |
# File 'lib/cafe_car/link_builder.rb', line 16 def cant?(action) = !can?(action) && disabled(action, :policy) |
#code(path = nil) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/cafe_car/link_builder.rb', line 54 def code(path = nil) return unless Rails.env.development? return unless @template.request.local? path ||= caller_locations(1, 1).first.path link_to "✎", "rubymine://open?file=#{path}" # &line=%{line} end |
#confirm(key) ⇒ Object
20 |
# File 'lib/cafe_car/link_builder.rb', line 20 def confirm(key) = i18n(key, scope: :confirm) |
#destroy ⇒ Object
50 |
# File 'lib/cafe_car/link_builder.rb', line 50 def destroy(*, **, &) = link(:destroy, @object, *, method: :delete, confirm: confirm(:destroy), **, &) |
#disabled(action, reason) ⇒ Object
21 |
# File 'lib/cafe_car/link_builder.rb', line 21 def disabled(action, reason) = i18n(action, scope: [ :disabled, reason ]) |
#edit ⇒ Object
49 |
# File 'lib/cafe_car/link_builder.rb', line 49 def edit(...) = link(:edit, @object, ...) |
#html_safe? ⇒ Boolean
62 |
# File 'lib/cafe_car/link_builder.rb', line 62 def html_safe? = true |
#i18n(scope: nil) ⇒ Object
18 |
# File 'lib/cafe_car/link_builder.rb', line 18 def i18n(*, scope: nil, **) = p.i18n(*, scope: [ :controls, *scope ], **) |
#index ⇒ Object
51 |
# File 'lib/cafe_car/link_builder.rb', line 51 def index(*, **, &) = link(:index, model, *, hide: true, data: { turbo_stream: nil }, **, &) |
#link(action, target, label = i18n(action), disabled: false, hide: false, params: nil, **opts) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cafe_car/link_builder.rb', line 33 def link(action, target, label = i18n(action), disabled: false, hide: false, params: nil, **opts, &) params ||= {} disabled ||= cant?(action) return if disabled and hide href = href_for(*target, action:, namespace: @namespace, **params) current = current_page?(href, check_parameters: true) in_link = @template.context?(:a) content = block_given? ? capture(label, &) : label link_to_unless(disabled || current || in_link, content, href, **turbo!(opts)) do @template.tag.span(content, class: "disabled", disabled: true, title: disabled.presence) end end |
#new ⇒ Object
52 |
# File 'lib/cafe_car/link_builder.rb', line 52 def new(*, **, &) = link(:new, model, *, hide: true, **, &) |
#p ⇒ Object
14 |
# File 'lib/cafe_car/link_builder.rb', line 14 def p = @template.present(@object) |
#show ⇒ Object
48 |
# File 'lib/cafe_car/link_builder.rb', line 48 def show(*, **, &) = link(:show, @object, *, data: { turbo_stream: nil }, **, &) |
#to_s ⇒ Object
63 |
# File 'lib/cafe_car/link_builder.rb', line 63 def to_s = p.to_s |
#turbo!(opts) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/cafe_car/link_builder.rb', line 23 def turbo!(opts) opts.replace({ data: { turbo_stream: true, turbo_method: opts.delete(:method), turbo_confirm: opts.delete(:confirm) } }.deep_merge(opts)) end |