Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast virtual attribute 'Hardware#ram_size_in_bytes' to bigint #15554

Merged
merged 2 commits into from
Jul 13, 2017
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
6 changes: 5 additions & 1 deletion app/models/hardware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Hardware < ApplicationRecord

virtual_aggregate :used_disk_storage, :disks, :sum, :used_disk_storage
virtual_aggregate :allocated_disk_storage, :disks, :sum, :size
virtual_attribute :ram_size_in_bytes, :integer, :arel => ->(t) { t.grouping(t[:memory_mb] * 1.megabyte) }

def ipaddresses
@ipaddresses ||= networks.collect(&:ipaddress).compact.uniq + networks.collect(&:ipv6address).compact.uniq
Expand All @@ -44,6 +43,11 @@ def mac_addresses
def ram_size_in_bytes
memory_mb.to_i * 1.megabyte
end
# resulting sql: "(CAST("hardwares"."memory_mb" AS bigint) * 1048576)"
virtual_attribute :ram_size_in_bytes, :integer, :arel => (lambda do |t|
t.grouping(Arel::Nodes::Multiplication.new(Arel::Nodes::NamedFunction.new("CAST", [t[:memory_mb].as("bigint")]),
1.megabyte))
end)

@@dh = {"type" => "device_name", "devicetype" => "device_type", "id" => "location", "present" => "present",
"filename" => "filename", "startconnected" => "start_connected", "autodetect" => "auto_detect", "mode" => "mode",
Expand Down
5 changes: 5 additions & 0 deletions spec/models/hardware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@

expect(virtual_column_sql_value(Hardware, "ram_size_in_bytes")).to eq(5.megabytes)
end

it "does not raise error if return value bigger than PostgreSQL's integer type" do
FactoryGirl.create(:hardware, :memory_mb => 131_015)
expect { virtual_column_sql_value(Hardware, "ram_size_in_bytes") }.to_not raise_error
end
end

# this is disks + ram_size_in_bytes
Expand Down