Skip to content

ActiveEntity embeds are not persisted by AllFutures v2 #8

Open
@willmcneilly

Description

@willmcneilly

You asked me to put together a bug report for the issue I was seeing, so here it is.

Expected Behaviour

ActiveEntity embeds should be persisted along with all other attributes when create is called on the parent.

Actual Behaviour

The embeds are created in memory but not persisted to redis.

Minimal Reproduction

Here we have two AllFutures models; TodoList which has many TodoListItem.

Note: Redis should be running.

# frozen_string_literal: true

require "bundler/inline"

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

  gem "rails", "~> 7.0.0"
  gem "kredis", "~> 1.0"
  gem "all_futures", github: "leastbad/all_futures", branch: "master"
end

require "rack/test"
require "action_controller/railtie"
require "active_record"
require "active_support/all"

class TestApp < Rails::Application
  config.root = __dir__
  config.hosts << "example.org"
  config.session_store :cookie_store, key: "cookie_store_key"
  secrets.secret_key_base = "secret_key_base"

  Kredis::Connections.connections[:shared] = Redis.new(
    url: ENV.fetch("REDIS_URL", "redis://localhost:6379/1")
  )

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger
end

# Required or AllFutures::Finder find method blows up at model.instance_variable_set "@created_at", Time.zone.parse(record["created_at"])
Time.zone = "Europe/London"

class TodoList < AllFutures::Base
  attribute :title, :text
  embeds_many :todo_items
  accepts_nested_attributes_for :todo_items, reject_if: :all_blank
end

class TodoItem < AllFutures::Base
  attribute :description, :text
end

require "minitest/autorun"

class BugTest < Minitest::Test
  
  # The parent TodoList persists as expected
  def test_that_parent_record_persists
    todo_list = TodoList.create(title: "Hey")
    
    assert todo_list.persisted?

    found = TodoList.find(todo_list.id)
    assert found.present?
    assert_equal "Hey", found.title 
  end

  def test_that_child_record_persists
    todo_list = TodoList.create(title: "Hey", todo_items: [{description: "Todo 1"}])

    assert todo_list.persisted?

    # The todo items list has been correctly cast to instances of TodoItem
    assert_equal TodoItem, todo_list.todo_items.first.class
    assert_equal "Todo 1", todo_list.todo_items.first.description

    found = TodoList.find(todo_list.id)
    assert found.present?

    # Expected to fail as the embed was not persisted
    assert_equal TodoItem, found.todo_items.first.class
  end
end

Please let me know if I can do anything else to assist. Thanks

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