parameter_sets 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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cf4d020e1fd5ac660c72ce5c00e973237c2a469cf6ec326b26a17c0de3c58564
4
+ data.tar.gz: f92a2eb6dde40a6cefffdac63ec8eefe8deb96a2edb2feffedaaae266f6a046f
5
+ SHA512:
6
+ metadata.gz: d3407e09f74ca259275ed27b99ee5a3cb749bf70e1d77420360dbec78dd5b1d3eb1c7a78af00432c9b7e861272ac2f4d134403510e40191bb0e610bd4a457beb
7
+ data.tar.gz: 73d67bda692338e7bfd5e5e39e72caf78e0f8bb4ca0d47491d16dfe01ea987853a74f46b5557f82f02da868294f1bdc6c799b99b56bc5052c687153c7e15b44b
@@ -0,0 +1,17 @@
1
+ require 'parameter_sets/schema'
2
+
3
+ module ParameterSets
4
+ class ControllerClassDelegate
5
+
6
+ def initialize(controller_class)
7
+ @controller_class = controller_class
8
+ end
9
+
10
+ def param_set(name, options = {}, &block)
11
+ name = name.to_sym
12
+ schema = Schema.new(name, options, &block)
13
+ @controller_class.parameter_sets[name] = schema
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,36 @@
1
+ require 'parameter_sets/controller_instance_delegate'
2
+ require 'parameter_sets/controller_class_delegate'
3
+
4
+ module ParameterSets
5
+ module ControllerExtension
6
+
7
+ module InstanceMethods
8
+ def param_set(*args)
9
+ parameter_sets_delegate.param_set(*args)
10
+ end
11
+
12
+ private
13
+
14
+ def parameter_sets_delegate
15
+ @parameter_sets_delegate ||= ControllerInstanceDelegate.new(self)
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+ def param_set(*args, &block)
21
+ parameter_sets_delegate.param_set(*args, &block)
22
+ end
23
+
24
+ def parameter_sets
25
+ @parameter_sets ||= {}
26
+ end
27
+
28
+ private
29
+
30
+ def parameter_sets_delegate
31
+ @parameter_sets_delegate ||= ControllerClassDelegate.new(self)
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ module ParameterSets
2
+ class ControllerInstanceDelegate
3
+
4
+ def initialize(controller)
5
+ @controller = controller
6
+ end
7
+
8
+ def param_set(name, object = nil, options = {})
9
+ if name.is_a?(Symbol) || name.is_a?(String)
10
+ if schema = @controller.class.parameter_sets[name.to_sym]
11
+ schema.parameters(@controller, object, options)
12
+ else
13
+ raise ParameterSetNotDefinedError, "No parameter set named #{name} is defined"
14
+ end
15
+ else
16
+ param_set_for(name)
17
+ end
18
+ end
19
+
20
+ def param_set_for(object, options = {})
21
+ param_set_name = object.model_name.param_key
22
+ param_set(param_set_name.to_sym, object, options)
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ module ParameterSets
2
+ class Error < StandardError
3
+ end
4
+
5
+ class NoParametersPermittedError < Error
6
+ end
7
+
8
+ class ParameterSetNotDefinedError < Error
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ module ParameterSets
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer 'parameter_sets.initialize' do
5
+ ActiveSupport.on_load(:action_controller) do
6
+ require 'parameter_sets/controller_extension'
7
+ include ParameterSets::ControllerExtension::InstanceMethods
8
+ extend ParameterSets::ControllerExtension::ClassMethods
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ require 'parameter_sets/error'
2
+ require 'parameter_sets/schema_dsl'
3
+
4
+ module ParameterSets
5
+ class Schema
6
+
7
+ def initialize(name, options = {}, &block)
8
+ @name = name
9
+ @options = options
10
+ @block = block
11
+ end
12
+
13
+ # Return the base parameter name to get from the request's parameters.
14
+ def base_param_name
15
+ @options[:param_name] || @name
16
+ end
17
+
18
+ # Return a suitablely scoped ActionController::Parameters object based on the
19
+ # rules defined for this param set.
20
+ #
21
+ # @param controller [ActionController::Base] the controller we're working within
22
+ # @param object [ActiveModel::Base] an object to generate attributes for
23
+ def parameters(controller, object = nil, options = {})
24
+ dsl = SchemaDSL.new
25
+ controller.instance_exec(dsl, object, options, &@block) if @block
26
+ if dsl.fields.empty?
27
+ raise NoParametersPermittedError, "No fields were permitted"
28
+ else
29
+ controller.params.require(base_param_name).permit(*dsl.fields)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ module ParameterSets
2
+ class SchemaDSL
3
+
4
+ def initialize
5
+ @fields = []
6
+ end
7
+
8
+ attr_reader :fields
9
+
10
+ def permit(field, *other_fields)
11
+ if other_fields[0].is_a?(Symbol)
12
+ permit(field)
13
+ other_fields.each { |field| permit(field) }
14
+ else
15
+ if options = other_fields[0]
16
+ @fields << {field => options}
17
+ else
18
+ @fields << field
19
+ end
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module ParameterSets
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,3 @@
1
+ if defined?(Rails)
2
+ require 'parameter_sets/railtie'
3
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parameter_sets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A friendly schema for defining permitted parameters in Rails.
14
+ email:
15
+ - me@adamcooke.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/parameter_sets.rb
21
+ - lib/parameter_sets/controller_class_delegate.rb
22
+ - lib/parameter_sets/controller_extension.rb
23
+ - lib/parameter_sets/controller_instance_delegate.rb
24
+ - lib/parameter_sets/error.rb
25
+ - lib/parameter_sets/railtie.rb
26
+ - lib/parameter_sets/schema.rb
27
+ - lib/parameter_sets/schema_dsl.rb
28
+ - lib/parameter_sets/version.rb
29
+ homepage: https://github.com/adamcooke/strong_parameters
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.7.4
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: This gem provides a simple framework allowing greater controler over which
53
+ parameters to allow in Active Model mass assignment.
54
+ test_files: []