Conversation
|
Note: The tricky part is that Mobility has already defined these accessor methods, so it can't easily delegate to method missing like AR does internally in this case. I'm not yet sure how to handle this. Most stuff works simply by using the "as " format, where |
|
|
||
| def as(*) | ||
| super | ||
| .extend(::Arel::Expressions) |
There was a problem hiding this comment.
This is necessary due to this line. We might not need it though.
d08348d to
b8d9cbd
Compare
| # TODO: Improve this. | ||
| def read(locale, **) | ||
| attr_set = model.instance_variable_get(:@attributes) | ||
| if attr_set && attr_set.key?(attribute) |
There was a problem hiding this comment.
This does not work right now, although it may pass tests. Aside from being an ugly hack, the problem is that @attributes may have a value for a key that is a translated attribute, but the value may not be the one we want to return here.
E.g. with the json backend, if your column name is the same as the name of the translated attribute, then you will have e.g. a value of {} for title (empty json hash). This patch to read here would return this value instead of nil (no translation for the current locale), which is obviously wrong.
I'm pretty stuck with this one. AR sets the value of the SELECT using AS in the corresponding key of @attributes, but this may be set for other reasons (as mentioned above) so its presence is not a reliable indicator that we should actually return it.
We could use a different name in the AS clause, which would make it unambiguous, but this would make combining with group and other things not work (I think)...
| # TODO: Improve this. | ||
| def read(locale, **) | ||
| attr_set = model.instance_variable_get(:@attributes) | ||
| if attr_set && attr_set.key?(attr_alias = Query.attribute_alias(attribute)) |
There was a problem hiding this comment.
I've switched to using an alias when selecting, so SELECT ... AS __mobility_title__. This avoids conflicts with other stuff in ActiveRecord, but still passes grouping specs.
I still don't like the instance_variable_get here...
4073bc7 to
a66ddd7
Compare
95d2bcf to
6621ecb
Compare
Fixes #275