Skip to content

Instance of class does not behave like ActiveRecord model #60

Open
@lacostenycoder

Description

@lacostenycoder

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions