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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6242d6eb6f5c0c22537b466e81800c54ee01a1db
4
- data.tar.gz: c49e6795c45fbff6d788ac306450ac0797c79d33
3
+ metadata.gz: adcf26d9b1d1745070a32eb1f8571c3210e5e057
4
+ data.tar.gz: 340c6a88f0f73738044c2e8ae6b51c482f0d6921
5
5
  SHA512:
6
- metadata.gz: b7c223b0bd3bdfa98aa17f2e6897122c11bd7d3b9478ccdbddacd01108a4dbb0c7e1dd5f8be269585b1e31120594e88c4574e861100633bc6970cd1507151506
7
- data.tar.gz: 7b5605c1f6a6f66ac5a6becea52dbc25e40f1db964ba6dccd91e2a7fd6cfbb74b0531b263a58375817a3d4194ccc9f49e881bd4842889ff0b5f8671738295f09
6
+ metadata.gz: 2d885cf5947235413d44408024675493d910439ce6561b69a9ab27ad879a08bb5ad77cd241077b25b1a6b5e304b14228842624a919c8baa88b88203f889e51d2
7
+ data.tar.gz: 4202a2f4be00570a36e257bae65f092d9a448a7e453ab5de2a787a2706e47eebe822eba8393ca07af27a117a0b3ea62d953ce7e2eac12122c2ec739ecbb73f17
@@ -1,9 +1,8 @@
1
1
  module Fencepost
2
2
  module ActsAsFencepost
3
3
  extend ActiveSupport::Concern
4
-
5
4
  def fencepost
6
- @fencepost = Fencepost.new(params)
5
+ Fencepost.new(params)
7
6
  end
8
7
  end
9
8
  end
@@ -1,14 +1,12 @@
1
1
  module Fencepost
2
2
  class Fencepost
3
- attr_reader :params
3
+ attr_reader :gate
4
4
  cattr_accessor :model_list
5
- def initialize(hash)
6
- @params = hash
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 |deny_list = nil|
9
- ActionController::Parameters.new(params).require(
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 get_node(key, value)
49
- self.model_list.select {|k,v| v[key] == value}
37
+ def allow(elements)
38
+ gate.allow(elements)
39
+ self
50
40
  end
51
41
 
52
- def model_attributes(model)
53
- get_node(:model, model).values[0][:attributes]
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
@@ -1,3 +1,3 @@
1
1
  module Fencepost
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/fencepost.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "fencepost/fencepost"
2
2
  require "fencepost/acts_as_fencepost"
3
+ require "fencepost/gate"
3
4
 
4
5
  module Fencepost
5
6
  end
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.0
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-08 00:00:00.000000000 Z
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: