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
Accessing attribute values from a hash rather than a reader method #113
Comments
You might be right that we need to switch in order to achieve least-surprise with people converting from ActiveRecord. For now you can workaround this surprising behavior by leveraging Ruby's standard method override behavior and calling |
@cgriego Thanks for the quick reply! Unfortunately calling Fortunately I just did a spike and because you've already implemented an I'll probably be able to submit a pull request in a few days. |
Without seeing your gem code it's hard to provide any guidance, but ActiveAttr does not create the accessors directly on the model class, it uses an anonymous module. That's how defining an override directly on the model and calling super can work. If you gem is not defining overrides on the model (which would work I would think) then maybe it too can use the anonymous module technique if dynamic, or if the overrides are static then just a plain module. |
@cgriego you just taught me something new. That's how my gems should be doing things as well. :) Thank you. Nevertheless, as long as you are ok with it, I'll get this pull request to you. I still feel like accessing the attributes hash is a slightly better implementation. On a side note, have you looked into actually modifying Rails to have some of this modularity? |
I am still interested in a pull request that changes ActiveAttr to work in a less surprising way. By modularity, do you mean the use of the anonymous module or the way that ActiveAttr is broken into modules? Rails actually uses the anonymous module technique, ActiveAttr is leveraging the same code that ActiveRecord uses, ActiveModel::AttributeMethods. That may be useful for your gem. |
Clearing out my "open" issues. This is obviously never going to happen. |
Currently I have a couple gems which utilize low level methods on A/R to make working with dates and times easier.
I override the high level readers/writers and use lower-level write/read_attribute so that I don't get stuck in a 'stack too deep' conundrum.
Once I found ActiveAttr, I was extremely happy because I had a few models which I had inherited from A/R::Base and all the baggage that comes with that and I was looking forward to using ActiveAttr instead.
Unfortunately, because ActiveAttr doesn't follow A/R's model more closely, my gem integrations break. Specifically, the problem is:
The reasoning behind this (as I'm sure you know) is that ActiveAttr delegates to the accessor method when
read_attribute
is called. The fallacy in this approach (in my mind) is that the top level accessor methods are the most likely to be overridden for different functionality. And in A/R it is expected that as long as you call read/write_attribute at the end of your overridden reader/writer, all DB functionality will work as expected.I feel like the correct approach to be compatible with gems which are using lower-level A/R would be:
Thoughts?
The text was updated successfully, but these errors were encountered: