-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |