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) ⇒ Component

Returns a new instance of Component.



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/para/components_configuration.rb', line 154

def initialize(identifier, type_identifier, shown_if: nil, **options)
  self.identifier = identifier.to_s
  self.type = Para::Component.registered_components[type_identifier]
  self.options = options
  self.shown_if = shown_if

  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.



152
153
154
# File 'lib/para/components_configuration.rb', line 152

def identifier
  @identifier
end

#modelObject

Returns the value of attribute model.



152
153
154
# File 'lib/para/components_configuration.rb', line 152

def model
  @model
end

#optionsObject

Returns the value of attribute options.



152
153
154
# File 'lib/para/components_configuration.rb', line 152

def options
  @options
end

#shown_ifObject

Returns the value of attribute shown_if.



152
153
154
# File 'lib/para/components_configuration.rb', line 152

def shown_if
  @shown_if
end

#typeObject

Returns the value of attribute type.



152
153
154
# File 'lib/para/components_configuration.rb', line 152

def type
  @type
end

Instance Method Details

#options_with_defaultsObject

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



177
178
179
180
181
182
183
184
185
# File 'lib/para/components_configuration.rb', line 177

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

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

#refresh(attributes = {}) ⇒ Object



168
169
170
171
172
# File 'lib/para/components_configuration.rb', line 168

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