G5 / storext

Add type-casting and other features on top of ActiveRecord::Store.store_accessor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Boolean type not recognized when using store_attributes inside concern

radubogdan opened this issue · comments

Hello again,

I noticed I get errors if I move store_attributes in a concern and use the Boolean type.

module CarLeasing
  extend ActiveSupport::Concern

  included do

    store_attributes :leasing_offer do
      leasing_has_rest_debt Boolean
    end

  end
end
pry(main)> Car.storext_definitions
NameError: uninitialized constant CarLeasing::Boolean

However inside the model which includes this concern everything works:

{:leasing_has_rest_debt=>{:column=>:leasing_offer, :type=>Axiom::Types::Boolean (BasicObject), :opts=>{}}}

Is it not suitable to use concerns together with storext?

Thanks!

I can probably try to work on it if you confirm is a bug.

Steps to reproduce:

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  ruby '2.3.1'

  gem "pg", '~> 0.20.0'
  gem "rails", '~> 5.0.0'
  gem "storext"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(
  adapter: "postgresql",
  username: "postgres",
  password: "",
  database: "postgres"
)
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :cars do |t|
    t.jsonb :leasing_offer, default: {}
  end
end

module CarLeasing
  extend ActiveSupport::Concern

  included do
    store_attributes :leasing_offer do
      leasing_has_rest_debt Boolean, default: false
    end
  end
end

class Car < ActiveRecord::Base
  include Storext.model
  include CarLeasing
end

class BugTest < Minitest::Test
  def test_assert_car_leasing_store_attributes_not_raising_exception
    assert true
  end
end

@radubogdan yup that's because Boolean is a Virtus constant that it can't find when importing it from a concern.

The fix would be somewhere here.

@radubogdan I'll close this. I'd be happy to get a pull request!

in case anyone else runs into this, here's how I solved this:

included do |base|
  store_attributes :settings do
    setting_field base::Boolean, default: false
  end
end