Class: Para::ComponentsConfiguration::Component
- Inherits:
-
Object
- Object
- Para::ComponentsConfiguration::Component
- Defined in:
- lib/para/components_configuration.rb
Instance Attribute Summary collapse
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#model ⇒ Object
Returns the value of attribute model.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#shown_if ⇒ Object
Returns the value of attribute shown_if.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #child_components ⇒ Object
- #component(*args, **child_options, &block) ⇒ Object
-
#initialize(identifier, type_identifier, shown_if: nil, **options, &block) ⇒ Component
constructor
A new instance of Component.
-
#options_with_defaults ⇒ Object
Ensures unset :configuration store options are set to nil to allow removing a configuration option from the components.rb file.
- #refresh(attributes = {}) ⇒ Object
Constructor Details
#initialize(identifier, type_identifier, shown_if: nil, **options, &block) ⇒ Component
Returns a new instance of Component.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/para/components_configuration.rb', line 180 def initialize(identifier, type_identifier, shown_if: nil, **, &block) @identifier = identifier.to_s @type = Para::Component.registered_components[type_identifier] @options = @shown_if = shown_if @parent = .delete(:parent) # Build child components if a block is provided instance_eval(&block) if block return if type raise UndefinedComponentTypeError, "Undefined Para component : #{type_identifier}. " + 'Please ensure that your app or gems define this component type.' end |
Instance Attribute Details
#identifier ⇒ Object
Returns the value of attribute identifier.
178 179 180 |
# File 'lib/para/components_configuration.rb', line 178 def identifier @identifier end |
#model ⇒ Object
Returns the value of attribute model.
178 179 180 |
# File 'lib/para/components_configuration.rb', line 178 def model @model end |
#options ⇒ Object
Returns the value of attribute options.
178 179 180 |
# File 'lib/para/components_configuration.rb', line 178 def @options end |
#parent ⇒ Object
Returns the value of attribute parent.
178 179 180 |
# File 'lib/para/components_configuration.rb', line 178 def parent @parent end |
#shown_if ⇒ Object
Returns the value of attribute shown_if.
178 179 180 |
# File 'lib/para/components_configuration.rb', line 178 def shown_if @shown_if end |
#type ⇒ Object
Returns the value of attribute type.
178 179 180 |
# File 'lib/para/components_configuration.rb', line 178 def type @type end |
Instance Method Details
#child_components ⇒ Object
205 206 207 |
# File 'lib/para/components_configuration.rb', line 205 def child_components @child_components ||= [] end |
#component(*args, **child_options, &block) ⇒ Object
196 197 198 199 200 201 202 203 |
# File 'lib/para/components_configuration.rb', line 196 def component(*args, **, &block) # Do not allow nesting components more than one level as the display of illimited # child nesting deepness is not implemented raise ComponentTooDeepError, 'Cannot nest components more than one level' if parent = .merge(parent: self) child_components << Component.new(*args, **, &block) end |
#options_with_defaults ⇒ Object
Ensures unset :configuration store options are set to nil to allow removing a configuration option from the components.rb file
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/para/components_configuration.rb', line 222 def configurable_keys = type.local_stored_attributes.try(:[], :configuration) || [] configurable_keys += .keys configurable_keys.uniq! = {} # 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. [:parent_component] = parent&.model configurable_keys.each_with_object() do |key, hash| hash[key] = [key] end end |
#refresh(attributes = {}) ⇒ Object
209 210 211 212 213 214 215 216 217 |
# File 'lib/para/components_configuration.rb', line 209 def refresh(attributes = {}) @model = type.where(identifier: identifier).first_or_initialize model.update_with(attributes.merge()) model.save! child_components.each_with_index do |child_component, child_index| child_component.refresh(component_section: nil, position: child_index) end end |