better_strong_params 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MGVjYWI4MWNmN2M5ZTM3NjRmNGFmYjVmMTZjMzhhN2U5OGQ5NWVjZg==
5
+ data.tar.gz: !binary |-
6
+ Y2U1ZTFhMTRmN2Y3Yzg1YzU5OTFmMzAxNzIxYzc0ZGZiMjJlODM3OQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MGRlNWYzZTU3ZjUwN2YxYmRjNDA0NWM2OTgwNGEzYjAxOWY5ZTZjY2NjZGIy
10
+ Yjc2NGM0MWE3MjViNzJkNGRjMGNhN2FmNTlhZjUyODk1MDE1M2YyMzU4ZGIw
11
+ ODk5ZDc0ZDhmNzgwZjFkNDQxNDA5NGE1MTA5NjJkZTczNGFkZDc=
12
+ data.tar.gz: !binary |-
13
+ YmMyNjEyN2IwOTBkODNjODkyNGJiN2M4YWFhN2E1YWZmMTg5MDRjOGM5Nzlh
14
+ ZTA3YjkzZTViNmM2OWIyMDljOTUxM2RiMTgwNTg0YzhkNDdmODE3YzBjNWQx
15
+ OWQ0NmY1OWFmNjhiNzM3YzJkMGQ2NWFmN2IxOTM4ZjE0OGM0YmY=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in better_strong_params.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Elad Meidar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # BetterStrongParams
2
+
3
+ Adds a DSL to ActionController that allows to add strong params filtering without adding a specific, dirty controller method.
4
+
5
+ ## Why?
6
+
7
+ [Rails's Strong Parameters](http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html) is obviously a better approach than having the model decide which attributes are protected or not. The only problem I have with this is the need to add an ugly method (at least one) to the controller to filter and whitelist the parameters.
8
+
9
+ BetterStrongParams is simply a way to create those methods via DSL instead of a manually typing them, seems like it is a more naturally looking interface for a good idea.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'better_strong_params'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install better_strong_params
24
+
25
+ ## Setting up
26
+
27
+ BetterStrongParams is automatically included in ActionController::Base, so you don't actually need to do anything to set this one up.
28
+
29
+ ## Usage
30
+
31
+ BetterStrongParams allows you to use the `filter_parameters` method in your controllers:
32
+
33
+ ```ruby
34
+ class UsersController < ApplicationController
35
+ filter_parameters create: {post: [:title, :body]}, ...
36
+ end
37
+ ```
38
+
39
+ `filter_parameters` accepts an option hash specifing a method name, a _required_ parameter and a permitted parameters list like this
40
+
41
+ ```
42
+ controller_action_name: {required_param_name: [permitted1, permitted2,...]}, controller_action_name2: ....
43
+ ```
44
+
45
+ for every controller action you set using `filter_parameters`, a method named `#{controller_action}_params` will be created and will be ready to use when you want it.
46
+
47
+
48
+ ### Full example
49
+ ```ruby
50
+ class UsersController < ApplicationController
51
+
52
+ filter_parameters create: {post: [:title, :body]}
53
+
54
+ def create
55
+ @user = User.new(create_params) # => create_params is available via BetterStrongParams and the filter_parameters DSL.
56
+ if @user.save
57
+ redirect_to treasure_url
58
+ else
59
+ redirect_to jail_url
60
+ end
61
+ end
62
+ end
63
+ ```
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it ( http://github.com/<my-github-username>/better_strong_params/fork )
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'better_strong_params/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "better_strong_params"
8
+ spec.version = BetterStrongParams::VERSION
9
+ spec.authors = ["Elad Meidar"]
10
+ spec.email = ["elad@eizesus.com"]
11
+ spec.summary = "Adds a DSL to ActionController that allows to add strong params filtering without adding a specific, dirty controller method."
12
+ spec.description = ""
13
+ spec.homepage = "https://github.com/ShinobiDevs/better_strong_params"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'rails'
25
+ end
@@ -0,0 +1,3 @@
1
+ module BetterStrongParams
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ require "better_strong_params/version"
2
+
3
+ module BetterStrongParams
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ extend ClassMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def filter_parameters(options = {})
13
+ action_names = options.keys
14
+ action_names.each do |action_name|
15
+ define_method("#{action_name.to_s}_params") do
16
+ base = options[action_name].keys.first
17
+ params.require(base).permit(*options[action_name][base])
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ if defined?(ActionController)
26
+ ActionController::Base.send(:include, BetterStrongParams)
27
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,10 @@
1
+ class UsersController < ApplicationController
2
+
3
+ def create
4
+ @user = User.new(params[:user])
5
+ render nothing: true, status: :created
6
+ end
7
+
8
+
9
+
10
+ end
@@ -0,0 +1,27 @@
1
+ class User
2
+ # It quacks like ActiveModel...
3
+ extend ActiveModel::Naming
4
+ include ActiveModel::Conversion
5
+ include ActiveModel::Validations
6
+ # Mass-assignment protection using Strong Params
7
+ include ActiveModel::MassAssignmentSecurity
8
+ include ActiveModel::ForbiddenAttributesProtection
9
+
10
+ attr_accessor :name, :age, :a_hidden_secret
11
+
12
+
13
+ def initialize(values = {})
14
+ assign_attributes(values)
15
+ end
16
+
17
+ def assign_attributes(values, options = {})
18
+ sanitize_for_mass_assignment(values, options[:as]).each do |k, v|
19
+ send("#{k}=", v)
20
+ end
21
+ end
22
+
23
+ def persisted?
24
+ false
25
+ end
26
+
27
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ require 'fixtures/controllers/application_controller'
4
+ require 'fixtures/controllers/users_controller'
5
+ require 'fixtures/models/user'
6
+
7
+ describe BetterStrongParams do
8
+ end
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'active_model'
5
+ require 'active_support'
6
+ require 'active_support/test_case'
7
+ require 'action_controller'
8
+ require 'better_strong_params' # and any other gems you need
9
+
10
+ RSpec.configure do |config|
11
+ config.expect_with :rspec do |c|
12
+ c.syntax = :should
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_strong_params
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Elad Meidar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: 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: ''
70
+ email:
71
+ - elad@eizesus.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - better_strong_params.gemspec
83
+ - lib/better_strong_params.rb
84
+ - lib/better_strong_params/version.rb
85
+ - spec/fixtures/controllers/application_controller.rb
86
+ - spec/fixtures/controllers/users_controller.rb
87
+ - spec/fixtures/models/user.rb
88
+ - spec/lib/better_strong_params_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/ShinobiDevs/better_strong_params
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.0.7
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Adds a DSL to ActionController that allows to add strong params filtering
114
+ without adding a specific, dirty controller method.
115
+ test_files:
116
+ - spec/fixtures/controllers/application_controller.rb
117
+ - spec/fixtures/controllers/users_controller.rb
118
+ - spec/fixtures/models/user.rb
119
+ - spec/lib/better_strong_params_spec.rb
120
+ - spec/spec_helper.rb