fencepost 0.0.1 → 0.1.0

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: 94ea0e1988b46a1e1155e30b71e278b10eb50ab5
4
- data.tar.gz: d786b93c63bd9af07b7d5ee87fb4b8104c790a77
3
+ metadata.gz: 6242d6eb6f5c0c22537b466e81800c54ee01a1db
4
+ data.tar.gz: c49e6795c45fbff6d788ac306450ac0797c79d33
5
5
  SHA512:
6
- metadata.gz: 75343d6944f670264e0a2524182e04f16672d36cdf7d55cc8a73e8218916777a21487cbf1545fcc59813a651f7b84782f08f4f14f7a3cc26bc9fa5db7f313f13
7
- data.tar.gz: 5c0e4be0d44aa1027516d6a083b95a7b2d8a0ba225b00667ab2aca46af2c4676c68a1b7da606b788a48b653701ea8c17386d7c34be5cff8ac24fcefe82642997
6
+ metadata.gz: b7c223b0bd3bdfa98aa17f2e6897122c11bd7d3b9478ccdbddacd01108a4dbb0c7e1dd5f8be269585b1e31120594e88c4574e861100633bc6970cd1507151506
7
+ data.tar.gz: 7b5605c1f6a6f66ac5a6becea52dbc25e40f1db964ba6dccd91e2a7fd6cfbb74b0531b263a58375817a3d4194ccc9f49e881bd4842889ff0b5f8671738295f09
@@ -1,35 +1,42 @@
1
1
  module Fencepost
2
2
  class Fencepost
3
- attr_reader :params, :model_list
3
+ attr_reader :params
4
+ cattr_accessor :model_list
4
5
  def initialize(hash)
5
- @model_list = {}
6
6
  @params = hash
7
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|
8
+ define_singleton_method self.class.method_name(model) do |deny_list = nil|
18
9
  ActionController::Parameters.new(params).require(
19
- param_key(model)).permit(
10
+ self.class.param_key(model)).permit(
20
11
  build_permits(model, []))
21
12
  end
22
13
  end
23
14
  end
24
15
 
25
-
26
16
  def self.models
27
17
  ActiveRecord::Base.descendants
28
18
  end
29
19
 
20
+ def self.generate_model_list
21
+ model_list = {}
22
+ models.each do |model|
23
+ model_list[self.param_key(model)] =
24
+ {
25
+ model: model,
26
+ attributes: self.attribute_keys(model),
27
+ demodulized_name: self.demodulized_name(model),
28
+ nested_collection_name: self.nested_collection_name(model),
29
+ nested_attributes_name: self.nested_attributes_name(model),
30
+ nested_attributes_options: self.nested_attributes_options(model)
31
+ }
32
+ end
33
+
34
+ model_list
35
+ end
36
+
30
37
  def build_permits(model, permits_array)
31
- attribute_keys(model).each {|k| permits_array << k }
32
- nested_attributes_options(model).each do |nao, value|
38
+ model_attributes(model).each {|k| permits_array << k }
39
+ model_nested_attributes(model).each do |nao, value|
33
40
  node = get_node(:nested_collection_name, nao)
34
41
  node = get_node(:demodulized_name, nao) if node.keys.size == 0
35
42
  node = node.values[0]
@@ -39,43 +46,50 @@ module Fencepost
39
46
  end
40
47
 
41
48
  def get_node(key, value)
42
- model_list.select {|k,v| v[key] == value}
49
+ self.model_list.select {|k,v| v[key] == value}
43
50
  end
44
51
 
52
+ def model_attributes(model)
53
+ get_node(:model, model).values[0][:attributes]
54
+ end
55
+
56
+ def model_nested_attributes(model)
57
+ get_node(:model, model).values[0][:nested_attributes_options]
58
+ end
45
59
  private
46
60
 
47
- def method_name(model)
61
+ def self.method_name(model)
48
62
  "#{model.name.underscore.gsub("/", "_")}_params".to_sym
49
63
  end
50
64
 
51
- def param_key(model)
65
+ def self.param_key(model)
52
66
  "#{model.name.underscore.gsub("/", "_")}".to_sym
53
67
  end
54
68
 
55
- def demodulized_name(model)
69
+ def self.demodulized_name(model)
56
70
  "#{model.name.demodulize.underscore.gsub("/", "_")}".to_sym
57
71
  end
58
72
 
59
- def nested_collection_name(model)
73
+ def self.nested_collection_name(model)
60
74
  "#{model.name.pluralize.demodulize.underscore.gsub("/", "_")}".to_sym
61
75
  end
62
76
 
63
- def nested_attributes_name(model)
77
+ def self.nested_attributes_name(model)
64
78
  a ="#{model.name.pluralize.demodulize.underscore.gsub("/", "_")}_attributes"
65
79
  a.to_sym
66
80
  end
67
81
 
68
- def always_forbidden_attributes
82
+ def self.always_forbidden_attributes
69
83
  [:id, :created_at, :updated_at, :created_by, :updated_by]
70
84
  end
71
85
 
72
- def attribute_keys(model)
86
+ def self.attribute_keys(model)
73
87
  a = model.new.attributes.keys.map {|k| k.to_sym} unless model.nil? || model.abstract_class
74
- (a || []) - always_forbidden_attributes
88
+ (a || []) - self.always_forbidden_attributes
75
89
  end
76
90
 
77
- def nested_attributes_options(model)
78
- opts = model.nested_attributes_options
91
+ def self.nested_attributes_options(model)
92
+ model.nested_attributes_options
79
93
  end
80
94
 
81
95
  end
@@ -1,3 +1,3 @@
1
1
  module Fencepost
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,20 @@
1
+ require 'yaml'
2
+
3
+ class FencepostConfigGenerator < Rails::Generators::Base
4
+ def create_initializer_file
5
+ Rails.application.eager_load!
6
+ create_file "config/initializers/fencepost.rb",
7
+ file_contents
8
+ end
9
+
10
+ def file_contents
11
+ <<-yaml
12
+ Rails.application.eager_load!
13
+ Fencepost::Fencepost.model_list = YAML.load(
14
+ <<-contents
15
+ #{Fencepost::Fencepost.generate_model_list.to_yaml}
16
+ contents
17
+ )
18
+ yaml
19
+ end
20
+ 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.0.1
4
+ version: 0.1.0
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-07 00:00:00.000000000 Z
11
+ date: 2013-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Dynamic strong parameter configuration for your AR models
56
70
  email:
57
71
  - helm.scott@gmail.com
@@ -63,6 +77,7 @@ files:
63
77
  - lib/fencepost/fencepost.rb
64
78
  - lib/fencepost/version.rb
65
79
  - lib/fencepost.rb
80
+ - lib/generators/fencepost_config_generator.rb
66
81
  - lib/tasks/fencepost_tasks.rake
67
82
  - MIT-LICENSE
68
83
  - Rakefile