-
Notifications
You must be signed in to change notification settings - Fork 121
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
Bad examples in README #2
Comments
I'm fine with the examples. It was extracted from Rails because it is something that we don't use in our applications and because we don't want to encourage it as best practice, not because people use it wrong. Either in your example I would not encourage the usage. It hides logic adding an another level of indirection. |
I would agree that sending an email from the observer is not the best use-case but as @rafaelfranca stated the example with the tags is not really a good abstraction either. Let's look for a better example to put into the README. |
I don't have any better idea, but we are open to suggestions. |
My mistake, then. Most blog posts talking about this say things along the lines of "they could be useful, but most people just put stuff in there that really shouldn't belong in them". I assumes that was one of the reasons. :-)
Well, you either choose indirection or coupling. That's a tradeoff one must pick. It's like Law of Demeter vs. duplication.
I agree with that too. Tags may not be the best example, but it's miles better than the email sending. That's just wrong on so many levels. I personally like the second example I presented the most since it's the most common, and most usable case for observers - denormalization. When using ORMs for databases that does not work with relational data, that is something that is present in pretty much all models and the coupling is the reverse (e.g. saving a Here's another one: class ThreadObserver < ActiveRecord::Observer
def after_save(thread)
User.update_watched_threads(thread)
end
def after_destroy(thread)
User.remove_watched_threads(thread)
end
end
class User
def self.update_watched_threads(thread)
denormalized_attributes = User::DenormalizedThreadSerializer.new(thread).attributes
watching_thread(thread).each do |user|
user.watched_threads.find(thread.id).update_attributes(denormalized_attributes)
end
end
def self.remove_watched_threads(thread)
watching_thread(thread).each do |user|
user.watched_threads.pull(thread.id)
end
end
end (Breaking LoD to keep example short. Not the best code; I would structure it to use atomic operations rather than iterating users.) This example makes sure that if a thread has its name (or whatever, this code doesn't know) changed, the denormalized documents in It's not proper for The solution is to have a layer between the models that solve this problem. They lie in the persistence part of the codebase, and their only responsibility is to make sure that the persistence models keep their contracts intact. |
For me, after a bad experience on this gem, I give up to try to use it. Thanks for the bad example and release without test. If you think it not good for use, just delete it. If you put it here and release on rubygems.org, you should make sure it can be work. Otherwise it will make people waste lots of time on it. |
Sorry, but I'm using this gem in a project and it work fine. Before the release we also tested the gem either with the unit tests If it is not working for you, you could be more constructive and tell us
|
Sorry I did not test it on a new rails4 app. I just use it in my project which upgraded to rails4 form rails3.2. maybe it works in a new rails4 project. Sorry about that. Can you have a look at #4 , I don't think it was been fixed. |
Thank you for point the issue. I'll work to fix it. |
That's cool, it's the best problem for me. Thanks a lot! |
They was extracted from a plugin. See https://github.com/rails/rails-observers [Rafael Mendonça França + Steve Klabnik]
So this was extracted from Rails since most people used observers wrong. Don't you think that examples that uses them in a bad way might invite people to make those mistakes again and again?
Observers should work with persistence, with concerns that shouldn't really be directly in the model. Here's some examples:
Sure, these might still not be optimal, but they are better than an example that sends an email from the persistence layer, every time a model is persisted. Thoughts?
The text was updated successfully, but these errors were encountered: