param_auto_permit 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01952b3dd2695bad86cf1bca25ef5e545059f822
4
+ data.tar.gz: be98be6c1047accac0df1bbba6272e2726083c5f
5
+ SHA512:
6
+ metadata.gz: 8ff331d67f7dab9b188fd48038b0bf2353bbde3022364bdd8210ec9b2559fa0fd771185093b30fadd0891ab5b998875f0015c4d77e9a16085178f138c9cc42a2
7
+ data.tar.gz: 038f1f454a4657924ac1ffa42c2b5027e66f2f0086a1b6491e55bbcf1489451dd763b8eb3769df30ec1cb0efafa7b8009308654ce2ca2d59c21f9c09f966642e
@@ -0,0 +1 @@
1
+ require 'param_auto_permit/railtie'
@@ -0,0 +1,43 @@
1
+ require 'param_auto_permit/verifier'
2
+
3
+ module ParamAutoPermit
4
+ module FormBuilder
5
+ def self.included(base)
6
+ base.class_eval do
7
+ alias_method_chain :label, :auto_attribute_permit
8
+ alias_method_chain :submit, :auto_attribute_permit
9
+ end
10
+ end
11
+
12
+ def auto_permitted_attributes
13
+ @auto_permitted_attributes ||= []
14
+ end
15
+
16
+ def label_with_auto_attribute_permit(method, text = nil, options = {}, &block)
17
+ unless (text.is_a?(Hash) ? text : options)[:auto_permit] == false
18
+ auto_permitted_attributes << method
19
+ end
20
+ label_without_auto_attribute_permit(method, text, options, &block)
21
+ end
22
+
23
+ def submit_with_auto_attribute_permit(value = nil, options = {})
24
+ value, options = nil, value if value.is_a?(Hash)
25
+ include_permit_field = options.delete(:auto_permit) != false
26
+ submit_button = submit_without_auto_attribute_permit(value, options)
27
+ if include_permit_field == false
28
+ submit_button
29
+ else
30
+ submit_button.safe_concat(self.auto_permitted_attributes_field(options[:auto_permit] || {}))
31
+ end
32
+ end
33
+
34
+ def encoded_auto_permitted_attributes(options = {})
35
+ options[:form_id] ||= object.class.name
36
+ ParamAutoPermit.verifier.generate([options[:form_id].to_s, auto_permitted_attributes])
37
+ end
38
+
39
+ def auto_permitted_attributes_field(attribute_options = {}, field_options = {})
40
+ @template.hidden_field_tag("#{@object_name}[permitted_fields]", encoded_auto_permitted_attributes(attribute_options), field_options)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+ module ParamAutoPermit
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer 'param_auto_permit.initialize' do
5
+ ActiveSupport.on_load(:action_view) do
6
+ require 'param_auto_permit/form_builder'
7
+ ActionView::Helpers::FormBuilder.send :include, ParamAutoPermit::FormBuilder
8
+ end
9
+
10
+ ActiveSupport.on_load(:action_controller) do
11
+ require 'param_auto_permit/strong_parameters'
12
+ ActionController::Parameters.send :include, ParamAutoPermit::StrongParameters
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ require 'param_auto_permit/verifier'
2
+
3
+ module ParamAutoPermit
4
+ module StrongParameters
5
+
6
+ class InvalidModelProvided < StandardError; end
7
+
8
+ def auto_permit(required_form_id, *filters)
9
+ if self['permitted_fields']
10
+ form_id, fields = ParamAutoPermit.verifier.verify(self['permitted_fields'])
11
+ unless form_id == required_form_id
12
+ raise InvalidModelProvided, "Form ID was `#{form_id}` but should be `#{required_form_id}`"
13
+ end
14
+ filters = filters | fields
15
+ end
16
+ permit(*filters)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_support/message_verifier'
2
+
3
+ module ParamAutoPermit
4
+
5
+ def self.verifier
6
+ @verifier ||= ActiveSupport::MessageVerifier.new(Rails.application.secrets.secret_key_base)
7
+ end
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: param_auto_permit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Rails extension to avoid duplicating attribute lists between forms
14
+ & controllers
15
+ email:
16
+ - me@adamcooke.io
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/param_auto_permit.rb
22
+ - lib/param_auto_permit/form_builder.rb
23
+ - lib/param_auto_permit/railtie.rb
24
+ - lib/param_auto_permit/strong_parameters.rb
25
+ - lib/param_auto_permit/verifier.rb
26
+ homepage: https://github.com/adamcooke/param-auto-permit
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.4.5
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A Rails extension to avoid duplicating attribute lists between forms & controllers
50
+ test_files: []