Skip to content

Instantly share code, notes, and snippets.

@gschorkopf
Created February 13, 2015 01:45
Show Gist options
  • Save gschorkopf/2c7a3ecc81fc9301d6b5 to your computer and use it in GitHub Desktop.
Save gschorkopf/2c7a3ecc81fc9301d6b5 to your computer and use it in GitHub Desktop.
Hum. I don't think I know of a good way to do that that isn't a bit meta-y. I assume you're saying, for this group of policy methods, return false if ```assigned_to_company?``` is false.
Here's a fun thing I found with help from the interwebs. Whether it's a good solution or not is up for debate.
```ruby
module CompanyPolicyHelper
def check_company_affiliation_for(*methods)
methods.each do |method_name|
method = instance_method(method_name)
define_method(method_name) do |*args, &block|
yield
method.bind(self).(*args, &block)
end
end
end
end
class OperationPolicy
extend CompanyPolicyHelper
def show?
# stuff
end
def index?
# stuff
end
check_company_affiliation_for([:show, :index]) do
return false if assigned_to_company?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment