Skip to content

Commit

Permalink
Add ObjectStore::Item model
Browse files Browse the repository at this point in the history
This inherits the edition model and sets a few defaults to
override some Edition defaults. It also contains an
`add_field_accessors` callback that reads the configuration for a
given item_type and sets the relevant variables using
[`store_accessor`][1]. Additionally, if the field is required, we set
validation on the object too.

[1]: https://api.rubyonrails.org/classes/ActiveRecord/Store.html
  • Loading branch information
pezholio committed Jun 4, 2024
1 parent 0156a1d commit 252dcf6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
33 changes: 33 additions & 0 deletions app/models/object_store/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module ObjectStore
class Item < Edition
store_accessor :details, :item_type

after_initialize :add_field_accessors

def summary_required?
false
end

def body_required?
false
end

def previously_published
false
end

private

def add_field_accessors
properties = ObjectStore.fields_for_item_type(item_type)
properties.each_key do |k|
singleton_class.class_eval { store_accessor :details, k }
if ObjectStore.field_is_required?(item_type, k)
singleton_class.class_eval { validates k, presence: true }
end
end
rescue UnknownItemType
# Ignored
end
end
end
2 changes: 1 addition & 1 deletion test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ def image_fixture_file
@image_fixture_file ||= File.open(Rails.root.join("test/fixtures/minister-of-funk.960x640.jpg"))
end

Dir[Rails.root.join("test/factories/*.rb")].sort.each { |f| require f }
Dir[Rails.root.join("test/factories/**/*.rb")].sort.each { |f| require f }
11 changes: 11 additions & 0 deletions test/factories/object_store/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FactoryBot.define do
factory :object_store_item, class: ObjectStore::Item, parent: :edition do
transient do
item_type { "email_address" }
end

title { "object-store-item" }

initialize_with { ObjectStore::Item.new(item_type:) }
end
end
30 changes: 30 additions & 0 deletions test/unit/app/models/object_store/item_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "test_helper"

class ObjectStore::ItemTest < ActiveSupport::TestCase
setup do
@item = build(:object_store_item, item_type: "email_address")
end

test "it creates accessors for an email address" do
@item.email_address = "[email protected]"

assert_equal @item.email_address, "[email protected]"
end

test "it validates the presence of an email address" do
assert_not @item.valid?
assert @item.errors[:email_address].any?
end

test "#summary_required? returns false" do
assert_equal @item.summary_required?, false
end

test "#body_required? returns false" do
assert_equal @item.body_required?, false
end

test "#previously_published returns false" do
assert_equal @item.previously_published, false
end
end

0 comments on commit 252dcf6

Please sign in to comment.