fencepost 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: 94ea0e1988b46a1e1155e30b71e278b10eb50ab5
4
+ data.tar.gz: d786b93c63bd9af07b7d5ee87fb4b8104c790a77
5
+ SHA512:
6
+ metadata.gz: 75343d6944f670264e0a2524182e04f16672d36cdf7d55cc8a73e8218916777a21487cbf1545fcc59813a651f7b84782f08f4f14f7a3cc26bc9fa5db7f313f13
7
+ data.tar.gz: 5c0e4be0d44aa1027516d6a083b95a7b2d8a0ba225b00667ab2aca46af2c4676c68a1b7da606b788a48b653701ea8c17386d7c34be5cff8ac24fcefe82642997
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 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 = 'Fencepost'
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,11 @@
1
+ module Fencepost
2
+ module ActsAsFencepost
3
+ extend ActiveSupport::Concern
4
+
5
+ def fencepost
6
+ @fencepost = Fencepost.new(params)
7
+ end
8
+ end
9
+ end
10
+
11
+ ActionController::Base.send :include, Fencepost::ActsAsFencepost
@@ -0,0 +1,82 @@
1
+ module Fencepost
2
+ class Fencepost
3
+ attr_reader :params, :model_list
4
+ def initialize(hash)
5
+ @model_list = {}
6
+ @params = hash
7
+ self.class.models.each do |model|
8
+ model_list[param_key(model)] =
9
+ {
10
+ model: model,
11
+ attributes: (),
12
+ demodulized_name: demodulized_name(model),
13
+ nested_collection_name: nested_collection_name(model),
14
+ nested_attributes_name: nested_attributes_name(model),
15
+ nested_attributes_options: nested_attributes_options(model)
16
+ }
17
+ define_singleton_method method_name(model) do |deny_list = nil|
18
+ ActionController::Parameters.new(params).require(
19
+ param_key(model)).permit(
20
+ build_permits(model, []))
21
+ end
22
+ end
23
+ end
24
+
25
+
26
+ def self.models
27
+ ActiveRecord::Base.descendants
28
+ end
29
+
30
+ def build_permits(model, permits_array)
31
+ attribute_keys(model).each {|k| permits_array << k }
32
+ nested_attributes_options(model).each do |nao, value|
33
+ node = get_node(:nested_collection_name, nao)
34
+ node = get_node(:demodulized_name, nao) if node.keys.size == 0
35
+ node = node.values[0]
36
+ permits_array << {node[:nested_attributes_name] => build_permits(node[:model], [])}
37
+ end
38
+ permits_array
39
+ end
40
+
41
+ def get_node(key, value)
42
+ model_list.select {|k,v| v[key] == value}
43
+ end
44
+
45
+ private
46
+
47
+ def method_name(model)
48
+ "#{model.name.underscore.gsub("/", "_")}_params".to_sym
49
+ end
50
+
51
+ def param_key(model)
52
+ "#{model.name.underscore.gsub("/", "_")}".to_sym
53
+ end
54
+
55
+ def demodulized_name(model)
56
+ "#{model.name.demodulize.underscore.gsub("/", "_")}".to_sym
57
+ end
58
+
59
+ def nested_collection_name(model)
60
+ "#{model.name.pluralize.demodulize.underscore.gsub("/", "_")}".to_sym
61
+ end
62
+
63
+ def nested_attributes_name(model)
64
+ a ="#{model.name.pluralize.demodulize.underscore.gsub("/", "_")}_attributes"
65
+ a.to_sym
66
+ end
67
+
68
+ def always_forbidden_attributes
69
+ [:id, :created_at, :updated_at, :created_by, :updated_by]
70
+ end
71
+
72
+ def attribute_keys(model)
73
+ a = model.new.attributes.keys.map {|k| k.to_sym} unless model.nil? || model.abstract_class
74
+ (a || []) - always_forbidden_attributes
75
+ end
76
+
77
+ def nested_attributes_options(model)
78
+ opts = model.nested_attributes_options
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module Fencepost
2
+ VERSION = "0.0.1"
3
+ end
data/lib/fencepost.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "fencepost/fencepost"
2
+ require "fencepost/acts_as_fencepost"
3
+
4
+ module Fencepost
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :fencepost do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fencepost
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Scott Helm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-07 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.1
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.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Dynamic strong parameter configuration for your AR models
56
+ email:
57
+ - helm.scott@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/fencepost/acts_as_fencepost.rb
63
+ - lib/fencepost/fencepost.rb
64
+ - lib/fencepost/version.rb
65
+ - lib/fencepost.rb
66
+ - lib/tasks/fencepost_tasks.rake
67
+ - MIT-LICENSE
68
+ - Rakefile
69
+ homepage: https://github.com/scotthelm/fencepost
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.3
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Rails 4.x strong parameter configuration for your AR models
93
+ test_files: []