lib/phlexi/display/base.rb in phlexi-display-0.0.3 vs lib/phlexi/display/base.rb in phlexi-display-0.0.4

- old
+ new

@@ -13,15 +13,23 @@ # render d.field(:email).text # end # # @attr_reader [Symbol] key The display's key, derived from the record or explicitly set # @attr_reader [ActiveModel::Model, nil] object The display's associated object - class Base < COMPONENT_BASE + class Base < Phlexi::Display::HTML class Namespace < Phlexi::Field::Structure::Namespace; end - class Builder < Builder; end + class Builder < Phlexi::Display::Builder; end + def self.inline(*, **, &block) + raise ArgumentError, "block is required" unless block + + new(*, **) do |f| + f.instance_exec(&block) + end + end + attr_reader :key, :object delegate :field, :nest_one, :nest_many, to: :@namespace # Initializes a new Display instance. @@ -31,11 +39,10 @@ # @param options [Hash] Additional options for display configuration # @option options [String] :class CSS classes for the display # @option options [Class] :namespace_klass Custom namespace class # @option options [Class] :builder_klass Custom field builder class def initialize(record, attributes: {}, **options) - @display_class = options.delete(:class) @attributes = attributes @namespace_klass = options.delete(:namespace_klass) || self.class::Namespace @builder_klass = options.delete(:builder_klass) || self.class::Builder @options = options @@ -44,20 +51,20 @@ end # Renders the display template. # # @return [void] - def view_template - display_template + def view_template(&) + display_wrapper { display_template(&) } end # Executes the display's content block. # Override this in subclasses to define a static display. # # @return [void] def display_template - instance_exec(&@_content_block) if @_content_block + yield if block_given? end protected attr_reader :options, :attributes, :namespace_klass, :builder_klass @@ -90,22 +97,31 @@ # # @return [void] def initialize_namespace @namespace = namespace_klass.root(key, object: object, builder_klass: builder_klass) end + # Retrieves the display's CSS classes. # # @return [String] The display's CSS classes - attr_reader :display_class + def display_class + themed(:base) + end # Generates the display attributes hash. # # @return [Hash] The display attributes def display_attributes mix({ id: @namespace.dom_id, class: display_class }, attributes) + end + + def display_wrapper(&) + div(**display_attributes) do + yield + end end end end end