configurable_engine icon indicating copy to clipboard operation
configurable_engine copied to clipboard

Instance of class does not behave like ActiveRecord model

Open lacostenycoder opened this issue 2 years ago • 0 comments

The error occur when trying to create a new model using ActiveRecord which hasn't been initialized in the config/configurable.yml file.

Ruby Version: 2.6.6 gem version: 0.4.6

Inside a Rails console:

c = Configurable.new
c.name='evil'
c.value='monkey_patch'
t=Time.now.to_s(:db)
c.created_at=t
c.updated_at=t
c.valid?
 c.valid?
NoMethodError: undefined method `[]' for nil:NilClass
from /home/dev/rails/my_app/vendor/cache/gems/configurable_engine-0.4.8/app/models/configurable.rb:125:in `type_of_value'

Looking at the method reveals the bug, which should be an easy fix:

  def type_of_value
    return unless name
    valid = case Configurable.defaults[name][:type] # raises if  Configurable.defaults[name].nil?
    when 'boolean'
      [true, 1, "1", "true", false, 0, "0", "false"].include?(value)
    when 'decimal'
      BigDecimal(value) rescue false
    when 'integer'
      Integer(value) rescue false
    when 'list'
      value.is_a?(Array)
    else
      true
    end
    errors.add(:value, I18n.t("activerecord.errors.messages.invalid")) unless valid
  end

lacostenycoder avatar Aug 24 '23 21:08 lacostenycoder