Class: Para::ComponentsConfiguration::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/para/components_configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, type_identifier, shown_if: nil, **options, &block) ⇒ Component

Returns a new instance of Component.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/para/components_configuration.rb', line 175

def initialize(identifier, type_identifier, shown_if: nil, **options, &block)
  @identifier = identifier.to_s
  @type = Para::Component.registered_components[type_identifier]
  @options = options
  @shown_if = shown_if
  @parent = options.delete(:parent)

  # Build child components if a block is provided
  instance_eval(&block) if block

  unless type
    raise UndefinedComponentTypeError.new(
      "Undefined Para component : #{ type_identifier }. " +
      "Please ensure that your app or gems define this component type."
    )
  end
end

Instance Attribute Details

#identifierObject

Returns the value of attribute identifier.



173
174
175
# File 'lib/para/components_configuration.rb', line 173

def identifier
  @identifier
end

#modelObject

Returns the value of attribute model.



173
174
175
# File 'lib/para/components_configuration.rb', line 173

def model
  @model
end

#optionsObject

Returns the value of attribute options.



173
174
175
# File 'lib/para/components_configuration.rb', line 173

def options
  @options
end

#parentObject

Returns the value of attribute parent.



173
174
175
# File 'lib/para/components_configuration.rb', line 173

def parent
  @parent
end

#shown_ifObject

Returns the value of attribute shown_if.



173
174
175
# File 'lib/para/components_configuration.rb', line 173

def shown_if
  @shown_if
end

#typeObject

Returns the value of attribute type.



173
174
175
# File 'lib/para/components_configuration.rb', line 173

def type
  @type
end

Instance Method Details

#child_componentsObject



204
205
206
# File 'lib/para/components_configuration.rb', line 204

def child_components
  @child_components ||= []
end

#component(*args, **child_options, &block) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/para/components_configuration.rb', line 193

def component(*args, **child_options, &block)
  # Do not allow nesting components more than one level as the display of illimited
  # child nesting deepness is not implemented
  if parent
    raise ComponentTooDeepError, "Cannot nest components more than one level"
  end

  child_component_options = child_options.merge(parent: self)
  child_components << Component.new(*args, **child_component_options, &block)
end

#options_with_defaultsObject

Ensures unset :configuration store options are set to nil to allow removing a configuration option from the components.rb file



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/para/components_configuration.rb', line 221

def options_with_defaults
  configurable_keys = type.local_stored_attributes.try(:[], :configuration) || []
  configurable_keys += options.keys
  configurable_keys.uniq!

  options_with_defaults = {}

  # Assign parent component resource to the final attribute options, assigning nil
  # if the `:parent` option is empty, to allow extracting a component from its
  # parent by just moving the component call outside of its parent block.
  options_with_defaults[:parent_component] = parent&.model

  configurable_keys.each_with_object(options_with_defaults) do |key, hash|
    hash[key] = options[key]
  end
end

#refresh(attributes = {}) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/para/components_configuration.rb', line 208

def refresh(attributes = {})
  @model = type.where(identifier: identifier).first_or_initialize
  model.update_with(attributes.merge(options_with_defaults))
  model.save!

  child_components.each_with_index do |child_component, child_index|
    child_component.refresh(component_section: nil, position: child_index)
  end
end