docbrown/app/models/application_record.rb
Jeremy Friesen 3253ba2c7a
Patching ERB rendering of the data-info JSON (#16067)
Prior to this commit, we were somewhat naively rendering Hash style data
attributes in our ERB templates.  By rendering each hash attribute
separately, we were rendering characters that could break the
javascript (e.g. double hack or backslash `"` or `\`).

By moving to this view_object rendering, we leverage Rails's `to_json`
behavior to ensure properly escaped values.  As part of this exercise, I
generalized the method to allow for other places to benefit from this
behavior.

This generalization also helps ensure that we have a more conformant
rendering (e.g. we should always have an :id, :className, and :name
value in our data-info hash).

_Note: I've updated the user's names for Cypress tests as they are more
likely to catch the particular issue than anything else.  I assume that
I'm going to break some cypress tests and will need some help fixing
them._

Closes #15916, #14704

Supersedes #15983

How to test locally:

Assuming you have seeded database (e.g. `rails db:seed`), checkout the
"main" branch.  Then in `rails console` find a user that's written articles:

```ruby
user = Article.last.user

user.update(name: "\\: #{user.name}")

user.articles.each(&:save)
```

Now, again on the "main" branch, start your application (e.g.,
`bin/startup`).

Then get a logged in and a logged out browser session going.  Open your
web inspector and open console.  Then go to the local instances homepage
(e.g., http://localhost:3000) and look for JS errors.

On the main branch, you should see an exception around
`JSON.parse(button.data.info)` (assuming that the `user`'s article is
rendered on the homepage).

Then go to the user's page (e.g. https://localhost:3000/:user-slug) and
look for JS parse errors.

On this PR's branch (e.g.,
`jeremyf/take-two-at-resolving-gh-15916`)
you shouldn't see those console errors.

More importantly, the Follow buttons should work.
2022-01-14 08:30:49 -05:00

86 lines
2.8 KiB
Ruby

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
include Purgeable
# see <https://www.postgresql.org/docs/11/catalog-pg-class.html> for details
# on the `pg_class` table
QUERY_ESTIMATED_COUNT = <<~SQL.squish.freeze
SELECT (
(reltuples / GREATEST(relpages, 1)) *
(pg_relation_size(?) / (GREATEST(current_setting('block_size')::integer, 1)))
)::bigint AS count
FROM pg_class
WHERE relname = ? AND relkind = 'r';
SQL
# Computes an estimated count of the number of rows using stats collected by VACUUM
# inspired by <https://www.citusdata.com/blog/2016/10/12/count-performance/#dup_counts_estimated_full>
# and <https://stackoverflow.com/a/48391562/4186181>
def self.estimated_count
query = sanitize_sql_array([QUERY_ESTIMATED_COUNT, table_name, table_name])
result = connection.execute(query)
count = result.first["count"]
result.clear # PG::Result is manually managed in memory, we need to release its resources
count
end
# Decorate object with appropriate decorator
def decorate
self.class.decorator_class.new(self)
end
def decorated?
false
end
# In our view objects, we often ask "What's this object's class's name?"
#
# We can either first check "Are you decorated?" If so, ask for the decorated object's class
# name. Or we can add a helper method for that very thing.
#
# @return [String]
def class_name
self.class.name
end
# Decorate collection with appropriate decorator
def self.decorate
decorator_class.decorate_collection(all)
end
# Infers the decorator class to be used by (e.g. `User` maps to `UserDecorator`).
# adapted from https://github.com/drapergem/draper/blob/157eb955072a941e6455e0121fca09a989fcbc21/lib/draper/decoratable.rb#L71
def self.decorator_class(called_on = self)
prefix = respond_to?(:model_name) ? model_name : name
decorator_name = "#{prefix}Decorator"
decorator_name_constant = decorator_name.safe_constantize
return decorator_name_constant unless decorator_name_constant.nil?
return superclass.decorator_class(called_on) if superclass.respond_to?(:decorator_class)
raise UninferrableDecoratorError, "Could not infer a decorator for #{called_on.class.name}."
end
def self.statement_timeout
connection.execute("SELECT setting FROM pg_settings WHERE name = 'statement_timeout'")
.first["setting"]
.to_i
.seconds / 1000
end
def self.with_statement_timeout(duration, connection: self.connection)
original_timeout = statement_timeout
milliseconds = (duration.to_f * 1000).to_i
connection.execute "SET statement_timeout = #{milliseconds}"
yield
ensure
milliseconds = original_timeout.to_i * 1000
connection.execute "SET statement_timeout = #{milliseconds}"
end
def errors_as_sentence
errors.full_messages.to_sentence
end
end