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

- old
+ new

@@ -14,13 +14,13 @@ # 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 Namespace < Structure::Namespace; end + class Namespace < Phlexi::Field::Structure::Namespace; end - class FieldBuilder < Structure::FieldBuilder; end + class Builder < Builder; end attr_reader :key, :object delegate :field, :nest_one, :nest_many, to: :@namespace @@ -33,12 +33,12 @@ # @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) || default_namespace_klass - @builder_klass = options.delete(:builder_klass) || default_builder_klass + @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 @@ -48,22 +48,22 @@ # @return [void] def view_template display_template end - protected - - attr_reader :options, :attributes, :namespace_klass, :builder_klass - # 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 end + protected + + attr_reader :options, :attributes, :namespace_klass, :builder_klass + # Initializes the object and key based on the given record. # # @param record [ActiveModel::Model, Symbol, String] The display's associated record or key # @return [void] def initialize_object_and_key(record) @@ -73,14 +73,16 @@ when String, Symbol @object = nil @key = record else @object = record - @key = if @key.nil? && object.respond_to?(:model_name) && object.model_name.respond_to?(:param_key) && object.model_name.param_key.present? - object.model_name.param_key - else - :object + if @key.nil? + @key = if object.respond_to?(:model_name) && object.model_name.respond_to?(:param_key) && object.model_name.param_key.present? + object.model_name.param_key + else + object.class.name.demodulize.underscore + end end end @key = @key.to_sym end @@ -97,24 +99,13 @@ # Generates the display attributes hash. # # @return [Hash] The display attributes def display_attributes - { + mix({ id: @namespace.dom_id, - class: display_class, - **attributes - } - end - - private - - def default_namespace_klass - self.class::Namespace - end - - def default_builder_klass - self.class::FieldBuilder + class: display_class + }, attributes) end end end end