Module: Compony::ComponentMixins::Default::Labelling

Extended by:
ActiveSupport::Concern
Defined in:
lib/compony/component_mixins/default/labelling.rb

Overview

api description This module contains all methods for Component that concern labelling and look.

Instance Method Summary collapse

Instance Method Details

#button(keyword, &block) ⇒ Object

DSL method Defines defaults for intents when rendering buttons. Just like in #label, the block may be given a resource.

Parameters:

  • keyword (Symbol)

    The name of the keyword that should be given to the button by the intent if not overwritten

  • block (Proc)

    The block that, when called in the context of the component while rendering, returns the value for the arg given to the button.

See Also:

  • Compony::ComponentMixins::Default::Labelling.{Compony{Compony::Component{Compony::Component#button_defaults}


61
62
63
64
65
# File 'lib/compony/component_mixins/default/labelling.rb', line 61

def button(keyword, &block)
  fail("Please pass a block to `button` in #{inspect}.") unless block_given?
  @button_blocks ||= {}
  @button_blocks[keyword.to_sym] = block
end

#button_defaults(resource = nil) ⇒ Object

Executes and retrieves the button blocks If this component is resourceful, give the block the resource. Expect the arity to match.

Parameters:

  • resource (defaults to: nil)

    Pass the resource if and only if the component is resourceful.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/compony/component_mixins/default/labelling.rb', line 70

def button_defaults(resource = nil)
  return @button_blocks.to_h do |keyword, block|
    value = case block.arity
            when 0
              block.call
            when 1
              resource ||= data
              if resource.blank?
                fail("Button block #{keyword.inspect} of #{inspect} takes a resource, but none was provided and a call to `data` did not return any.")
              end
              block.call(resource)
            else
              fail "#{inspect} has a button block #{keyword.inspect} that takes 2 or more arguments, which is unsupported."
            end
    next [keyword, value]
  end
end

#label(data_or_format = nil, format: :long, &block) ⇒ Object

DSL method and accessor When assigning via DSL, pass format as first parameter. When accessing the value, pass format as named parameter (e.g. format: :short).
A component either generates labels without data (e.g. "New user") or with data (e.g. "Edit John Doe"). This needs to be consistent across all formats. If the block generates labels with data, the label block must take exactly one argument, otherwise none. Label blocks with data are given the data as argument. The block is expected to return the label in the given format.
Examples:

  • Setting a block with data: label(:short){ |data| "Edit #{data.label}" }
  • Setting a block without data: label(:short){ 'New user' }
  • Reading a component's label with data: comp.label(User.first, format: :short)
  • Reading a component's label without data: comp.label(format: :short)


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
# File 'lib/compony/component_mixins/default/labelling.rb', line 24

def label(data_or_format = nil, format: :long, &block)
  format = data_or_format if block_given?
  format ||= :long
  format = format.to_sym

  if block_given?
    # Assignment via DSL
    if format == :all
      @label_blocks[:short] = block
      @label_blocks[:long] = block
    else
      @label_blocks[format] = block
    end
  else
    # Retrieval of the actual label
    fail('Label format :all may only be used for setting a label (with a block), not for retrieving it.') if format == :all
    label_block = @label_blocks[format] || fail("Format #{format} was not found for #{inspect}.")
    case label_block.arity
    when 0
      label_block.call
    when 1
      data_or_format ||= data
      if data_or_format.blank?
        fail "Label block of #{inspect} takes an argument, but no data was provided and a call to `data` did not return any data either."
      end
      label_block.call(data_or_format)
    else
      fail "#{inspect} has a label block that takes 2 or more arguments, which is unsupported."
    end
  end
end