Skip to content
Snippets Groups Projects
Commit 2ef9cc4c authored by Ryan Senior's avatar Ryan Senior
Browse files

Clear dimension data when the special_type is not supported

If a field is set to an enum or category type and has a remapping,
that should be cleared if the field moves to a special_type that is
not allowed to have remappings (i.e. text). This commit adds checks
for that and clears the dimension in that case.
parent 230ccc49
No related branches found
No related tags found
No related merge requests found
......@@ -40,6 +40,23 @@
(or (nil? new-special-type)
(not (isa? new-special-type :type/FK)))))
(defn- internal-remapping-allowed? [base-type special-type]
(and (isa? base-type :type/Integer)
(or
(nil? special-type)
(isa? special-type :type/Category)
(isa? special-type :type/Enum))))
(defn- clear-dimension-on-type-change!
"Removes a related dimension if the field is moving to a type that
does not support remapping"
[{{old-dim-id :id, old-dim-type :type} :dimensions, :as old-field} base-type new-special-type]
(when (and old-dim-id
(= :internal old-dim-type)
(not (internal-remapping-allowed? base-type new-special-type)))
(db/delete! Dimension :id old-dim-id))
true)
(api/defendpoint PUT "/:id"
"Update `Field` with ID."
[id :as {{:keys [caveats description display_name fk_target_field_id points_of_interest special_type visibility_type], :as body} :body}]
......@@ -67,6 +84,7 @@
(if removed-fk?
(clear-dimension-on-fk-change! field)
true)
(clear-dimension-on-type-change! field (:base_type field) new-special-type)
(db/update! Field id
(u/select-keys-when (assoc body :fk_target_field_id (when-not removed-fk? fk-target-field-id))
:present #{:caveats :description :fk_target_field_id :points_of_interest :special_type :visibility_type}
......
......@@ -501,3 +501,38 @@
after-change (simple-field-details (Field field-id-2))]
[(tu/boolean-ids-and-timestamps before-change)
(tu/boolean-ids-and-timestamps after-change)])))
;; Changing a remapped field's type to something that can't be remapped will clear the dimension
(expect
[{:id true
:created_at true
:updated_at true
:type :internal
:name "some dimension name"
:human_readable_field_id false
:field_id true}
[]]
(tt/with-temp* [Field [{field-id :id} {:name "Field Test"
:base_type "type/Integer"}]]
(dimension-post field-id {:name "some dimension name", :type "internal"})
(let [new-dim (dimension-for-field field-id)]
((user->client :crowberto) :put 200 (format "field/%d" field-id) {:special_type "type/Text"})
[(tu/boolean-ids-and-timestamps new-dim)
(dimension-for-field field-id)])))
;; Change from supported type to supported type will leave the dimension
(expect
(repeat 2 {:id true
:created_at true
:updated_at true
:type :internal
:name "some dimension name"
:human_readable_field_id false
:field_id true})
(tt/with-temp* [Field [{field-id :id} {:name "Field Test"
:base_type "type/Integer"}]]
(dimension-post field-id {:name "some dimension name", :type "internal"})
(let [new-dim (dimension-for-field field-id)]
((user->client :crowberto) :put 200 (format "field/%d" field-id) {:special_type "type/Category"})
[(tu/boolean-ids-and-timestamps new-dim)
(tu/boolean-ids-and-timestamps (dimension-for-field field-id))])))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment