Code ownership for Rails
🍊 Battle-tested at Instacart
Add this line to your application’s Gemfile:
gem 'ownership'
Ownership provides the ability to specify owners for different parts of the codebase. We highly recommend owners are teams rather than individuals. You can then use this information however you’d like, like routing errors to the correct team.
class OrdersController < ApplicationController
owner :logistics
end
You can use any options that before_action
supports.
class OrdersController < ApplicationController
owner :logistics, only: [:index]
owner :customers, except: [:index]
end
class SomeJob < ApplicationJob
owner :logistics
end
owner :logistics do
# code
end
You can set a default owner with:
Ownership.default_owner = :logistics
There are a few built-in integrations with other gems.
Marginalia adds comments to ActiveRecord queries. If installed, the owner is added.
SELECT ...
/*application:MyApp,controller:posts,action:index,owner:logistics*/
This can be useful when looking at the most time-consuming queries on your database.
Rollbar tracks exceptions. This integration makes it easy to send exceptions to different projects based on the owner. We recommend having a project for each team.
Ownership::Rollbar.access_token = {
logistics: "token1",
customers: "token2"
}
Also works with a proc
Ownership::Rollbar.access_token = -> (owner) { ENV["#{owner.to_s.upcase}_ROLLBAR_ACCESS_TOKEN"] }
You can define a custom block of code to run with:
Ownership.around_change = proc do |owner, block|
puts "New owner: #{owner}"
block.call
puts "Done"
end
Please don’t hesitate to submit a pull request if you create an integration that others can use.
Exceptions that bubble up from an owner
block have the owner, which your exception reporting library can use.
begin
owner :logistics do
raise "error"
end
rescue => e
puts e.owner # :logistics
end
- GitHub Code Owners for code reviews
Thanks to Nick Elser for creating this pattern.
View the changelog.
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features