allowed_params 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2603a57ec1250a74784b8a554f116248cb9ff869
4
+ data.tar.gz: d61ea3df8b87d9e83b6a7012a3eb0e2d92ec313c
5
+ SHA512:
6
+ metadata.gz: 8762d755093bf557a7ed4314f58d3321319ae3f09fc55837718733cd610d9b1a8bbabc4b59c26dfcd1048083261360a8fed14e90ae2fec6123eecbc9bbcbfb08
7
+ data.tar.gz: 4a17cbfc92b86766d26bf8be2f90a7ecba93fbcbdf6ae37a539f3eaf37e587b25b931488acce668f904c1b46c07f59217a0715838ebebe90bd052ed8da93e340
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # AllowedParams
2
+
3
+ This gem provides filtering and validations of params
4
+
5
+ ```ruby
6
+
7
+ class DogsController < ApplicationController
8
+ include AllowedParams::Helper
9
+
10
+ params do
11
+ allow :name, presence: true
12
+ allow :age
13
+ end
14
+ def update
15
+ # do the job
16
+ end
17
+ end
18
+
19
+ ```
20
+
21
+ This will validate `params[:name]` and raise `AllowedParams::ValidationError` in case of invalid value.
22
+ `age` param is allowed and not validated. All other params is not allowed.
23
+
24
+ To allow params on all controllers:
25
+
26
+ ```ruby
27
+
28
+ AllowedParams.config.allowed_params = [:format]
29
+
30
+ ```
31
+
32
+ This project rocks and uses MIT-LICENSE.
33
+
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AllowedParams'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
@@ -0,0 +1,36 @@
1
+ require_relative 'validator_builder'
2
+
3
+ module AllowedParams
4
+ ValidationError = Class.new(ArgumentError)
5
+ NotAllowedError = Class.new(ArgumentError)
6
+
7
+ module Helper
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ def params(&block)
12
+ builder = ::AllowedParams::ValidatorBuilder.new(self)
13
+ builder.instance_eval(&block) if block_given?
14
+ @request_params_validator = builder.validator
15
+ end
16
+
17
+ def method_added(method)
18
+ if instance_variable_get(:@request_params_validator).present?
19
+ request_params_validator = @request_params_validator
20
+ @request_params_validator = nil
21
+
22
+ before_filter only: [method] do
23
+ request_params = request_params_validator.new(params)
24
+ if request_params.invalid?
25
+ raise ValidationError, request_params.errors.full_messages.first
26
+ end
27
+ if request_params.not_allowed.present?
28
+ raise NotAllowedError, request_params.not_allowed.join(', ')
29
+ end
30
+ end
31
+ end
32
+ super
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,28 @@
1
+ module AllowedParams
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+ extend ActiveModel::Naming
5
+ extend ActiveModel::Translation
6
+ include ActiveModel::Conversion
7
+ include ActiveModel::Validations
8
+
9
+
10
+ module ClassMethods
11
+ def i18n_scope
12
+ :allowed_params
13
+ end
14
+ end
15
+
16
+ def i18n_scope
17
+ self.class.i18n_scope
18
+ end
19
+
20
+ def new_record?
21
+ true
22
+ end
23
+
24
+ def persisted?
25
+ false
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,49 @@
1
+ require_relative 'model'
2
+
3
+ module AllowedParams
4
+ include ActiveSupport::Configurable
5
+ config.allowed_params = []
6
+
7
+ class ValidatorBuilder
8
+ def initialize(controller)
9
+ @controller = controller
10
+ @params = {}
11
+ end
12
+
13
+ def allow(name, options = {})
14
+ @params[name] = options
15
+ end
16
+
17
+ def validator
18
+ params_options = @params
19
+ controller_name = @controller.name.underscore
20
+
21
+ Struct.new(*params_options.keys) do
22
+ include Model
23
+ attr_reader :params
24
+ @controller_name = controller_name
25
+
26
+ def self.model_name
27
+ ActiveModel::Name.new(self, nil, @controller_name)
28
+ end
29
+
30
+ params_options.each_pair do |name, validations|
31
+ if validations.present?
32
+ validates name, validations
33
+ end
34
+ end
35
+
36
+ def initialize(params = {})
37
+ @params = params.except(*Array(AllowedParams.config.allowed_params)).except(:controller, :action)
38
+ members.each do |name|
39
+ send(:"#{name}=", params[name])
40
+ end
41
+ end
42
+
43
+ def not_allowed
44
+ @params.keys.map(&:to_s) - members.map(&:to_s)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module AllowedParams
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'allowed_params/validator_builder'
2
+ require 'allowed_params/helper'
3
+
4
+ module AllowedParams
5
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: allowed_params
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tema Bolshakov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Allow to filter and validate params on rails controllers
70
+ email:
71
+ - abolshakov@spbtv.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - lib/allowed_params.rb
80
+ - lib/allowed_params/helper.rb
81
+ - lib/allowed_params/model.rb
82
+ - lib/allowed_params/validator_builder.rb
83
+ - lib/allowed_params/version.rb
84
+ homepage:
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Filter params on rails controllers
108
+ test_files: []
109
+ has_rdoc: