Module: ActiveParams
- Defined in:
- lib/active_params.rb,
lib/active_params/version.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Class Method Summary collapse
-
.combine_hashes(array_of_hashes) ⇒ Object
to obtain a hash of all possible keys.
- .development_mode? ⇒ Boolean
- .global_config ⇒ Object
- .included(base) ⇒ Object
- .path ⇒ Object
- .strong_params_definition_for(params) ⇒ Object
Instance Method Summary collapse
- #apply_strong_params(global_config: ActiveParams.global_config) ⇒ Object
- #write_strong_params(global_config: ActiveParams.global_config) ⇒ Object
Class Method Details
.combine_hashes(array_of_hashes) ⇒ Object
to obtain a hash of all possible keys
52 53 54 55 |
# File 'lib/active_params.rb', line 52 def self.combine_hashes(array_of_hashes) array_of_hashes.select {|v| v.kind_of?(Hash) }. inject({}) {|sum, hash| hash.inject(sum) {|sum,(k,v)| sum.merge(k => v) } } end |
.development_mode? ⇒ Boolean
36 37 38 |
# File 'lib/active_params.rb', line 36 def self.development_mode? defined?(Rails) && Rails.env.development? end |
.global_config ⇒ Object
44 45 46 47 48 49 |
# File 'lib/active_params.rb', line 44 def self.global_config @@global_config ||= (File.exists?(ActiveParams.path) ? JSON.parse(IO.read ActiveParams.path) : {}) ensure # undo cache in development mode @@global_config = nil if development_mode? end |
.included(base) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/active_params.rb', line 27 def self.included(base) if base.methods.include? :around_action base.class_eval do around_action :write_strong_params if ActiveParams.development_mode? around_action :apply_strong_params end end end |
.path ⇒ Object
40 41 42 |
# File 'lib/active_params.rb', line 40 def self.path ENV.fetch("ACTIVE_PARAMS_PATH", "config/active_params.json") end |
.strong_params_definition_for(params) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_params.rb', line 57 def self.strong_params_definition_for(params) if params.kind_of?(Array) combined_hash = combine_hashes(params) if combined_hash.empty? [] else strong_params_definition_for(combined_hash) end elsif params.respond_to?(:keys) # Hash, ActionController::Parameters values, arrays = [[], {}] params.each do |key, value| case value when Array arrays[key] = strong_params_definition_for(value) when Hash arrays[key] = strong_params_definition_for(combine_hashes(value.values)) else values.push(key) end end if arrays.empty? values else [*values.sort, arrays] end else params end end |
Instance Method Details
#apply_strong_params(global_config: ActiveParams.global_config) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_params.rb', line 16 def apply_strong_params(global_config: ActiveParams.global_config) # e.g. POST users#update current_config = global_config["#{request.method} #{controller_name}/#{action_name}"] ||= {} params.each do |k,v| if result = current_config[k] params[k] = params.require(k).permit(result) end end yield end |
#write_strong_params(global_config: ActiveParams.global_config) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/active_params.rb', line 4 def write_strong_params(global_config: ActiveParams.global_config) current_config = global_config["#{request.method} #{controller_name}/#{action_name}"] ||= {} params.each do |k,v| case result = ActiveParams.strong_params_definition_for(v) when Array, Hash current_config[k] = result end end open(ActiveParams.path, "wb") {|f| f.write(JSON.pretty_generate(global_config)) } yield end |