Sha256: 510b640948065f932cee25bc810f9dcca0d1560f4ac4c3017a4f1c3f650bbf08

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

module CanCan
  module ControllerResourceFinder
    protected

    def find_resource
      if @options[:singleton] && parent_resource.respond_to?(name)
        parent_resource.send(name)
      elsif @options[:find_by]
        find_resource_using_find_by
      else
        adapter.find(resource_base, id_param)
      end
    end

    def find_resource_using_find_by
      find_by_dynamic_finder || find_by_find_by_finder || resource_base.send(@options[:find_by], id_param)
    end

    def find_by_dynamic_finder
      method_name = "find_by_#{@options[:find_by]}!"
      resource_base.send(method_name, id_param) if resource_base.respond_to? method_name
    end

    def find_by_find_by_finder
      resource_base.find_by(@options[:find_by].to_sym => id_param) if resource_base.respond_to? :find_by
    end

    def id_param
      @params[id_param_key].to_s if @params[id_param_key].present?
    end

    def id_param_key
      if @options[:id_param]
        @options[:id_param]
      else
        parent? ? :"#{name}_id" : :id
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cancancan-2.3.0 lib/cancan/controller_resource_finder.rb
cancancan-2.2.0 lib/cancan/controller_resource_finder.rb
cancancan-2.1.4 lib/cancan/controller_resource_finder.rb
cancancan-2.1.3 lib/cancan/controller_resource_finder.rb
cancancan-2.1.2 lib/cancan/controller_resource_finder.rb