Skip to content

Commit c5aa15b

Browse files
committed
feat(history): add action field
1 parent 4411cb0 commit c5aa15b

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

migration/db/migrations/20251204120500000_add_history_table.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS "history" (
55
id TEXT NOT NULL PRIMARY KEY,
66
type TEXT NOT NULL,
77
resource_id TEXT NOT NULL,
8+
action TEXT NOT NULL,
89
changed_fields TEXT[] NOT NULL,
910
created_at TIMESTAMPTZ NOT NULL,
1011
updated_at TIMESTAMPTZ NOT NULL

spec/generator.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,11 +745,13 @@ module PlaceOS::Model
745745
def self.history(
746746
type : String = "zone",
747747
resource_id : String = "zone-#{RANDOM.hex(4)}",
748+
action : String = "update",
748749
changed_fields : Array(String) = ["name", "description"],
749750
)
750751
History.new(
751752
type: type,
752753
resource_id: resource_id,
754+
action: action,
753755
changed_fields: changed_fields
754756
)
755757
end

spec/history_spec.cr

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ module PlaceOS::Model
1919
found.id.should eq history.id
2020
found.type.should eq history.type
2121
found.resource_id.should eq history.resource_id
22+
found.action.should eq history.action
2223
found.changed_fields.should eq history.changed_fields
2324
end
2425

2526
it "requires type" do
2627
history = History.new(
2728
type: "",
28-
resource_id: "zone-123"
29+
resource_id: "zone-123",
30+
action: "update"
2931
)
3032
history.valid?.should be_false
3133
history.errors.first.field.should eq :type
@@ -34,7 +36,8 @@ module PlaceOS::Model
3436
it "requires resource_id" do
3537
history = History.new(
3638
type: "zone",
37-
resource_id: ""
39+
resource_id: "",
40+
action: "update"
3841
)
3942
history.valid?.should be_false
4043
history.errors.first.field.should eq :resource_id
@@ -43,13 +46,39 @@ module PlaceOS::Model
4346
it "defaults changed_fields to empty array" do
4447
history = History.new(
4548
type: "zone",
46-
resource_id: "zone-123"
49+
resource_id: "zone-123",
50+
action: "create"
4751
)
4852
history.save!
4953

5054
history.changed_fields.should eq [] of String
5155
end
5256

57+
it "saves action field" do
58+
history = History.new(
59+
type: "zone",
60+
resource_id: "zone-123",
61+
action: "update",
62+
changed_fields: ["name"]
63+
)
64+
history.save!
65+
66+
found = History.find!(history.id.as(String))
67+
found.action.should eq "update"
68+
end
69+
70+
it "supports different action types" do
71+
["create", "update", "delete"].each do |action|
72+
history = History.new(
73+
type: "zone",
74+
resource_id: "zone-123",
75+
action: action
76+
)
77+
history.save!
78+
history.action.should eq action
79+
end
80+
end
81+
5382
it "sets timestamps on create" do
5483
history = Generator.history.save!
5584

src/placeos-models/history.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ module PlaceOS::Model
88

99
attribute type : String, es_subfield: "keyword"
1010
attribute resource_id : String, es_subfield: "keyword"
11+
attribute action : String, es_subfield: "keyword"
1112
attribute changed_fields : Array(String) = [] of String, es_type: "keyword"
1213

1314
# Validation
1415
###############################################################################################
1516

1617
validates :type, presence: true
1718
validates :resource_id, presence: true
19+
validates :action, presence: true
1820
end
1921
end

0 commit comments

Comments
 (0)