Skip to content

Commit

Permalink
adding detection of custom attributes field type in miq expression
Browse files Browse the repository at this point in the history
  • Loading branch information
alongoldboim committed Sep 27, 2016
1 parent 76ae3d7 commit 4687394
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app/models/custom_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ class CustomAttribute < ApplicationRecord
belongs_to :resource, :polymorphic => true
serialize :serialized_value

def value=(value)
self.serialized_value = value
self[:value] = value
end

def stored_on_provider?
source == "VC"
end

def value_type
serialized_value ? serialized_value.class.to_s.downcase.to_sym : :string
end
end
1 change: 1 addition & 0 deletions lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,7 @@ def self.get_col_type(field)

model = determine_model(model, parts)
return nil if model.nil?
return Field.parse(field).column_type if col.include?(CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX)

col_type(model, col)
end
Expand Down
16 changes: 12 additions & 4 deletions lib/miq_expression/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,20 @@ def contains(other)
)
end

def column_type
if custom_attribute_column?
CustomAttribute.where(:name => custom_attribute_column_name, :resource_type => model.to_s).first.try(:value_type)
else
target.type_for_attribute(column).type
end
end

private

def custom_attribute_column_name
column.gsub(CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX, "")
end

class WhereExtractionVisitor < Arel::Visitors::PostgreSQL
def visit_Arel_Nodes_SelectStatement(o, collector)
collector = o.cores.inject(collector) do |c, x|
Expand Down Expand Up @@ -127,8 +139,4 @@ def extract_where_values(klass, scope)
def arel_attribute
target.arel_attribute(column)
end

def column_type
target.type_for_attribute(column).type
end
end
24 changes: 24 additions & 0 deletions spec/lib/miq_expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1626,12 +1626,36 @@

describe ".get_col_type" do
subject { described_class.get_col_type(@field) }
let(:string_custom_attribute) do
FactoryGirl.create(:custom_attribute,
:name => "foo",
:value => "string",
:resource_type => 'ExtManagementSystem')
end
let(:date_custom_attribute) do
FactoryGirl.create(:custom_attribute,
:name => "foo",
:value => DateTime.current,
:resource_type => 'ExtManagementSystem')
end

it "with model-field__with_pivot_table_suffix" do
@field = "Vm-name__pv"
expect(subject).to eq(described_class.get_col_type("Vm-name"))
end

it "with custom attribute without value_type" do
string_custom_attribute
@field = "ExtManagementSystem-#{CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX}foo"
expect(subject).to eq(:string)
end

it "with custom attribute with value_type" do
date_custom_attribute
@field = "ExtManagementSystem-#{CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX}foo"
expect(subject).to eq(:datetime)
end

it "with managed-field" do
@field = "managed.location"
expect(subject).to eq(:string)
Expand Down
17 changes: 17 additions & 0 deletions spec/models/custom_attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe CustomAttribute do
let(:string_custom_attribute) { FactoryGirl.build(:custom_attribute, :name => "foo", :value => "string", :resource_type => 'ExtManagementSystem') }
let(:time_custom_attribute) { FactoryGirl.build(:custom_attribute, :name => "bar", :value => DateTime.current, :resource_type => 'ExtManagementSystem') }
let(:int_custom_attribute) { FactoryGirl.build(:custom_attribute, :name => "foobar", :value => 5, :resource_type => 'ExtManagementSystem') }

it "returns the value type of String custom attributes" do
expect(string_custom_attribute.value_type).to eq(:string)
end

it "returns the value type of DateTime custom attributes" do
expect(time_custom_attribute.value_type).to eq(:datetime)
end

it "returns the value type of Fixnum custom attributes" do
expect(int_custom_attribute.value_type).to eq(:fixnum)
end
end

0 comments on commit 4687394

Please sign in to comment.