Skip to content

Instantly share code, notes, and snippets.

@gschorkopf
Created February 13, 2015 01:45
Show Gist options
  • Select an option

  • Save gschorkopf/2c7a3ecc81fc9301d6b5 to your computer and use it in GitHub Desktop.

Select an option

Save gschorkopf/2c7a3ecc81fc9301d6b5 to your computer and use it in GitHub Desktop.

Revisions

  1. gschorkopf created this gist Feb 13, 2015.
    32 changes: 32 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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