-
Notifications
You must be signed in to change notification settings - Fork 29
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
Define new hooks for ActiveRecord::Reflection::AssociationReflection #1
Conversation
@ronen Finally got postgresql/ruby2.2/AR4.2 tested on Windows. Not the full matrix, but at least it's some testing. |
@ronen Any comments on this? I've no clue why coverage goes down on those, I didn't remove anything... |
@lowjoel [sorry for the slow response, my (limited) schema_plus time has been mostly taken up by a small flurry of bug reports, hadn't had a chance to look into this until now.] I'm concerned that the stack is being wrapped around an undocumented internal method -- and given where that method is called, I think for your inverse-checking gem you want to be able to hook into the declaration of associations (rather than, say, looking up associations via def belongs_to(name, scope=nil, options={})
SchemaMonkey::Middleware::Model::Association::Declare.start model:self, name: name, scope: scope, options: options, type: :belongs_to do |env|
super env.name, env.scope, env.options
end
end and similarly for the others. Given that, for your inverse-checking gem, I think you'd use an def after(env)
return if env.type == :has_and_belongs_to_many # no :inverse_of needed for HABTM
reflection = env.model.reflections[env.name]
# ...rest of your logic here..
end Does this make sense? |
Good idea. That was the callsite I identified needing hooking, so that was the first thing I came up with. I'll change it. |
@ronen updated, how does this look? I'll port my gem over now. |
@lowjoel for the stack to allow middleware to examine (and potentially modify) the arguments, they need to be captured in the def belongs_to(name, scope=nil, options={}) # duplicates the signature of the overrided method
SchemaMonkey::Middleware::Model::Association::Declare.start model:self, name: name, scope: scope, options: options, type: :belongs_to do |env| # captures model and all args in env
super env.name, env.scope, env.options # call implementation using values from env (potentially modified by middleware)
end
end |
Yeah I realised after pushing that self isn't captured. Spent a lot of time messing around with gemfiles |
The problem I foresee is that across different versions of Rails, the parameter list might change and the gem will break for some versions but not others. Declaring the list of parameters in the environment isn't sustainable... I'm fixing the PR now. |
1 similar comment
@ronen pushed. I'm not sure if you're going to bump the gem to 0.3 |
@lowjoel good catch adding Care to update the README too? Re parameter list changing across different versions of AR: Yeah, not much can be done about that -- we need to expose all the args to the middleware in the stack. As versions change, schema_plus_core is going to accumulate version-specific conditionals and other cruft, (just as schema_plus 1.x did). But at least all that ugliness will (hopefully) be hidden in schema_plus_core so all the other gems would stay cleaner. And yes, this will be a minor version bump. |
Pushed, have a look. |
@lowjoel Did you mean to push something new? |
crap, push was rejected, sorry. really pushed now. |
1 similar comment
Cool, thanks. Merging. |
Define new hooks for ActiveRecord::Reflection::AssociationReflection
...and I've released 0.3.0 |
Needed for me to do reflection on reflection properties.
I've not managed to run the tests yet. The tests here are complete stabs in the dark...