Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a normalizer for conditions #577

Merged
merged 1 commit into from
Mar 17, 2019

Conversation

coorasse
Copy link
Member

@coorasse coorasse commented Mar 16, 2019

In a situation with a has_many through or has_one through association, cancancan could produce more joins than necessary if the rules were poorly defined.

e.g.

given the class:

class Article
  has_many :mentions
  has_many :mentioned_users, through: :mentions, source: :user
end

and the following rules:

can :read, Article, mentioned_users: { name: 'luke' }
can :read, Article, mentions: { user: { name: 'jabba' } }

this was producing the following query:

SELECT DISTINCT "articles".* 
FROM "articles" 
LEFT OUTER JOIN "mentions" ON "mentions"."article_id" = "articles"."id" 
LEFT OUTER JOIN "users" ON "users"."id" = "mentions"."user_id" 
LEFT OUTER JOIN "mentions" "mentions_articles" ON "mentions_articles"."article_id" = "articles"."id"
LEFT OUTER JOIN "users" "users_mentions" ON "users_mentions"."id" = "mentions_articles"."user_id"
WHERE (("users_mentions"."name" = 'jabba') OR ("users"."name" = 'luke'))

The rules normalizer recognises this situation and normalizes the rules automatically into:

can :read, Article, mentions: { user: { name: 'luke' } }
can :read, Article, mentions: { user: { name: 'jabba' } }

producing the following query instead:

SELECT DISTINCT "articles".* 
FROM "articles" 
LEFT OUTER JOIN "mentions" ON "mentions"."article_id" = "articles"."id" 
LEFT OUTER JOIN "users" ON "users"."id" = "mentions"."user_id" 
WHERE (("users"."name" = 'jabba') OR ("users"."name" = 'luke'))

The RuleNormalizer can be extended to allow also the usage of STI and solve the following issue: #532

@coorasse coorasse changed the base branch from develop to feature/3.0.0 March 16, 2019 12:32
@coorasse coorasse self-assigned this Mar 16, 2019
@coorasse coorasse force-pushed the feature/conditions_normalizer branch from 1b330ef to 5f40bbc Compare March 16, 2019 17:08
@coorasse coorasse merged commit fa14f3e into feature/3.0.0 Mar 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant