know_it_all 0.1.4 → 0.1.5

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
  SHA1:
3
- metadata.gz: 85674d4642b03b912a9963a5f5d9845946c38961
4
- data.tar.gz: 7d38e9a2dd38d6b68365d448d127268eef9f8f53
3
+ metadata.gz: f2c9d3f6e989ec8e15956f85a2734e15ce858a7b
4
+ data.tar.gz: 1e7194c8936f99fcecbd5c543855c69bdc5577e2
5
5
  SHA512:
6
- metadata.gz: 1d6f9f57dcb2ca6e63091c35d5f2461e819696697e36f37bae808673e2928e068ac6e20763d9489cb8b435134c645e2e718f202472e90b76bc2db11e7e532175
7
- data.tar.gz: 916d1060f7d361600b95a9714eb535c185d73f48212f32d562900bfc0defaa3aaf6372b0d6e183c33c7125e71cc9c3578d4b3163481d76ecfc88fced0d4da15a
6
+ metadata.gz: 8efd4ad6004315675decbf3038628d5178319e5895c7d0d2a7e80df15984dce504d5c315d33893b6988ced57d63fafbc65e66bba58ebe10a0caf10cb36884ac3
7
+ data.tar.gz: 26183fd792e49f71e69bed56a522648a5fc0854500f72823223dd127c162b314b1462a46a9f1f8f63f7114cd0858913f0e7dc05c857c09a98df7daa0ff67322a
data/CHANGELOG.md ADDED
@@ -0,0 +1,31 @@
1
+ # Change Log
2
+
3
+ ## [v0.1.5](https://github.com/mrodrigues/know_it_all/tree/v0.1.5) (2016-08-17)
4
+ [Full Changelog](https://github.com/mrodrigues/know_it_all/compare/v0.1.4...v0.1.5)
5
+
6
+ - Add Rails generators for installing the gem and creating policies
7
+ - Deprecating `assert` in favor of `validate`
8
+
9
+ ## [v0.1.4](https://github.com/mrodrigues/know_it_all/tree/v0.1.4) (2016-07-03)
10
+ [Full Changelog](https://github.com/mrodrigues/know_it_all/compare/v0.1.3...v0.1.4)
11
+
12
+ - Add support to define error message with a block
13
+
14
+ ## [v0.1.3](https://github.com/mrodrigues/know_it_all/tree/v0.1.3) (2016-07-03)
15
+ [Full Changelog](https://github.com/mrodrigues/know_it_all/compare/v0.1.2...v0.1.3)
16
+
17
+ - Remove ActiveSupport dependency [\#3](https://github.com/mrodrigues/know_it_all/issues/3)
18
+
19
+ ## [v0.1.2](https://github.com/mrodrigues/know_it_all/tree/v0.1.2) (2016-06-30)
20
+ [Full Changelog](https://github.com/mrodrigues/know_it_all/compare/v0.1.1...v0.1.2)
21
+
22
+ - Add support to inheritance chain of `KnowItAll::Base`
23
+
24
+ ## [v0.1.1](https://github.com/mrodrigues/know_it_all/tree/v0.1.1) (2016-06-30)
25
+ [Full Changelog](https://github.com/mrodrigues/know_it_all/compare/v0.1.0...v0.1.1)
26
+
27
+ - Fix bug when extending `KnowItAll::Base` with more than one class
28
+
29
+ ## [v0.1.0](https://github.com/mrodrigues/know_it_all/tree/v0.1.0) (2016-06-29)
30
+
31
+ - Initial release
data/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/know_it_all.svg)](https://badge.fury.io/rb/know_it_all)
6
6
  [![Join the chat at https://gitter.im/mrodrigues/know_it_all](https://badges.gitter.im/mrodrigues/know_it_all.svg)](https://gitter.im/mrodrigues/know_it_all?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7
+ [![Twitter Follow](https://img.shields.io/twitter/follow/know_it_all_gem.svg?style=social&label=Follow&maxAge=2592000)](https://twitter.com/know_it_all_gem)
7
8
 
8
9
  KnowItAll is a small, object-oriented approach to authorization. It knows everything about your application!
9
10
 
@@ -18,6 +19,9 @@ Table of Contents
18
19
  * [Why?](#why)
19
20
  * [Why not just Pundit?](#why-not-just-pundit)
20
21
  * [Installation](#installation)
22
+ * [Generators](#generators)
23
+ * [Install](#install)
24
+ * [Policy](#policy)
21
25
  * [Usage](#usage)
22
26
  * [Creating policies](#creating-policies)
23
27
  * [Naming convention](#naming-convention)
@@ -28,6 +32,8 @@ Table of Contents
28
32
  * [Avoiding conflicts in the controller](#avoiding-conflicts-in-the-controller)
29
33
  * [Overrides](#overrides)
30
34
  * [Enforcing authorization checks](#enforcing-authorization-checks)
35
+ * [Get help](#get-help)
36
+ * [Alternatives](#alternatives)
31
37
  * [Development](#development)
32
38
  * [Contributing](#contributing)
33
39
  * [License](#license)
@@ -112,17 +118,47 @@ Or install it yourself as:
112
118
 
113
119
  $ gem install know_it_all
114
120
 
121
+ Then include `KnowItAll` in your application controller:
122
+
123
+ ```ruby
124
+ class ApplicationController < ActionController::Base
125
+ include KnowItAll
126
+ end
127
+ ```
128
+
129
+ ### Generators
130
+
131
+ #### Install
132
+
133
+ When adding the gem to your application, you can optionally run:
134
+
135
+ ```bash
136
+ rails generate know_it_all:install
137
+ ```
138
+
139
+ This will include the `KnowItAll` module in your application controller and create a `ApplicationPolicy`, which you can use to define some defaults for your policies.
140
+
141
+ #### Policy
142
+
143
+ To create a new policy, you can use the following generator:
144
+
145
+ ```bash
146
+ rails generate know_it_all:policy orders index create update destroy
147
+ ```
148
+
149
+ This will create, in addition to one policy file for each action (`app/policies/orders_policies/index.rb`, `app/policies/orders_policies/create.rb`, etc.), a `app/policies/stores_policy.rb` file containing both the `StoresPolicies` module declaration and a `StoresPolicies::Base` class to share common behaviors specific to that controller's policies.
150
+
115
151
  ## Usage
116
152
 
117
153
  There are two steps to using this gem: creating and using policies:
118
154
 
119
155
  ### Creating policies
120
156
 
121
- A policy is simply a class obeys some rules:
157
+ The core of this gem, policies are regular Ruby classes that perform some validation and obey some rules:
122
158
 
123
159
  * It is initialized with the same arguments that are passed to the `authorize`, `authorize!` and `authorize?` methods in the controller;
124
160
  * It responds to a method `errors`;
125
- * Calling `errors` returns an object that responds to the method `empty?` and is serializable. It's usually an array, but it could easily be an `ActiveModel::Errors`.
161
+ * Calling `errors` returns an object that responds to the method `empty?` and is serializable (so you can render it as the response to the request). It's usually an array of `String`s, but it could easily be an `ActiveModel::Errors`.
126
162
 
127
163
  Here's an example:
128
164
 
@@ -148,7 +184,7 @@ module OrdersPolicies
148
184
  end
149
185
  ```
150
186
 
151
- Using `ActiveModel::Validations`:
187
+ Notice that I didn't include any module or extend from any class here. `KnowItAll` will simply infer the name of the class (based on the [Naming convention](#naming-convention)) and instantiate it, even if it's a PORO. Now, using `ActiveModel::Validations`:
152
188
 
153
189
  ```ruby
154
190
  module OrdersPolicies
@@ -189,12 +225,12 @@ If you don't want to write your own policy from the scratch, I've also provided
189
225
  ```ruby
190
226
  module OrdersPolicies
191
227
  class Create < KnowItAll::Base
192
- assert :user_signed_in?, "User must be signed in"
193
- assert :address_present?, "User must have a registered address"
194
- assert :address_in_range?, "Registered address is outside the range"
195
- assert :cart_has_minimum?, "Cart must contain at least $10 in items"
196
- assert :items_available?, "Some of the items are not available"
197
- assert :store_open?, "The store is closed"
228
+ validate :user_signed_in?, "User must be signed in"
229
+ validate :address_present?, "User must have a registered address"
230
+ validate :address_in_range?, "Registered address is outside the range"
231
+ validate :cart_has_minimum?, "Cart must contain at least $10 in items"
232
+ validate :items_available?, "Some of the items are not available"
233
+ validate :store_open?, "The store is closed"
198
234
 
199
235
  def initialize(current_user, cart, store)
200
236
  @current_user = current_user
@@ -205,7 +241,25 @@ module OrdersPolicies
205
241
  end
206
242
  ```
207
243
 
208
- The class method `assert` expects a `Symbol` representing the name of a predicate and a `String` containing the error message in case the predicate fails. The default `errors` method returns an array containing the messages for all the assertions that didn't pass.
244
+ The class method `validate` expects a `Symbol` representing the name of a predicate and a `String` containing the error message in case the predicate fails. The default `errors` method returns an array containing the messages for all the validations that didn't pass.
245
+
246
+ In case you need to define dynamically the error message (e.g. for internationalization), the `validate` method also accepts any object that responds to `call`, as a lambda. It'll pass the policy itself as an argument:
247
+
248
+ ```ruby
249
+ module OrdersPolicies
250
+ class Create < KnowItAll::Base
251
+ attr_reader :cart
252
+
253
+ validate :cart_has_minimum?, -> (policy) {
254
+ I18n.t(:cart_minimum_total_error, total: policy.cart.total)
255
+ }
256
+
257
+ def initialize(cart)
258
+ @cart = cart
259
+ end
260
+ end
261
+ end
262
+ ```
209
263
 
210
264
  ### Using policies
211
265
 
@@ -390,6 +444,14 @@ class ApplicationController < ActionController::Base
390
444
  end
391
445
  ```
392
446
 
447
+ ## Get help
448
+
449
+ Follow me on [Twitter](https://twitter.com/know_it_all_gem) or chat with me on [Gitter](https://gitter.im/mrodrigues/know_it_all)! :)
450
+
451
+ ## Alternatives
452
+
453
+ As I said in the beginning, this project is heavily inspired by [Pundit](https://github.com/elabs/pundit), so obviously it is an excellent alternative. I've also used for several years the [CanCan](https://github.com/ryanb/cancan) gem, but it's been abandoned since then; the [CanCanCan](https://github.com/CanCanCommunity/cancancan) gem is a continuation that seems to be thriving. For more alternatives, check [The Ruby Toolbox](https://www.ruby-toolbox.com/categories/rails_authorization) page.
454
+
393
455
  ## Development
394
456
 
395
457
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Installs KnowItAll in the current application
3
+
4
+ Example:
5
+ rails generate know_it_all:install
6
+
7
+ This will create:
8
+ app/policies/application_policy.rb
9
+
10
+ And add the KnowItAll module to:
11
+ app/controllers/application_controller.rb
@@ -0,0 +1,18 @@
1
+ module KnowItAll
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def add_module_to_application_controller
7
+ inject_into_file "app/controllers/application_controller.rb",
8
+ after: "class ApplicationController < ActionController::Base\n" do
9
+ " include KnowItAll\n"
10
+ end
11
+ end
12
+
13
+ def create_application_policy_file
14
+ template "application_policy.rb", "app/policies/application_policy.rb"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationPolicy < KnowItAll::Base
2
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Generates a new policy
3
+
4
+ Example:
5
+ rails generate know_it_all:policy controller_name action_name [first_validation first_message second_validation second_message...]
6
+
7
+ This will create:
8
+ app/policies/controller_name_policies.rb
9
+ app/policies/controller_name_policies/action_name.rb
@@ -0,0 +1,29 @@
1
+ require 'pry'
2
+
3
+ module KnowItAll
4
+ module Generators
5
+ class PolicyGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ argument :controller_name, type: :string
9
+ argument :action_names, type: :array
10
+
11
+ def create_policy_file
12
+ unless File.exists?("app/policies/#{module_name}.rb")
13
+ template 'module.rb', "app/policies/#{module_name}.rb"
14
+ end
15
+
16
+ action_names.each do |action_name|
17
+ @action_name = action_name
18
+ template 'policy.rb', "app/policies/#{module_name}/#{action_name}.rb"
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def module_name
25
+ "#{controller_name}_policies"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module <%= module_name.camelize %>
2
+ class Base < ApplicationPolicy
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module <%= module_name.camelize %>
2
+ class <%= @action_name.camelize %> < <%= module_name.camelize %>::Base
3
+ end
4
+ end
@@ -12,7 +12,13 @@ module KnowItAll
12
12
  subclass.validations = validations.dup
13
13
  end
14
14
 
15
- def self.assert(method_name, message)
15
+ # <b>DEPRECATED:</b> Please use <tt>validate</tt> instead.
16
+ def self.assert(*args)
17
+ warn "[DEPRECATION] `assert` is deprecated. Please use `validate` instead."
18
+ validate(*args)
19
+ end
20
+
21
+ def self.validate(method_name, message)
16
22
  validations[method_name] = message
17
23
  end
18
24
 
@@ -1,3 +1,3 @@
1
1
  module KnowItAll
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: know_it_all
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - mrodrigues
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-03 00:00:00.000000000 Z
11
+ date: 2016-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,6 +89,7 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
91
  - ".travis.yml"
92
+ - CHANGELOG.md
92
93
  - CODE_OF_CONDUCT.md
93
94
  - Gemfile
94
95
  - LICENSE.txt
@@ -97,6 +98,13 @@ files:
97
98
  - bin/console
98
99
  - bin/setup
99
100
  - know_it_all.gemspec
101
+ - lib/generators/know_it_all/install/USAGE
102
+ - lib/generators/know_it_all/install/install_generator.rb
103
+ - lib/generators/know_it_all/install/templates/application_policy.rb
104
+ - lib/generators/know_it_all/policy/USAGE
105
+ - lib/generators/know_it_all/policy/policy_generator.rb
106
+ - lib/generators/know_it_all/policy/templates/module.rb
107
+ - lib/generators/know_it_all/policy/templates/policy.rb
100
108
  - lib/know_it_all.rb
101
109
  - lib/know_it_all/authorizer.rb
102
110
  - lib/know_it_all/base.rb
@@ -127,4 +135,3 @@ signing_key:
127
135
  specification_version: 4
128
136
  summary: OO authorization for APIs
129
137
  test_files: []
130
- has_rdoc: