Class: Phlexi::Display::Base

Inherits:
HTML
  • Object
show all
Defined in:
lib/phlexi/display/base.rb

Overview

A display component for rendering flexible and customizable data views.

Examples:

Basic usage

Phlexi::Display.new(user) do |d|
  render d.field(:name).text
  render d.field(:email).text
end

Direct Known Subclasses

Components::Url

Defined Under Namespace

Classes: Builder, Namespace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, attributes: {}, **options) ⇒ Base

Initializes a new Display instance.

Parameters:

  • record (ActiveModel::Model, Symbol, String)

    The display's associated record or key

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

    Additional HTML attributes for the display container

  • options (Hash)

    Additional options for display configuration

Options Hash (**options):

  • :class (String)

    CSS classes for the display

  • :namespace_klass (Class)

    Custom namespace class

  • :builder_klass (Class)

    Custom field builder class



43
44
45
46
47
48
49
50
51
# File 'lib/phlexi/display/base.rb', line 43

def initialize(record, attributes: {}, **options)
  @attributes = attributes
  @namespace_klass = options.delete(:namespace_klass) || self.class::Namespace
  @builder_klass = options.delete(:builder_klass) || self.class::Builder
  @options = options

  initialize_object_and_key(record)
  initialize_namespace
end

Instance Attribute Details

#keySymbol (readonly)

The display's key, derived from the record or explicitly set

Returns:

  • (Symbol)

    the current value of key



18
19
20
# File 'lib/phlexi/display/base.rb', line 18

def key
  @key
end

#objectActiveModel::Model? (readonly)

The display's associated object

Returns:

  • (ActiveModel::Model, nil)

    the current value of object



18
19
20
# File 'lib/phlexi/display/base.rb', line 18

def object
  @object
end

Class Method Details

.inline(&block) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
# File 'lib/phlexi/display/base.rb', line 23

def self.inline(*, **, &block)
  raise ArgumentError, "block is required" unless block

  new(*, **) do |f|
    f.instance_exec(&block)
  end
end

Instance Method Details

#display_templatevoid

This method returns an undefined value.

Executes the display's content block. Override this in subclasses to define a static display.



64
65
66
# File 'lib/phlexi/display/base.rb', line 64

def display_template
  yield if block_given?
end

#view_templatevoid

This method returns an undefined value.

Renders the display template.



56
57
58
# File 'lib/phlexi/display/base.rb', line 56

def view_template(&)
  display_wrapper { display_template(&) }
end