simple_form_strong_parameters 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: 85f112d5e6955d0d29743de3a357463fc0c1ad08
4
+ data.tar.gz: d7847642ed540f3ff447bd9f2c59277f7dc81d30
5
+ SHA512:
6
+ metadata.gz: 74ab87ca95f073d7ca145ff66a48b980d9ba993c59de8479b4aaf4f765ba9d48144fe2d770332e6c1df91ead0b34f1fdb1a3bea9d68f86eb3586d1a6531f3cee
7
+ data.tar.gz: 088253b3798be73fcdf0a8a383f86520efa44c622cdf55cfeb7409345b0cc440c382bb632a83f287908f1ded83688531671e64f11617cb686ce1f80c9033d037
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/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 = 'SimpleFormStrongParameters'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
@@ -0,0 +1,59 @@
1
+ module SimpleFormStrongParameters::ControllerHelper
2
+ def simple_form_strong_parameters_data
3
+ url = request.path
4
+
5
+ if !session[:simple_form_strong_parameters_storage] || !session[:simple_form_strong_parameters_storage][url]
6
+ raise ActiveModel::ForbiddenAttributesError, "No strong attributes data was detected for: '#{url}'. Allowed was #{session[:simple_form_strong_parameters_storage].keys.join(", ")}."
7
+ end
8
+
9
+ return session[:simple_form_strong_parameters_storage][url]
10
+ end
11
+
12
+ def simple_form_strong_parameters namespace
13
+ hash = params.require(namespace)
14
+ hash = permit_from_simple_form(hash, simple_form_strong_parameters_data[namespace])
15
+ hash.permit!
16
+
17
+ return hash
18
+ end
19
+
20
+ private
21
+
22
+ def permit_from_simple_form hash_params, allowed_params
23
+ raise "'hash_params' was not set?" unless hash_params
24
+
25
+ # Add allowed attributes.
26
+ allowed_attributes = allowed_params[:attributes].map(&:to_s)
27
+
28
+ allowed_params[:subs].each do |key, val|
29
+ # Scan rest of hash recursive.
30
+ permit_key = val[:write_namespace]
31
+ sub_hash_params = hash_params[permit_key]
32
+ raise "'#{permit_key}' did not exist in '#{hash_params}'." unless sub_hash_params
33
+
34
+ if sub_hash_params.is_a?(Array) && permit_key.to_s.match(/_attributes\Z/)
35
+ sub_hash_params.each do |sub_hash_attributes|
36
+ permit_from_simple_form(sub_hash_attributes, val)
37
+ end
38
+ else
39
+ permit_from_simple_form(sub_hash_params, val)
40
+ end
41
+
42
+ # Add namespace to allowed attributes.
43
+ allowed_attributes << val[:write_namespace].to_s
44
+ end
45
+
46
+ # Check if the given keys are allowed.
47
+ check_hash_for_illegal_attributes(hash_params, allowed_attributes)
48
+ end
49
+
50
+ def check_hash_for_illegal_attributes hash, allowed_attributes
51
+ hash.each do |key, val|
52
+ if !allowed_attributes.include?(key.to_s)
53
+ raise ActiveModel::ForbiddenAttributesError, "Illegal attribute: '#{key}' for allowed '#{allowed_attributes}'."
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ ActionController::Base.send :include, SimpleFormStrongParameters::ControllerHelper
@@ -0,0 +1,20 @@
1
+ module SimpleFormStrongParameters::FormHelper
2
+ def simple_form_strong_parameters_for(*args)
3
+ sfsp = SimpleFormStrongParameters::FormProxy.new(object: args[0], first: true)
4
+ storage = session[:simple_form_strong_parameters_storage] ||= {}
5
+
6
+ simple_form_for *args do |f|
7
+ url = f.options[:url]
8
+
9
+ url_storage = storage[url] ||= {}
10
+ ns_storage = url_storage[sfsp.namespace] ||= {}
11
+
12
+ sfsp.simple_form = f
13
+ sfsp.session_var = ns_storage
14
+
15
+ yield sfsp
16
+ end
17
+ end
18
+ end
19
+
20
+ ActionView::Base.send :include, SimpleFormStrongParameters::FormHelper
data/lib/form_proxy.rb ADDED
@@ -0,0 +1,75 @@
1
+ require "string-cases"
2
+
3
+ class SimpleFormStrongParameters::FormProxy
4
+ attr_accessor :simple_form
5
+
6
+ def initialize args
7
+ @args = args
8
+ @object = args[:object]
9
+ end
10
+
11
+ def session_var=(session_var)
12
+ @session_var = session_var
13
+ @session_var[:attributes] = []
14
+ @session_var[:write_namespace] = write_namespace
15
+ @session_var[:subs] = {}
16
+ end
17
+
18
+ def method_missing name, *args, &blk
19
+ if name == :input
20
+ attribute_name = args[0]
21
+ register_parameter(attribute_name)
22
+ end
23
+
24
+ @simple_form.__send__(name, *args, &blk)
25
+ end
26
+
27
+ def simple_fields_for *args
28
+ fields_namespace = args[0]
29
+ fields_session_var = {}
30
+
31
+ @simple_form.simple_fields_for *args do |fields_simple_form|
32
+ proxy = SimpleFormStrongParameters::FormProxy.new(object: fields_namespace)
33
+ proxy.simple_form = fields_simple_form
34
+ proxy.session_var = fields_session_var
35
+
36
+ begin
37
+ yield proxy
38
+ ensure
39
+ @session_var[:subs][proxy.namespace] = fields_session_var
40
+ end
41
+ end
42
+ end
43
+
44
+ def namespace
45
+ if @object.is_a? ActiveRecord::Base
46
+ return StringCases.camel_to_snake(@object.class.name).to_sym
47
+ elsif @object.is_a? Symbol
48
+ return @object
49
+ else
50
+ raise "Doesn't know how to handle: #{@object.class.name}"
51
+ end
52
+ end
53
+
54
+ def write_namespace
55
+ if @object.is_a? ActiveRecord::Base
56
+ if @args[:first]
57
+ return StringCases.camel_to_snake(@object.class.name).to_sym
58
+ end
59
+
60
+ class_name = StringCases.camel_to_snake(@object.class.name).pluralize
61
+ namespace = "#{class_name}_attributes".to_sym
62
+ return namespace
63
+ elsif @object.is_a? Symbol
64
+ return @object
65
+ else
66
+ raise "Doesn't know how to handle: #{@object.class.name}"
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def register_parameter name
73
+ @session_var[:attributes] << name
74
+ end
75
+ end
@@ -0,0 +1,8 @@
1
+ require "simple_form"
2
+
3
+ module SimpleFormStrongParameters
4
+ end
5
+
6
+ require_relative "controller_helper"
7
+ require_relative "form_helper"
8
+ require_relative "form_proxy"
@@ -0,0 +1,3 @@
1
+ module SimpleFormStrongParameters
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :simple_form_strong_parameters do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_form_strong_parameters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Johansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-23 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.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: simple_form
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: string-cases
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: sqlite3
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
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Detecting inputs from simple form, saving them in a session in order
98
+ to do automatic strong parameters in the controller.
99
+ email:
100
+ - k@spernj.org
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - MIT-LICENSE
106
+ - Rakefile
107
+ - lib/controller_helper.rb
108
+ - lib/form_helper.rb
109
+ - lib/form_proxy.rb
110
+ - lib/simple_form_strong_parameters.rb
111
+ - lib/simple_form_strong_parameters/version.rb
112
+ - lib/tasks/simple_form_strong_parameters_tasks.rake
113
+ homepage: https://www.github.com/kaspernj/simple_form_strong_parameters
114
+ licenses: []
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.0
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Automatic handeling of strong parameters with simple form.
136
+ test_files: []