Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

23 allow passing a lambda as record #25

Merged
merged 7 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ AllCops:
TargetRubyVersion: 2.4
# Include common Ruby source files.
Include:
- '**/*.gemspec'
- '**/*.podspec'
- '**/*.jbuilder'
- '**/*.rake'
- '**/*.opal'
- '**/config.ru'
- '**/Gemfile'
- '**/Rakefile'
- '**/Capfile'
- '**/Guardfile'
Expand All @@ -35,6 +33,8 @@ AllCops:
- '**/Fastfile'
- '**/*Fastfile'
Exclude:
- '**/*.gemspec'
- '**/Gemfile'
- 'bin/bundle'
- 'bin/rails'
- 'bin/rake'
Expand Down Expand Up @@ -471,17 +471,6 @@ Style/EmptyMethod:
- compact
- expanded

# Checks whether the source file has a utf-8 encoding comment or not
# AutoCorrectEncodingComment must match the regex
# /#.*coding\s?[:=]\s?(?:UTF|utf)-8/
Style/Encoding:
EnforcedStyle: never
SupportedStyles:
- when_needed
- always
- never
AutoCorrectEncodingComment: '# encoding: utf-8'

Layout/EndOfLine:
# The `native` style means that CR+LF (Carriage Return + Line Feed) is
# enforced on Windows, and LF is enforced on other platforms. The other styles
Expand All @@ -500,7 +489,7 @@ Layout/ExtraSpacing:
# When true, forces the alignment of `=` in assignments on consecutive lines.
ForceEqualSignAlignment: false

Style/FileName:
Naming/FileName:
# File names listed in `AllCops:Include` are excluded by default. Add extra
# excludes here.
Exclude: ['lib/graphql-pundit.rb']
Expand Down Expand Up @@ -731,7 +720,7 @@ Style/MethodDefParentheses:
- require_no_parentheses
- require_no_parentheses_except_multiline

Style/MethodName:
Naming/MethodName:
EnforcedStyle: snake_case
SupportedStyles:
- snake_case
Expand Down Expand Up @@ -902,7 +891,7 @@ Style/PercentQLiterals:
- lower_case_q # Use `%q` when possible, `%Q` when necessary
- upper_case_q # Always use `%Q`

Style/PredicateName:
Naming/PredicateName:
# Predicate name prefixes.
NamePrefix:
- is_
Expand Down Expand Up @@ -1169,13 +1158,13 @@ Style/TrivialAccessors:
- to_s
- to_sym

Style/VariableName:
Naming/VariableName:
EnforcedStyle: snake_case
SupportedStyles:
- snake_case
- camelCase

Style/VariableNumber:
Naming/VariableNumber:
EnforcedStyle: normalcase
SupportedStyles:
- snake_case
Expand Down
104 changes: 48 additions & 56 deletions .rubocop_enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,59 @@ Layout/TrailingWhitespace:
StyleGuide: '#no-trailing-whitespace'
Enabled: true

#################### Style ###############################
#################### Naming ##############################

Style/AccessorMethodName:
Naming/AccessorMethodName:
Description: Check the naming of accessor methods for get_/set_.
StyleGuide: '#accessor_mutator_method_names'
Enabled: true

Naming/AsciiIdentifiers:
Description: 'Use only ascii symbols in identifiers.'
StyleGuide: '#english-identifiers'
Enabled: true

Naming/BinaryOperatorParameterName:
Description: 'When defining binary operators, name the argument other.'
StyleGuide: '#other-arg'
Enabled: true

Naming/ClassAndModuleCamelCase:
Description: 'Use CamelCase for classes and modules.'
StyleGuide: '#camelcase-classes'
Enabled: true

Naming/ConstantName:
Description: 'Constants should use SCREAMING_SNAKE_CASE.'
StyleGuide: '#screaming-snake-case'
Enabled: true

Naming/FileName:
Description: 'Use snake_case for source file names.'
StyleGuide: '#snake-case-files'
Enabled: true

Naming/MethodName:
Description: 'Use the configured style when naming methods.'
StyleGuide: '#snake-case-symbols-methods-vars'
Enabled: true

Naming/PredicateName:
Description: 'Check the names of predicate methods.'
StyleGuide: '#bool-methods-qmark'
Enabled: true

Naming/VariableName:
Description: 'Use the configured style when naming variables.'
StyleGuide: '#snake-case-symbols-methods-vars'
Enabled: true

Naming/VariableNumber:
Description: 'Use the configured style when numbering variables.'
Enabled: true

#################### Style ###############################

Style/Alias:
Description: 'Use alias instead of alias_method.'
StyleGuide: '#alias-method'
Expand All @@ -365,11 +411,6 @@ Style/AsciiComments:
StyleGuide: '#english-comments'
Enabled: true

Style/AsciiIdentifiers:
Description: 'Use only ascii symbols in identifiers.'
StyleGuide: '#english-identifiers'
Enabled: true

Style/Attr:
Description: 'Checks for uses of Module#attr.'
StyleGuide: '#attr'
Expand Down Expand Up @@ -412,11 +453,6 @@ Style/CharacterLiteral:
StyleGuide: '#no-character-literals'
Enabled: true

Style/ClassAndModuleCamelCase:
Description: 'Use CamelCase for classes and modules.'
StyleGuide: '#camelcase-classes'
Enabled: true

Style/ClassAndModuleChildren:
Description: 'Checks style of children classes and modules.'
Enabled: true
Expand Down Expand Up @@ -459,11 +495,6 @@ Style/ConditionalAssignment:
of assigning that variable inside of each branch.
Enabled: true

Style/ConstantName:
Description: 'Constants should use SCREAMING_SNAKE_CASE.'
StyleGuide: '#screaming-snake-case'
Enabled: true

Style/DefWithParentheses:
Description: 'Use def with parentheses when there are arguments.'
StyleGuide: '#method-parens'
Expand Down Expand Up @@ -519,11 +550,6 @@ Style/EvenOdd:
StyleGuide: '#predicate-methods'
Enabled: true

Style/FileName:
Description: 'Use snake_case for source file names.'
StyleGuide: '#snake-case-files'
Enabled: true

Style/FrozenStringLiteralComment:
Description: >-
Add the frozen_string_literal comment to the top of files
Expand Down Expand Up @@ -634,11 +660,6 @@ Style/MethodDefParentheses:
StyleGuide: '#method-parens'
Enabled: true

Style/MethodName:
Description: 'Use the configured style when naming methods.'
StyleGuide: '#snake-case-symbols-methods-vars'
Enabled: true

Style/MethodMissing:
Description: 'Avoid using `method_missing`.'
StyleGuide: '#no-method-missing'
Expand Down Expand Up @@ -768,11 +789,6 @@ Style/OneLineConditional:
StyleGuide: '#ternary-operator'
Enabled: true

Style/OpMethod:
Description: 'When defining binary operators, name the argument other.'
StyleGuide: '#other-arg'
Enabled: true

Style/OptionalArguments:
Description: >-
Checks for optional arguments that do not appear at the end
Expand Down Expand Up @@ -809,11 +825,6 @@ Style/PerlBackrefs:
StyleGuide: '#no-perl-regexp-last-matchers'
Enabled: true

Style/PredicateName:
Description: 'Check the names of predicate methods.'
StyleGuide: '#bool-methods-qmark'
Enabled: true

Style/PreferredHashMethods:
Description: 'Checks use of `has_key?` and `has_value?` Hash methods.'
StyleGuide: '#hash-key'
Expand Down Expand Up @@ -988,15 +999,6 @@ Style/VariableInterpolation:
StyleGuide: '#curlies-interpolate'
Enabled: true

Style/VariableName:
Description: 'Use the configured style when naming variables.'
StyleGuide: '#snake-case-symbols-methods-vars'
Enabled: true

Style/VariableNumber:
Description: 'Use the configured style when numbering variables.'
Enabled: true

Style/WhenThen:
Description: 'Use when x then ... for one-line cases.'
StyleGuide: '#one-line-cases'
Expand Down Expand Up @@ -1217,16 +1219,6 @@ Lint/InheritException:
Description: 'Avoid inheriting from the `Exception` class.'
Enabled: true

Lint/InvalidCharacterLiteral:
Description: >-
Checks for invalid character literals with a non-escaped
whitespace character.
Enabled: true

Lint/LiteralInCondition:
Description: 'Checks of literals used in conditions.'
Enabled: true

Lint/LiteralInInterpolation:
Description: 'Checks for literals used in interpolation.'
Enabled: true
Expand Down
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
sudo: required
sudo: false
dist: trusty

language: ruby

rvm:
- 2.2.7
- 2.3.4
- 2.3.5
- 2.4.2
- 2.5.0

notifications:
email: false

matrix:
fast_finish: true
allow_failures:
- rvm: 2.5.0

script:
- bundle exec rspec --format progress
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ field :createUser
end
```

You can also pass a `lambda` as a record. This receives the usual three arguments (parent value, arguments, context) and returns the value to be used as a record.

You might have also noticed the use of `authorize!` instead of `authorize` in this example. The difference between the two is this:

- `authorize` will set the field to `nil` if authorization fails
Expand Down
3 changes: 3 additions & 0 deletions graphql-pundit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'codecov', '~> 0.1.10'
spec.add_development_dependency 'fuubar', '~> 2.2.0'
spec.add_development_dependency 'pry', '~> 0.11.0'
spec.add_development_dependency 'pry-byebug', '~> 3.5.0'
spec.add_development_dependency 'pry-rescue', '~> 1.4.4'
spec.add_development_dependency 'pry-stack_explorer', '~> 0.4.9.2'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rspec', '~> 3.6'
spec.add_development_dependency 'rubocop', '~> 0.51.0'
Expand Down
2 changes: 2 additions & 0 deletions lib/graphql-pundit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

# Define `authorize` and `authorize!` helpers
module GraphQL
# rubocop:disable Metrics/MethodLength
def self.assign_authorize(raise_unauthorized)
# rubocop:enable Metrics/MethodLength
lambda do |defn, query = nil, policy: nil, record: nil|
opts = {record: record,
query: query || defn.name,
Expand Down
10 changes: 8 additions & 2 deletions lib/graphql-pundit/instrumenters/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ def instrument(_type, field)
end

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/BlockLength
def resolve_proc(current_user, old_resolve, options)
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this line removed?

Copy link
Collaborator Author

@phyrog phyrog Oct 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the last method in the file and re-enabling all disabled cops would add a lot of clutter. I'm planning on refactoring this file after this PR.

lambda do |obj, args, ctx|
begin
result = if options[:proc]
options[:proc].call(obj, args, ctx)
else
query = options[:query].to_s + '?'
record = options[:record] || obj
record = if options[:record].respond_to?(:call)
options[:record].call(obj, args, ctx)
else
options[:record] || obj
end
policy = options[:policy] || record
policy = ::Pundit::PolicyFinder.new(policy).policy!
policy = policy.new(ctx[current_user], record)
Expand Down
Loading