Commit ea12c5aa authored by Patrick Bajao's avatar Patrick Bajao

Cleanup #attributes method

Since we're prepending the ActiveRecord::Extension module, we
can take advantage of it and avoid using an alias to extend the
original #attributes method.
parent 93dd5390
...@@ -7,31 +7,25 @@ module Gitlab ...@@ -7,31 +7,25 @@ module Gitlab
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
# Using before_update here conflicts with elasticsearch-model somehow
before_create :refresh_markdown_cache, if: :invalidated_markdown_cache?
before_update :refresh_markdown_cache, if: :invalidated_markdown_cache?
end
# Always exclude _html fields from attributes (including serialization). # Always exclude _html fields from attributes (including serialization).
# They contain unredacted HTML, which would be a security issue # They contain unredacted HTML, which would be a security issue
alias_method :attributes_before_markdown_cache, :attributes
def attributes def attributes
attrs = attributes_before_markdown_cache attrs = super
html_fields = cached_markdown_fields.html_fields html_fields = cached_markdown_fields.html_fields
whitelisted = cached_markdown_fields.html_fields_whitelisted whitelisted = cached_markdown_fields.html_fields_whitelisted
exclude_fields = html_fields - whitelisted exclude_fields = html_fields - whitelisted
exclude_fields.each do |field| attrs.except!(*exclude_fields)
attrs.delete(field) attrs.delete('cached_markdown_version') if whitelisted.empty?
end
if whitelisted.empty?
attrs.delete('cached_markdown_version')
end
attrs attrs
end end
# Using before_update here conflicts with elasticsearch-model somehow
before_create :refresh_markdown_cache, if: :invalidated_markdown_cache?
before_update :refresh_markdown_cache, if: :invalidated_markdown_cache?
end
def changed_markdown_fields def changed_markdown_fields
changed_attributes.keys.map(&:to_s) & cached_markdown_fields.markdown_fields.map(&:to_s) changed_attributes.keys.map(&:to_s) & cached_markdown_fields.markdown_fields.map(&:to_s)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment