Module: Fluxbit::ComponentsHelper

Defined in:
app/helpers/fluxbit/components_helper.rb

Instance Method Summary collapse

Instance Method Details

#fluxbit_method(method_name, *args, **kwargs, &c) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'app/helpers/fluxbit/components_helper.rb', line 107

def fluxbit_method(method_name, *args, **kwargs, &c)
  component_klass = "Fluxbit::#{method_name}Component".constantize
  if kwargs[:with_content]
    content = kwargs.delete(:with_content)
    render(component_klass.new(*args, **kwargs).with_content(content), &c)
  else
    render(component_klass.new(*args, **kwargs), &c)
  end
end

#form_builderObject



59
# File 'app/helpers/fluxbit/components_helper.rb', line 59

def form_builder(...) = fluxbit_method("Form::FormBuilder", ...)

#fx_headingObject

Typography



74
# File 'app/helpers/fluxbit/components_helper.rb', line 74

def fx_heading(...) = fluxbit_method("Heading", ...)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/fluxbit/components_helper.rb', line 85

def fx_link_to(body = nil, url = nil, **kwargs, &block)
  kwargs[:as] = :a
  if block_given? && body.is_a?(String) && (url.is_a?(String) || url.is_a?(Symbol))
    # Handle case: fx_link_to("Link text", "/path") { ... }
    kwargs[:href] = url
    fluxbit_method("Link", as: :a, content: body, **kwargs, &block)
  elsif block_given? && body.is_a?(String) && url.nil?
    # Handle case: fx_link_to("/path") { ... content ... }
    kwargs[:href] = body # first arg is URL
    fluxbit_method("Link", as: :a, **kwargs, &block)
  elsif !block_given? && body.is_a?(String) && (url.is_a?(String) || url.is_a?(Symbol))
    # Handle case: fx_link_to("Link text", "/path", class: "...")
    component_klass = "Fluxbit::LinkComponent".constantize
    kwargs[:href] = url
    render(component_klass.new(**kwargs).with_content(body))
  else
    # Handle other cases or provide feedback
    raise ArgumentError, "Invalid arguments for fx_link_to"
  end
end


78
79
80
81
82
83
# File 'app/helpers/fluxbit/components_helper.rb', line 78

def fx_link_to_old(body, url, **kwargs, &block)
  kwargs[:body] = body if body.is_a?(String)
  kwargs[:url] = url if url.is_a?(String)
  kwargs[:with_content] = block if block_given?
  Fluxbit::TextComponent.new(body, url, **kwargs).render(&block)
end

#fx_select(name = nil, option_tags = nil, options = {}, &block) ⇒ Object

Mimics Rails’ select_tag signature: select_tag(name, option_tags = nil, options = {})

Examples:

Basic usage

fx_select "role", ["Admin", "User", "Guest"]

With options

fx_select "country", countries, prompt: "Select a country", class: "custom-select"

With pre-formatted options

fx_select "status", options_for_select(statuses, selected: "active")

Named parameters (backwards compatible)

fx_select name: "role", options: ["Admin", "User"], prompt: "Choose..."

Parameters:

  • name (String, Symbol) (defaults to: nil)

    Name attribute for the select tag

  • option_tags (String, Array, Hash, nil) (defaults to: nil)

    Pre-formatted HTML or raw options data

  • options (Hash) (defaults to: {})

    HTML attributes and select options (prompt, class, etc.)

  • block (Proc)

    Optional block for custom options



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/fluxbit/components_helper.rb', line 40

def fx_select(name = nil, option_tags = nil, options = {}, &block)
  # Handle both positional and named parameters
  if name.is_a?(Hash)
    # Named parameters: fx_select(name: "role", options: [...], prompt: "...")
    options = name
    name = options.delete(:name)
    option_tags = options.delete(:options) || options.delete(:choices)
  elsif option_tags.is_a?(Hash) && options.empty?
    # Two args with second being options: fx_select("role", prompt: "...")
    options = option_tags
    option_tags = options.delete(:options) || options.delete(:choices)
  end

  # Set the options/choices
  options[:options] = option_tags if option_tags.present?
  options[:name] = name if name.present?

  fluxbit_method("Form::Select", **options, &block)
end

#fx_txtObject



75
# File 'app/helpers/fluxbit/components_helper.rb', line 75

def fx_txt(...) = fluxbit_method("Text", ...)