fencepost 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fencepost/acts_as_fencepost.rb +1 -2
- data/lib/fencepost/fencepost.rb +18 -25
- data/lib/fencepost/gate.rb +88 -0
- data/lib/fencepost/version.rb +1 -1
- data/lib/fencepost.rb +1 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adcf26d9b1d1745070a32eb1f8571c3210e5e057
|
4
|
+
data.tar.gz: 340c6a88f0f73738044c2e8ae6b51c482f0d6921
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d885cf5947235413d44408024675493d910439ce6561b69a9ab27ad879a08bb5ad77cd241077b25b1a6b5e304b14228842624a919c8baa88b88203f889e51d2
|
7
|
+
data.tar.gz: 4202a2f4be00570a36e257bae65f092d9a448a7e453ab5de2a787a2706e47eebe822eba8393ca07af27a117a0b3ea62d953ce7e2eac12122c2ec739ecbb73f17
|
data/lib/fencepost/fencepost.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
module Fencepost
|
2
2
|
class Fencepost
|
3
|
-
attr_reader :
|
3
|
+
attr_reader :gate
|
4
4
|
cattr_accessor :model_list
|
5
|
-
def initialize(
|
6
|
-
@
|
5
|
+
def initialize(params)
|
6
|
+
@gate = Gate.new(self.class.model_list, params)
|
7
7
|
self.class.models.each do |model|
|
8
|
-
define_singleton_method self.class.method_name(model) do
|
9
|
-
|
10
|
-
self.class.param_key(model)).permit(
|
11
|
-
build_permits(model, []))
|
8
|
+
define_singleton_method self.class.method_name(model) do
|
9
|
+
gate.open(params,self.class.param_key(model), model)
|
12
10
|
end
|
13
11
|
end
|
14
12
|
end
|
@@ -27,35 +25,25 @@ module Fencepost
|
|
27
25
|
demodulized_name: self.demodulized_name(model),
|
28
26
|
nested_collection_name: self.nested_collection_name(model),
|
29
27
|
nested_attributes_name: self.nested_attributes_name(model),
|
30
|
-
nested_attributes_options: self.nested_attributes_options(model)
|
28
|
+
nested_attributes_options: self.nested_attributes_options(model),
|
29
|
+
nested_singular_name: self.nested_singular_attributes_name(model)
|
31
30
|
}
|
32
31
|
end
|
33
32
|
|
34
33
|
model_list
|
35
34
|
end
|
36
35
|
|
37
|
-
def build_permits(model, permits_array)
|
38
|
-
model_attributes(model).each {|k| permits_array << k }
|
39
|
-
model_nested_attributes(model).each do |nao, value|
|
40
|
-
node = get_node(:nested_collection_name, nao)
|
41
|
-
node = get_node(:demodulized_name, nao) if node.keys.size == 0
|
42
|
-
node = node.values[0]
|
43
|
-
permits_array << {node[:nested_attributes_name] => build_permits(node[:model], [])}
|
44
|
-
end
|
45
|
-
permits_array
|
46
|
-
end
|
47
36
|
|
48
|
-
def
|
49
|
-
|
37
|
+
def allow(elements)
|
38
|
+
gate.allow(elements)
|
39
|
+
self
|
50
40
|
end
|
51
41
|
|
52
|
-
def
|
53
|
-
|
42
|
+
def deny(elements)
|
43
|
+
gate.deny(elements)
|
44
|
+
self
|
54
45
|
end
|
55
46
|
|
56
|
-
def model_nested_attributes(model)
|
57
|
-
get_node(:model, model).values[0][:nested_attributes_options]
|
58
|
-
end
|
59
47
|
private
|
60
48
|
|
61
49
|
def self.method_name(model)
|
@@ -79,6 +67,11 @@ module Fencepost
|
|
79
67
|
a.to_sym
|
80
68
|
end
|
81
69
|
|
70
|
+
def self.nested_singular_attributes_name(model)
|
71
|
+
a ="#{model.name.demodulize.underscore.gsub("/", "_")}_attributes"
|
72
|
+
a.to_sym
|
73
|
+
end
|
74
|
+
|
82
75
|
def self.always_forbidden_attributes
|
83
76
|
[:id, :created_at, :updated_at, :created_by, :updated_by]
|
84
77
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Fencepost
|
2
|
+
class Gate
|
3
|
+
attr_reader :allow_array, :deny_array, :model_list, :model
|
4
|
+
def initialize(model_list, params)
|
5
|
+
@allow_array = []
|
6
|
+
@deny_array = []
|
7
|
+
@model_list = model_list
|
8
|
+
@model = model
|
9
|
+
end
|
10
|
+
|
11
|
+
def open(params, key, model)
|
12
|
+
perms = deny_permissions(allow_permissions(build_permits(model, [])))
|
13
|
+
ActionController::Parameters.new(params).require(key).permit(perms)
|
14
|
+
end
|
15
|
+
|
16
|
+
def allow(*elements)
|
17
|
+
@allow_array += elements
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def deny(*elements)
|
22
|
+
@deny_array += elements
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def allow_permissions(perms)
|
27
|
+
hash_values(allow_array).each do |hash_value|
|
28
|
+
perms = nested_operations(perms, hash_value, :+)
|
29
|
+
end
|
30
|
+
perms + scalar_values(allow_array)
|
31
|
+
end
|
32
|
+
|
33
|
+
def deny_permissions(perms)
|
34
|
+
hash_values(deny_array).each do |hash_value|
|
35
|
+
perms = nested_operations(perms, hash_value, :-)
|
36
|
+
end
|
37
|
+
perms - scalar_values(deny_array)
|
38
|
+
end
|
39
|
+
|
40
|
+
def scalar_values(arry)
|
41
|
+
arry.select{|x| x.is_a?(String) || x.is_a?(Symbol)}
|
42
|
+
end
|
43
|
+
|
44
|
+
def hash_values(arry)
|
45
|
+
arry.select{|x| x.is_a?(Hash)}
|
46
|
+
end
|
47
|
+
|
48
|
+
def nested_operations(perms, hash, operator)
|
49
|
+
perms.each do |perm|
|
50
|
+
hash.each do |key, value|
|
51
|
+
set_permission_value(perm, key, value, operator)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_permission_value(perm, key, value, operator)
|
57
|
+
if perm.is_a?(Hash) && perm.keys.index(key) && perm[key].is_a?(Array)
|
58
|
+
perm[key] = perm[key].send(operator, value)
|
59
|
+
elsif perm.is_a?(Hash) && perm.keys.index(key) && perm[key].is_a?(Hash)
|
60
|
+
perm[key] = nested_denies(perm[key], value)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def build_permits(model, permits_array)
|
65
|
+
model_attributes(model).each {|k| permits_array << k }
|
66
|
+
model_nested_attributes(model).each do |nao, value|
|
67
|
+
node = get_node(:nested_collection_name, nao)
|
68
|
+
node = get_node(:demodulized_name, nao) if node.keys.size == 0
|
69
|
+
node = node.values[0]
|
70
|
+
permits_array << {node[:nested_attributes_name] => build_permits(node[:model], [])}
|
71
|
+
end
|
72
|
+
permits_array
|
73
|
+
end
|
74
|
+
|
75
|
+
def model_attributes(model)
|
76
|
+
get_node(:model, model).values[0][:attributes]
|
77
|
+
end
|
78
|
+
|
79
|
+
def model_nested_attributes(model)
|
80
|
+
get_node(:model, model).values[0][:nested_attributes_options]
|
81
|
+
end
|
82
|
+
|
83
|
+
def get_node(key, value)
|
84
|
+
self.model_list.select {|k,v| v[key] == value}
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
data/lib/fencepost/version.rb
CHANGED
data/lib/fencepost.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fencepost
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Helm
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-plus
|
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'
|
69
83
|
description: Dynamic strong parameter configuration for your AR models
|
70
84
|
email:
|
71
85
|
- helm.scott@gmail.com
|
@@ -75,6 +89,7 @@ extra_rdoc_files: []
|
|
75
89
|
files:
|
76
90
|
- lib/fencepost/acts_as_fencepost.rb
|
77
91
|
- lib/fencepost/fencepost.rb
|
92
|
+
- lib/fencepost/gate.rb
|
78
93
|
- lib/fencepost/version.rb
|
79
94
|
- lib/fencepost.rb
|
80
95
|
- lib/generators/fencepost_config_generator.rb
|
@@ -106,3 +121,4 @@ signing_key:
|
|
106
121
|
specification_version: 4
|
107
122
|
summary: Rails 4.x strong parameter configuration for your AR models
|
108
123
|
test_files: []
|
124
|
+
has_rdoc:
|