diff --git a/app/models/custom_attribute.rb b/app/models/custom_attribute.rb index 927694cde15..5f4e33b0f14 100644 --- a/app/models/custom_attribute.rb +++ b/app/models/custom_attribute.rb @@ -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 diff --git a/lib/miq_expression.rb b/lib/miq_expression.rb index f9d0ce5dc11..cdbf0ad851b 100644 --- a/lib/miq_expression.rb +++ b/lib/miq_expression.rb @@ -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 diff --git a/lib/miq_expression/field.rb b/lib/miq_expression/field.rb index a1a6d5e4cb2..ccb3a74d2fa 100644 --- a/lib/miq_expression/field.rb +++ b/lib/miq_expression/field.rb @@ -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| @@ -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 diff --git a/spec/lib/miq_expression_spec.rb b/spec/lib/miq_expression_spec.rb index 5fbcd9aff0e..7504b15db21 100644 --- a/spec/lib/miq_expression_spec.rb +++ b/spec/lib/miq_expression_spec.rb @@ -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) diff --git a/spec/models/custom_attribute_spec.rb b/spec/models/custom_attribute_spec.rb new file mode 100644 index 00000000000..6ad97d3a680 --- /dev/null +++ b/spec/models/custom_attribute_spec.rb @@ -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