pragma 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3011120ae28ea10f9c9c287c23c6b3644d139bff0850ef7cb94d0353b01807fb
4
- data.tar.gz: d2bf641611df5b41a3f57264872e30e4e45d28c17b3fe7978693d412fa2c7e47
3
+ metadata.gz: 474ef14673089ce28a581b260a0c8913b3e1afe462eeb7cb840c406a29195316
4
+ data.tar.gz: 15174e9df88e96007f15475464d023eab01c92e3b791f8c0289382f65a01c279
5
5
  SHA512:
6
- metadata.gz: e8c89a042285a28cf7abb1c79bf1f7d083b0b306badfb50d536944b246c6402f19459bcbe9962f35e9197c709f211fae0913a52578b2e29421f8e8e9a440990b
7
- data.tar.gz: 6eaf14f5241b3dd29bc96c6a4de261f10f45899f8a5a79361e30b3e847eb50c27cd8fbfc7abd973f62e7208f0ee31401f8af13d667dce0135f352df43ff6c05b
6
+ metadata.gz: b1a710dd54c3a2aff7fa789293d160e0061c61e9d3fd86d3b00c8360efa6171de79719803c89a352ac6059895d3e1b4fdfe5d6c54e5e73e6ea0cbcdf68e0bc8c
7
+ data.tar.gz: 62440fa3450e7e40314e92d6a771c072657130cfd785b05cabf2becf736fb2429be7f012882308572791c2e6e113f52a206f199b5b549cc7385ac91d8d79748d
@@ -5,6 +5,7 @@ AllCops:
5
5
  Include:
6
6
  - '**/Gemfile'
7
7
  - '**/Rakefile'
8
+ - '**/*.rb'
8
9
  Exclude:
9
10
  - 'bin/*'
10
11
  - 'db/**/*'
@@ -13,8 +14,6 @@ AllCops:
13
14
  - 'spec/rails_helper.rb'
14
15
  - 'spec/support/**/*'
15
16
  - 'config/**/*'
16
- - '**/Rakefile'
17
- - '**/Gemfile'
18
17
 
19
18
  RSpec/DescribeClass:
20
19
  Exclude:
@@ -52,7 +51,7 @@ Style/SignalException:
52
51
  Style/BracesAroundHashParameters:
53
52
  EnforcedStyle: context_dependent
54
53
 
55
- Lint/EndAlignment:
54
+ Layout/EndAlignment:
56
55
  EnforcedStyleAlignWith: variable
57
56
  AutoCorrect: true
58
57
 
@@ -5,11 +5,25 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [Unreleased]
9
+
10
+ ## [2.4.0]
11
+
12
+ ### Added
13
+
14
+ - Implemented `Boolean` scope
15
+ - Added support for additional policy context via `policy.context`
16
+
17
+ ### Fixed
18
+
19
+ - Added support for different capitalizations of API namespace
20
+ - Fixed an issue where each filter would override the previous one
21
+
8
22
  ## [2.3.0]
9
23
 
10
24
  ### Added
11
25
 
12
- - Add support for Kaminari in `Pagination` macro
26
+ - Added support for Kaminari in `Pagination` macro
13
27
 
14
28
  ### Changed
15
29
 
@@ -62,7 +76,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
62
76
 
63
77
  First Pragma 2 release.
64
78
 
65
- [Unreleased]: https://github.com/pragmarb/pragma/compare/v2.3.0...HEAD
79
+ [Unreleased]: https://github.com/pragmarb/pragma/compare/v2.4.0...HEAD
80
+ [2.4.0]: https://github.com/pragmarb/pragma/compare/v2.3.0...v2.4.0
66
81
  [2.3.0]: https://github.com/pragmarb/pragma/compare/v2.2.0...v2.3.0
67
82
  [2.2.0]: https://github.com/pragmarb/pragma/compare/v2.1.1...v2.2.0
68
83
  [2.1.1]: https://github.com/pragmarb/pragma/compare/v2.1.0...v2.1.1
data/Gemfile CHANGED
@@ -1,11 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in pragma.gemspec
4
6
  gemspec
5
7
 
6
8
  gem 'pry'
7
-
8
- gem 'pragma-policy', github: 'pragmarb/pragma-policy'
9
- gem 'pragma-operation', github: 'pragmarb/pragma-operation'
10
- gem 'pragma-contract', github: 'pragmarb/pragma-contract'
11
- gem 'pragma-decorator', github: 'pragmarb/pragma-decorator'
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # Pragma
2
2
 
3
3
  [![Build Status](https://travis-ci.org/pragmarb/pragma.svg?branch=master)](https://travis-ci.org/pragmarb/pragma)
4
- [![Dependency Status](https://gemnasium.com/badges/github.com/pragmarb/pragma.svg)](https://gemnasium.com/github.com/pragmarb/pragma)
5
4
  [![Coverage Status](https://coveralls.io/repos/github/pragmarb/pragma/badge.svg?branch=master)](https://coveralls.io/github/pragmarb/pragma?branch=master)
6
5
  [![Maintainability](https://api.codeclimate.com/v1/badges/e51e8d7489eb72ab97ba/maintainability)](https://codeclimate.com/github/pragmarb/pragma/maintainability)
7
6
 
@@ -296,6 +295,11 @@ If the user is not authorized to perform the operation (i.e. if the policy's `#s
296
295
  to override the error handling logic, you can remove the `fail_fast` option and instead implement
297
296
  your own `failure` step.
298
297
 
298
+ The macro accepts the following options, which can be defined on the operation or at runtime:
299
+
300
+ - `policy.context`: the context to use for the policy (optional, `current_user` is used if not
301
+ provided).
302
+
299
303
  ### Filtering
300
304
 
301
305
  **Used in:** Index
@@ -338,7 +342,8 @@ The following filters are available for ActiveRecord currently:
338
342
  - `Where`: adds a generic `WHERE` clause (requires `:condition` and passes the parameter's value as
339
343
  `:value`).
340
344
  - `Scope`: calls a method on the collection (requires `:scope` and passes the parameter's value as
341
- the first argument).
345
+ the first argument);
346
+ - `Boolean`: calls a method on the collection (requires `:scope` and doesn't pass any arguments).
342
347
 
343
348
  Support for more clauses as well as more ORMs will come soon.
344
349
 
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -15,6 +15,7 @@ require 'pragma/filter/like'
15
15
  require 'pragma/filter/ilike'
16
16
  require 'pragma/filter/where'
17
17
  require 'pragma/filter/scope'
18
+ require 'pragma/filter/boolean'
18
19
 
19
20
  require 'pragma/operation/filter'
20
21
 
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragma
4
+ module Filter
5
+ class Boolean < Base
6
+ attr_reader :scope
7
+
8
+ def initialize(scope:, **other)
9
+ super(**other)
10
+ @scope = scope
11
+ end
12
+
13
+ def apply(relation:, **)
14
+ relation.public_send(scope)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -47,8 +47,9 @@ module Pragma
47
47
 
48
48
  def root_namespace(input, options)
49
49
  resource_namespace = resource_namespace(input, options)
50
- return [] if resource_namespace.first == 'API'
51
- resource_namespace[0..((resource_namespace.index('API') || 1) - 1)]
50
+ return [] if %w[API Api].include?(resource_namespace.first)
51
+ api_index = (resource_namespace.index('API') || resource_namespace.index('Api') || 1)
52
+ resource_namespace[0..(api_index - 1)]
52
53
  end
53
54
 
54
55
  def expected_model_class(input, options)
@@ -34,7 +34,7 @@ module Pragma
34
34
  value = options['params'][filter.param]
35
35
  next unless value.present?
36
36
 
37
- relation = filter.apply(relation: options['model'], value: value)
37
+ relation = filter.apply(relation: relation, value: value)
38
38
  end
39
39
 
40
40
  relation
@@ -50,9 +50,7 @@ module Pragma
50
50
  # can't distinguish. Maybe there's a better way to do it?
51
51
  options['params'].tap do |p|
52
52
  %w[pagination.page_param pagination.per_page_param].each do |key|
53
- if p[options[key]] && p[options[key]].respond_to?(:to_i)
54
- p[options[key]] = p[options[key]].to_i
55
- end
53
+ p[options[key]] = p[options[key]].to_i if p[options[key]]&.respond_to?(:to_i)
56
54
  end
57
55
  end
58
56
  end
@@ -10,7 +10,10 @@ module Pragma
10
10
  module Policy
11
11
  class << self
12
12
  def for(input, name, options, action = nil)
13
- policy = options["policy.#{name}.class"].new(options['current_user'], options['model'])
13
+ policy = options["policy.#{name}.class"].new(
14
+ options['policy.context'] || options['current_user'],
15
+ options['model']
16
+ )
14
17
 
15
18
  action_name = action.is_a?(Proc) ? action.call(options) : action
16
19
  action_name ||= input.class.operation_name
@@ -20,8 +20,12 @@ module Pragma
20
20
  options['model'] = options['model.class'].all
21
21
  end
22
22
 
23
- def scope!(options, current_user:, model:, **)
24
- options['model'] = options['policy.default.scope.class'].new(current_user, model).resolve
23
+ # TODO: Turn this into a macro.
24
+ def scope!(options, model:, **)
25
+ options['model'] = options['policy.default.scope.class'].new(
26
+ options['policy.context'] || options['current_user'],
27
+ model
28
+ ).resolve
25
29
  end
26
30
 
27
31
  def respond!(options, **)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pragma
4
- VERSION = '2.3.0'
4
+ VERSION = '2.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pragma
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Desantis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-28 00:00:00.000000000 Z
11
+ date: 2018-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pragma-contract
@@ -199,6 +199,7 @@ files:
199
199
  - lib/pragma.rb
200
200
  - lib/pragma/decorator/error.rb
201
201
  - lib/pragma/filter/base.rb
202
+ - lib/pragma/filter/boolean.rb
202
203
  - lib/pragma/filter/equals.rb
203
204
  - lib/pragma/filter/ilike.rb
204
205
  - lib/pragma/filter/like.rb