X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/c8060b94c485d5bf0e500d2321793cd56db4c856..e37caee5e3cfc165c6505ea5f3b55a4b8b07fe5e:/services/api/app/models/arvados_model.rb diff --git a/services/api/app/models/arvados_model.rb b/services/api/app/models/arvados_model.rb index c89efdf404..731eecf838 100644 --- a/services/api/app/models/arvados_model.rb +++ b/services/api/app/models/arvados_model.rb @@ -8,11 +8,15 @@ class ArvadosModel < ActiveRecord::Base attr_protected :modified_by_user_uuid attr_protected :modified_by_client_uuid attr_protected :modified_at + after_initialize :log_start_state before_create :ensure_permission_to_create before_update :ensure_permission_to_update before_destroy :ensure_permission_to_destroy before_create :update_modified_by_fields before_update :maybe_update_modified_by_fields + around_create { |&block| make_log_around(:create, nil, self, &block) } + around_update { |&block| make_log_around(:update, self, self, &block) } + around_destroy { |&block| make_log_around(:destroy, self, nil, &block) } validate :ensure_serialized_attribute_type validate :normalize_collection_uuids @@ -38,14 +42,23 @@ class ArvadosModel < ActiveRecord::Base "#{current_api_base}/#{self.class.to_s.pluralize.underscore}/#{self.uuid}" end - def self.searchable_columns + def self.searchable_columns operator + textonly_operator = !operator.match(/[<=>]/) self.columns.collect do |col| - if [:string, :text].index(col.type) && col.name != 'owner_uuid' + if col.name == 'owner_uuid' + nil + elsif [:string, :text].index(col.type) + col.name + elsif !textonly_operator and [:datetime, :integer].index(col.type) col.name end end.compact end + def self.attribute_column attr + self.columns.select { |col| col.name == attr.to_s }.first + end + def eager_load_associations self.class.columns.each do |col| re = col.name.match /^(.*)_kind$/ @@ -201,4 +214,24 @@ class ArvadosModel < ActiveRecord::Base nil end + def log_start_state + @old_etag = etag + @old_attributes = attributes + end + + def make_log_around(event_type, old_thing, new_thing) + if self.is_a? Log + yield + else + log = Log.start_from(old_thing, event_type.to_s) + if not old_thing.nil? + log.properties['old_etag'] = @old_etag + log.properties['old_attributes'] = @old_attributes + end + yield + log.update_to new_thing + log_start_state + log.save + end + end end