{"action":"closed","issue":{"url":"https://api.github.com/repos/abitdodgy/abitdodgy.github.io/issues/1","repository_url":"https://api.github.com/repos/abitdodgy/abitdodgy.github.io","labels_url":"https://api.github.com/repos/abitdodgy/abitdodgy.github.io/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/abitdodgy/abitdodgy.github.io/issues/1/comments","events_url":"https://api.github.com/repos/abitdodgy/abitdodgy.github.io/issues/1/events","html_url":"https://github.com/abitdodgy/abitdodgy.github.io/issues/1","id":132128620,"number":1,"title":"How to avoid using callbacks","user":{"login":"abitdodgy","id":545430,"avatar_url":"https://avatars.githubusercontent.com/u/545430?v=3","gravatar_id":"","url":"https://api.github.com/users/abitdodgy","html_url":"https://github.com/abitdodgy","followers_url":"https://api.github.com/users/abitdodgy/followers","following_url":"https://api.github.com/users/abitdodgy/following{/other_user}","gists_url":"https://api.github.com/users/abitdodgy/gists{/gist_id}","starred_url":"https://api.github.com/users/abitdodgy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abitdodgy/subscriptions","organizations_url":"https://api.github.com/users/abitdodgy/orgs","repos_url":"https://api.github.com/users/abitdodgy/repos","events_url":"https://api.github.com/users/abitdodgy/events{/privacy}","received_events_url":"https://api.github.com/users/abitdodgy/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/abitdodgy/abitdodgy.github.io/labels/blog-post-idea","name":"blog-post-idea","color":"fad8c7"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-02-08T12:25:51Z","updated_at":"2016-03-18T14:27:52Z","closed_at":"2016-03-18T14:27:52Z","body":"This is an idea for a blog post about callbacks. It should discuss:\r\n\r\n1. Why are callbacks usually a bad idea.\r\n2. Techniques to avoid using callbacks, using:\r\n  1. Using custom crud methods\r\n  2. Using service objects\r\n  3. Using custom setter and getter methods.\r\n3. When using callbacks is acceptable.\r\n\r\n### Why are callbacks a bad idea?\r\n\r\nThey have unintended consequences. Show examples of how they've complicated our lives, especially when building the activity feed for bulk actions.\r\n\r\n### Techniques to avoid using callbacks\r\n\r\nUsing custom crud methods we can replace:\r\n\r\n```ruby\r\nclass Order\r\n  has_many :line_items\r\n\r\n  before_create :set_total!\r\n\r\nprivate\r\n\r\n  def set_total!\r\n    self.total = line_items.sum(:price)\r\n  end\r\nend\r\n```\r\n\r\nWith...\r\n\r\n```ruby\r\nclass Person\r\n  has_many :line_items\r\n\r\n  def dispatch!\r\n    set_total! and save\r\n  end\r\n\r\nprivate\r\n\r\n  def set_total!\r\n    self.total = line_items.sum(:price)\r\n  end\r\nend\r\n```\r\n\r\n### When using callbacks is acceptable\r\n\r\nAre there times when we can safely use callbacks? When and why? Why is it better than any of the previous techniques?\r\n \r\n"}}