Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .ameba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,17 @@ Style/PredicateName:
Style/QueryBoolMethods:
Enabled: false

Style/RedundantSelf:
Enabled: false

Style/HeredocIndent:
Enabled: false

Style/PercentLiteralDelimiters:
Enabled: false

Style/MultilineStringLiteral:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false
74 changes: 73 additions & 1 deletion spec/control_system_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ module PlaceOS::Model
end
end

describe "camera_snapshot_urls" do
describe "url cleanup" do
it "setting camera_snapshot_urls works" do
control_system = Generator.control_system
control_system.camera_snapshot_urls = ["https://placeos.com/"]
Expand All @@ -184,6 +184,78 @@ module PlaceOS::Model

control_system.destroy
end

it "strips blank strings from camera_snapshot_urls on save" do
control_system = Generator.control_system
control_system.camera_snapshot_urls = [""]
control_system.save!
control_system.reload!
control_system.camera_snapshot_urls.should eq([] of String)

control_system.camera_snapshot_urls = ["", ""]
control_system.save!
control_system.reload!
control_system.camera_snapshot_urls.should eq([] of String)

control_system.camera_snapshot_urls = ["", "https://placeos.com/"]
control_system.save!
control_system.reload!
control_system.camera_snapshot_urls.should eq(["https://placeos.com/"])

control_system.camera_snapshot_urls = [" ", "\t", "https://placeos.com/"]
control_system.save!
control_system.reload!
control_system.camera_snapshot_urls.should eq(["https://placeos.com/"])

control_system.destroy
end

it "strips blank strings from images on save" do
control_system = Generator.control_system
control_system.images = ["", "https://placeos.com/image.png", " "]
control_system.save!
control_system.reload!
control_system.images.should eq(["https://placeos.com/image.png"])

control_system.images = ["", ""]
control_system.save!
control_system.reload!
control_system.images.should eq([] of String)

control_system.destroy
end

it "nils blank single url fields on save" do
control_system = Generator.control_system
control_system.support_url = " "
control_system.timetable_url = ""
control_system.camera_url = "\t"
control_system.room_booking_url = " "
control_system.save!
control_system.reload!
control_system.support_url.should eq("")
control_system.timetable_url.should be_nil
control_system.camera_url.should be_nil
control_system.room_booking_url.should be_nil

control_system.destroy
end

it "preserves valid urls" do
control_system = Generator.control_system
control_system.support_url = "https://support.placeos.com/"
control_system.timetable_url = "https://timetable.placeos.com/"
control_system.camera_url = "https://camera.placeos.com/"
control_system.room_booking_url = "https://booking.placeos.com/"
control_system.save!
control_system.reload!
control_system.support_url.should eq("https://support.placeos.com/")
control_system.timetable_url.should eq("https://timetable.placeos.com/")
control_system.camera_url.should eq("https://camera.placeos.com/")
control_system.room_booking_url.should eq("https://booking.placeos.com/")

control_system.destroy
end
end

describe "remove_module" do
Expand Down
37 changes: 26 additions & 11 deletions src/placeos-models/control_system.cr
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ module PlaceOS::Model
name.strip
end

# Validate support URI
# Validate URIs
validate ->(this : ControlSystem) {
this.camera_snapshot_urls.each do |snap_url|
next if snap_url.blank?
if !Validation.valid_uri?(snap_url)
this.validation_error(:camera_snapshot_urls, "contains an invalid URI")
break
Expand All @@ -138,13 +139,25 @@ module PlaceOS::Model
this.validation_error(:support_url, "is an invalid URI") unless Validation.valid_uri?(this.support_url)
}

before_save :unique_camera_urls
before_save :clean_urls

def unique_camera_urls
unique_urls = self.camera_snapshot_urls.uniq
if unique_urls.size != self.camera_snapshot_urls.size
self.camera_snapshot_urls = unique_urls
def clean_urls
# Strip blank entries from array URL fields
cleaned_snapshot_urls = self.camera_snapshot_urls.reject(&.blank?)
if cleaned_snapshot_urls.size != self.camera_snapshot_urls.size
self.camera_snapshot_urls = cleaned_snapshot_urls
end

cleaned_images = self.images.reject(&.blank?)
if cleaned_images.size != self.images.size
self.images = cleaned_images
end

# Blank single URL fields to their empty/nil defaults
self.support_url = "" if self.support_url.blank?
self.timetable_url = nil if self.timetable_url.try(&.blank?)
self.camera_url = nil if self.camera_url.try(&.blank?)
self.room_booking_url = nil if self.room_booking_url.try(&.blank?)
end

def camera_snapshot_urls=(vals : Array(String))
Expand Down Expand Up @@ -398,11 +411,13 @@ module PlaceOS::Model

Module.find?(module_id).try(&.destroy) unless still_in_use

Log.debug { {
message: "module removed from system #{still_in_use ? "still in use" : "deleted as not in any other systems"}",
module_id: module_id,
control_system_id: control_system_id,
} }
Log.debug do
{
message: "module removed from system #{still_in_use ? "still in use" : "deleted as not in any other systems"}",
module_id: module_id,
control_system_id: control_system_id,
}
end

true
end
Expand Down
Loading