{"action":"created","comment":{"url":"https://api.github.com/repos/theforeman/foreman/pulls/comments/27043576","id":27043576,"diff_hunk":"@@ -260,6 +247,67 @@ def action_permission\n       end\n     end\n \n+    def parent_permission(child_permission)\n+      case child_permission.to_s\n+      when 'create', 'destroy'\n+        'edit'\n+      when 'edit', 'view'\n+        'view'\n+      else\n+        raise ::Foreman::Exception.new(N_(\"unknown parent permission for %s\"), \"#{params[:controller]}##{child_permission}\")\n+      end\n+    end\n+\n+    def parent_resource_details\n+      classes_arr = params.map do |key, value|\n+        md = key.match(/(\\w+)_id$/)\n+        if md\n+          values = [md[1], resource_class_for(resource_name_for(md[1])), value]\n+        else\n+          values = [nil, nil, nil]\n+        end\n+        values\n+      end\n+\n+      parent_name, parent_class, parent_id = classes_arr.find{|kvp| not kvp[1].nil?}\n+\n+      return [nil, nil] if parent_name.nil?\n+\n+      parent_scope = scope_for(parent_class, :permission => \"#{parent_permission(action_permission)}_#{parent_name.pluralize}\")\n+      parent_scope = select_by_resource_id_scope(parent_scope, parent_class, parent_id)\n+      [parent_name, parent_scope]\n+    end\n+\n+    #Should be replaced after moving to friendly_id version >= 5.0\n+    def select_by_resource_id_scope(base_scope, resource_class, resource_id)\n+      arel = resource_class.arel_table","path":"app/controllers/api/base_controller.rb","position":110,"original_position":110,"commit_id":"2e64c17da3b4f891be9f5d4c6aa69d99ea2bd925","original_commit_id":"2e64c17da3b4f891be9f5d4c6aa69d99ea2bd925","user":{"login":"ShimShtein","id":10046168,"avatar_url":"https://avatars.githubusercontent.com/u/10046168?v=3","gravatar_id":"","url":"https://api.github.com/users/ShimShtein","html_url":"https://github.com/ShimShtein","followers_url":"https://api.github.com/users/ShimShtein/followers","following_url":"https://api.github.com/users/ShimShtein/following{/other_user}","gists_url":"https://api.github.com/users/ShimShtein/gists{/gist_id}","starred_url":"https://api.github.com/users/ShimShtein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ShimShtein/subscriptions","organizations_url":"https://api.github.com/users/ShimShtein/orgs","repos_url":"https://api.github.com/users/ShimShtein/repos","events_url":"https://api.github.com/users/ShimShtein/events{/privacy}","received_events_url":"https://api.github.com/users/ShimShtein/received_events","type":"User","site_admin":false},"body":"It's modified from the newer version: even the newer version does not support scopes.\r\nProbably it would be a good idea to create a PR there that will enable scope functionality.","created_at":"2015-03-24T16:10:04Z","updated_at":"2015-03-24T16:10:04Z","html_url":"https://github.com/theforeman/foreman/pull/2099#discussion_r27043576","pull_request_url":"https://api.github.com/repos/theforeman/foreman/pulls/2099","_links":{"self":{"href":"https://api.github.com/repos/theforeman/foreman/pulls/comments/27043576"},"html":{"href":"https://github.com/theforeman/foreman/pull/2099#discussion_r27043576"},"pull_request":{"href":"https://api.github.com/repos/theforeman/foreman/pulls/2099"}}},"pull_request":{"url":"https://api.github.com/repos/theforeman/foreman/pulls/2099","id":27678266,"html_url":"https://github.com/theforeman/foreman/pull/2099","diff_url":"https://github.com/theforeman/foreman/pull/2099.diff","patch_url":"https://github.com/theforeman/foreman/pull/2099.patch","issue_url":"https://api.github.com/repos/theforeman/foreman/issues/2099","number":2099,"state":"open","locked":false,"title":"Fixes #8343: Consolidated code paths, and removed unnecessary calls","user":{"login":"ShimShtein","id":10046168,"avatar_url":"https://avatars.githubusercontent.com/u/10046168?v=3","gravatar_id":"","url":"https://api.github.com/users/ShimShtein","html_url":"https://github.com/ShimShtein","followers_url":"https://api.github.com/users/ShimShtein/followers","following_url":"https://api.github.com/users/ShimShtein/following{/other_user}","gists_url":"https://api.github.com/users/ShimShtein/gists{/gist_id}","starred_url":"https://api.github.com/users/ShimShtein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ShimShtein/subscriptions","organizations_url":"https://api.github.com/users/ShimShtein/orgs","repos_url":"https://api.github.com/users/ShimShtein/repos","events_url":"https://api.github.com/users/ShimShtein/events{/privacy}","received_events_url":"https://api.github.com/users/ShimShtein/received_events","type":"User","site_admin":false},"body":"Some changes to the controller's base resource scopes to reflect nested paths without performing extra DB call to fetch the parent object.\r\n\r\nMotivations:\r\nBase problem that the bug is indicating was running multiple queries against DB, when chaining calls that should just modify an existing query (like Host.where(a => b).where(c => d) ).\r\nFrom there on I was looking at different use cases that were executing more than one query to get the resource. I have realized that besides the obvious delegation of the options object, the chaining mechanism should always return an ActiveRecord::Relation object to successfully modify the query, that will be executed on the actual demand for the record.\r\nOne of the places that broke this concept was API parent resource extension for the resource_scope method. To fix it I refactored the find_nested_object base method that was responsible for the scoping based on relations given in the URI as in \"/architectures/x86/operatingsystems/1\".\r\n","created_at":"2015-01-20T13:45:51Z","updated_at":"2015-03-24T16:10:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ca86601e8eb285a53f4b0612e44da191e60a2272","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/theforeman/foreman/pulls/2099/commits","review_comments_url":"https://api.github.com/repos/theforeman/foreman/pulls/2099/comments","review_comment_url":"https://api.github.com/repos/theforeman/foreman/pulls/comments{/number}","comments_url":"https://api.github.com/repos/theforeman/foreman/issues/2099/comments","statuses_url":"https://api.github.com/repos/theforeman/foreman/statuses/2e64c17da3b4f891be9f5d4c6aa69d99ea2bd925","head":{"label":"ShimShtein:8343","ref":"8343","sha":"2e64c17da3b4f891be9f5d4c6aa69d99ea2bd925","user":{"login":"ShimShtein","id":10046168,"avatar_url":"https://avatars.githubusercontent.com/u/10046168?v=3","gravatar_id":"","url":"https://api.github.com/users/ShimShtein","html_url":"https://github.com/ShimShtein","followers_url":"https://api.github.com/users/ShimShtein/followers","following_url":"https://api.github.com/users/ShimShtein/following{/other_user}","gists_url":"https://api.github.com/users/ShimShtein/gists{/gist_id}","starred_url":"https://api.github.com/users/ShimShtein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ShimShtein/subscriptions","organizations_url":"https://api.github.com/users/ShimShtein/orgs","repos_url":"https://api.github.com/users/ShimShtein/repos","events_url":"https://api.github.com/users/ShimShtein/events{/privacy}","received_events_url":"https://api.github.com/users/ShimShtein/received_events","type":"User","site_admin":false},"repo":{"id":27429897,"name":"foreman","full_name":"ShimShtein/foreman","owner":{"login":"ShimShtein","id":10046168,"avatar_url":"https://avatars.githubusercontent.com/u/10046168?v=3","gravatar_id":"","url":"https://api.github.com/users/ShimShtein","html_url":"https://github.com/ShimShtein","followers_url":"https://api.github.com/users/ShimShtein/followers","following_url":"https://api.github.com/users/ShimShtein/following{/other_user}","gists_url":"https://api.github.com/users/ShimShtein/gists{/gist_id}","starred_url":"https://api.github.com/users/ShimShtein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ShimShtein/subscriptions","organizations_url":"https://api.github.com/users/ShimShtein/orgs","repos_url":"https://api.github.com/users/ShimShtein/repos","events_url":"https://api.github.com/users/ShimShtein/events{/privacy}","received_events_url":"https://api.github.com/users/ShimShtein/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ShimShtein/foreman","description":"an application that automates the lifecycle of servers ","fork":true,"url":"https://api.github.com/repos/ShimShtein/foreman","forks_url":"https://api.github.com/repos/ShimShtein/foreman/forks","keys_url":"https://api.github.com/repos/ShimShtein/foreman/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ShimShtein/foreman/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ShimShtein/foreman/teams","hooks_url":"https://api.github.com/repos/ShimShtein/foreman/hooks","issue_events_url":"https://api.github.com/repos/ShimShtein/foreman/issues/events{/number}","events_url":"https://api.github.com/repos/ShimShtein/foreman/events","assignees_url":"https://api.github.com/repos/ShimShtein/foreman/assignees{/user}","branches_url":"https://api.github.com/repos/ShimShtein/foreman/branches{/branch}","tags_url":"https://api.github.com/repos/ShimShtein/foreman/tags","blobs_url":"https://api.github.com/repos/ShimShtein/foreman/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ShimShtein/foreman/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ShimShtein/foreman/git/refs{/sha}","trees_url":"https://api.github.com/repos/ShimShtein/foreman/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ShimShtein/foreman/statuses/{sha}","languages_url":"https://api.github.com/repos/ShimShtein/foreman/languages","stargazers_url":"https://api.github.com/repos/ShimShtein/foreman/stargazers","contributors_url":"https://api.github.com/repos/ShimShtein/foreman/contributors","subscribers_url":"https://api.github.com/repos/ShimShtein/foreman/subscribers","subscription_url":"https://api.github.com/repos/ShimShtein/foreman/subscription","commits_url":"https://api.github.com/repos/ShimShtein/foreman/commits{/sha}","git_commits_url":"https://api.github.com/repos/ShimShtein/foreman/git/commits{/sha}","comments_url":"https://api.github.com/repos/ShimShtein/foreman/comments{/number}","issue_comment_url":"https://api.github.com/repos/ShimShtein/foreman/issues/comments{/number}","contents_url":"https://api.github.com/repos/ShimShtein/foreman/contents/{+path}","compare_url":"https://api.github.com/repos/ShimShtein/foreman/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ShimShtein/foreman/merges","archive_url":"https://api.github.com/repos/ShimShtein/foreman/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ShimShtein/foreman/downloads","issues_url":"https://api.github.com/repos/ShimShtein/foreman/issues{/number}","pulls_url":"https://api.github.com/repos/ShimShtein/foreman/pulls{/number}","milestones_url":"https://api.github.com/repos/ShimShtein/foreman/milestones{/number}","notifications_url":"https://api.github.com/repos/ShimShtein/foreman/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ShimShtein/foreman/labels{/name}","releases_url":"https://api.github.com/repos/ShimShtein/foreman/releases{/id}","created_at":"2014-12-02T11:40:34Z","updated_at":"2015-03-16T11:37:25Z","pushed_at":"2015-03-16T11:58:16Z","git_url":"git://github.com/ShimShtein/foreman.git","ssh_url":"git@github.com:ShimShtein/foreman.git","clone_url":"https://github.com/ShimShtein/foreman.git","svn_url":"https://github.com/ShimShtein/foreman","homepage":"http://theforeman.org","size":28844,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"develop"}},"base":{"label":"theforeman:develop","ref":"develop","sha":"cbe1391f48f37d4bf9a53091c668b6a91bc6ef83","user":{"login":"theforeman","id":1439844,"avatar_url":"https://avatars.githubusercontent.com/u/1439844?v=3","gravatar_id":"","url":"https://api.github.com/users/theforeman","html_url":"https://github.com/theforeman","followers_url":"https://api.github.com/users/theforeman/followers","following_url":"https://api.github.com/users/theforeman/following{/other_user}","gists_url":"https://api.github.com/users/theforeman/gists{/gist_id}","starred_url":"https://api.github.com/users/theforeman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/theforeman/subscriptions","organizations_url":"https://api.github.com/users/theforeman/orgs","repos_url":"https://api.github.com/users/theforeman/repos","events_url":"https://api.github.com/users/theforeman/events{/privacy}","received_events_url":"https://api.github.com/users/theforeman/received_events","type":"Organization","site_admin":false},"repo":{"id":258750,"name":"foreman","full_name":"theforeman/foreman","owner":{"login":"theforeman","id":1439844,"avatar_url":"https://avatars.githubusercontent.com/u/1439844?v=3","gravatar_id":"","url":"https://api.github.com/users/theforeman","html_url":"https://github.com/theforeman","followers_url":"https://api.github.com/users/theforeman/followers","following_url":"https://api.github.com/users/theforeman/following{/other_user}","gists_url":"https://api.github.com/users/theforeman/gists{/gist_id}","starred_url":"https://api.github.com/users/theforeman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/theforeman/subscriptions","organizations_url":"https://api.github.com/users/theforeman/orgs","repos_url":"https://api.github.com/users/theforeman/repos","events_url":"https://api.github.com/users/theforeman/events{/privacy}","received_events_url":"https://api.github.com/users/theforeman/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/theforeman/foreman","description":"an application that automates the lifecycle of servers ","fork":false,"url":"https://api.github.com/repos/theforeman/foreman","forks_url":"https://api.github.com/repos/theforeman/foreman/forks","keys_url":"https://api.github.com/repos/theforeman/foreman/keys{/key_id}","collaborators_url":"https://api.github.com/repos/theforeman/foreman/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/theforeman/foreman/teams","hooks_url":"https://api.github.com/repos/theforeman/foreman/hooks","issue_events_url":"https://api.github.com/repos/theforeman/foreman/issues/events{/number}","events_url":"https://api.github.com/repos/theforeman/foreman/events","assignees_url":"https://api.github.com/repos/theforeman/foreman/assignees{/user}","branches_url":"https://api.github.com/repos/theforeman/foreman/branches{/branch}","tags_url":"https://api.github.com/repos/theforeman/foreman/tags","blobs_url":"https://api.github.com/repos/theforeman/foreman/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/theforeman/foreman/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/theforeman/foreman/git/refs{/sha}","trees_url":"https://api.github.com/repos/theforeman/foreman/git/trees{/sha}","statuses_url":"https://api.github.com/repos/theforeman/foreman/statuses/{sha}","languages_url":"https://api.github.com/repos/theforeman/foreman/languages","stargazers_url":"https://api.github.com/repos/theforeman/foreman/stargazers","contributors_url":"https://api.github.com/repos/theforeman/foreman/contributors","subscribers_url":"https://api.github.com/repos/theforeman/foreman/subscribers","subscription_url":"https://api.github.com/repos/theforeman/foreman/subscription","commits_url":"https://api.github.com/repos/theforeman/foreman/commits{/sha}","git_commits_url":"https://api.github.com/repos/theforeman/foreman/git/commits{/sha}","comments_url":"https://api.github.com/repos/theforeman/foreman/comments{/number}","issue_comment_url":"https://api.github.com/repos/theforeman/foreman/issues/comments{/number}","contents_url":"https://api.github.com/repos/theforeman/foreman/contents/{+path}","compare_url":"https://api.github.com/repos/theforeman/foreman/compare/{base}...{head}","merges_url":"https://api.github.com/repos/theforeman/foreman/merges","archive_url":"https://api.github.com/repos/theforeman/foreman/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/theforeman/foreman/downloads","issues_url":"https://api.github.com/repos/theforeman/foreman/issues{/number}","pulls_url":"https://api.github.com/repos/theforeman/foreman/pulls{/number}","milestones_url":"https://api.github.com/repos/theforeman/foreman/milestones{/number}","notifications_url":"https://api.github.com/repos/theforeman/foreman/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/theforeman/foreman/labels{/name}","releases_url":"https://api.github.com/repos/theforeman/foreman/releases{/id}","created_at":"2009-07-23T15:07:00Z","updated_at":"2015-03-24T13:15:21Z","pushed_at":"2015-03-24T15:24:39Z","git_url":"git://github.com/theforeman/foreman.git","ssh_url":"git@github.com:theforeman/foreman.git","clone_url":"https://github.com/theforeman/foreman.git","svn_url":"https://github.com/theforeman/foreman","homepage":"http://theforeman.org","size":73216,"stargazers_count":700,"watchers_count":700,"language":"Ruby","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":371,"mirror_url":null,"open_issues_count":85,"forks":371,"open_issues":85,"watchers":700,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/theforeman/foreman/pulls/2099"},"html":{"href":"https://github.com/theforeman/foreman/pull/2099"},"issue":{"href":"https://api.github.com/repos/theforeman/foreman/issues/2099"},"comments":{"href":"https://api.github.com/repos/theforeman/foreman/issues/2099/comments"},"review_comments":{"href":"https://api.github.com/repos/theforeman/foreman/pulls/2099/comments"},"review_comment":{"href":"https://api.github.com/repos/theforeman/foreman/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/theforeman/foreman/pulls/2099/commits"},"statuses":{"href":"https://api.github.com/repos/theforeman/foreman/statuses/2e64c17da3b4f891be9f5d4c6aa69d99ea2bd925"}}}}