-
Notifications
You must be signed in to change notification settings - Fork 897
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
Changes custom_attribute virtual_attributes to support AREL/SQL #17615
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,19 +41,41 @@ def self.load_custom_attributes_for(cols) | |
def self.add_custom_attribute(custom_attribute) | ||
return if respond_to?(custom_attribute) | ||
|
||
virtual_column(custom_attribute.to_sym, :type => :string, :uses => :custom_attributes) | ||
ca_sym = custom_attribute.to_sym | ||
without_prefix = custom_attribute.sub(CUSTOM_ATTRIBUTES_PREFIX, "") | ||
name_val, section = without_prefix.split(SECTION_SEPARATOR) | ||
sanatized_column_alias = custom_attribute.tr('.', 'DOT').tr('/', 'BS').tr(':', 'CLN') | ||
ca_arel = custom_attribute_arel(name_val, section, sanatized_column_alias) | ||
|
||
define_method(custom_attribute.to_sym) do | ||
custom_attribute_without_prefix = custom_attribute.sub(CUSTOM_ATTRIBUTES_PREFIX, "") | ||
custom_attribute_without_section, section = custom_attribute_without_prefix.split(SECTION_SEPARATOR) | ||
virtual_column(ca_sym, :type => :string, :uses => :custom_attributes, :arel => ca_arel) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the trend is to always define the uses even with arel (as you have here) (speculation here) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we can't remove the The The The call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding |
||
|
||
where_args = {} | ||
where_args[:name] = custom_attribute_without_section | ||
define_method(ca_sym) do | ||
return self[sanatized_column_alias] if has_attribute?(sanatized_column_alias) | ||
|
||
where_args = {} | ||
where_args[:name] = name_val | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? where_args = {:name => name_val} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't write this originally, just updated the variable name. Just left it the same as it was. |
||
where_args[:section] = section if section | ||
|
||
custom_attributes.find_by(where_args).try(:value) | ||
end | ||
end | ||
|
||
def self.custom_attribute_arel(name_val, section, column_alias) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nicely done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😊 |
||
lambda do |t| | ||
ca_field = CustomAttribute.arel_table | ||
|
||
field_where = ca_field[:resource_id].eq(t[:id]) | ||
field_where = field_where.and(ca_field[:resource_type].eq(base_class.name)) | ||
field_where = field_where.and(ca_field[:name].eq(name_val)) | ||
field_where = field_where.and(ca_field[:section].eq(section)) if section | ||
|
||
# Because there is a `find_by` in the `define_method` above, we are | ||
# using a `take(1)` here as well, since a limit is assumed in each. | ||
# Without it, there can be some invalid queries if more than one result | ||
# is returned. | ||
ca_field.project(:value).where(field_where).take(1).as(column_alias) | ||
end | ||
end | ||
end | ||
|
||
def self.to_human(column) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to understand this more:
This change adds
remove_loading_relations_for_virtual_custom_attributes
A (optionally).build_table
always callsremove_loading_relations_for_virtual_custom_attributes
.generate_*
methods?build_table
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly, though not the priority for this BZ. Should function still without it at the moment, and I think the way we gather results for reports is vastly different in each of those methods, so I think waiting to do that makes sense for this PR.
No, this should be idempotent to remove the value from the
include
hash. It is not saving the record with it removed anyway, so we are just removing it earlier than originally planed.