Module: ActiveParams::Controller
- Defined in:
- lib/active_params/controller.rb
Class Method Summary collapse
Instance Method Summary collapse
- #active_params_apply(global_json: active_params_json) ⇒ Object
- #active_params_json ⇒ Object
- #active_params_path ⇒ Object
- #active_params_scope ⇒ Object
- #active_params_write(global_json: active_params_json) ⇒ Object
- #active_params_writing? ⇒ Boolean
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_params/controller.rb', line 5 def self.included(base) add_before_method = (base.methods.include?(:before_action) && :before_action) || (base.methods.include?(:before_filter) && :before_filter) if add_before_method base.class_eval do send add_before_method, :active_params_write, if: :active_params_writing? send add_before_method, :active_params_apply end end end |
Instance Method Details
#active_params_apply(global_json: active_params_json) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/active_params/controller.rb', line 51 def active_params_apply(global_json: active_params_json) # e.g. POST users#update scoped_json = global_json[active_params_scope] ||= {} params.each do |k,v| if result = scoped_json[k] params[k] = params.require(k).permit(result) end end end |
#active_params_json ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/active_params/controller.rb', line 26 def active_params_json @@active_params_json ||= (File.exists?(active_params_path) ? JSON.parse(IO.read active_params_path) : {}) rescue JSON::ParserError return {} ensure # undo cache in development mode @@active_params_json = nil if active_params_writing? end |
#active_params_path ⇒ Object
22 23 24 |
# File 'lib/active_params/controller.rb', line 22 def active_params_path [:path] || ActiveParams.path end |
#active_params_scope ⇒ Object
35 36 37 38 |
# File 'lib/active_params/controller.rb', line 35 def active_params_scope scope = [:scope] || ActiveParams.scope scope.respond_to?(:call) ? scope.(self) : scope end |
#active_params_write(global_json: active_params_json) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/active_params/controller.rb', line 40 def active_params_write(global_json: active_params_json) scoped_json = global_json[active_params_scope] ||= {} params.each do |k,v| case result = ActiveParams::Parser.strong_params_definition_for(v) when Array, Hash scoped_json[k] = result end end open(active_params_path, "wb") {|f| f.write(JSON.pretty_generate(global_json)) } end |
#active_params_writing? ⇒ Boolean
18 19 20 |
# File 'lib/active_params/controller.rb', line 18 def active_params_writing? [:writing] || ActiveParams.writing end |