Class: ModifyControllerRewriter

Inherits:
Parser::Rewriter
  • Object
show all
Defined in:
lib/attr_accessible2strong_params/modify_controller_rewriter.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_class_name, model_fields) ⇒ ModifyControllerRewriter

Returns a new instance of ModifyControllerRewriter.



2
3
4
5
6
# File 'lib/attr_accessible2strong_params/modify_controller_rewriter.rb', line 2

def initialize(model_class_name, model_fields)
  @model_class_name = model_class_name
  @model_fields = model_fields
  super()
end

Instance Method Details

#on_class(node) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/attr_accessible2strong_params/modify_controller_rewriter.rb', line 8

def on_class(node)
  if node.children.count == 3 and node.children[1].children[1] == :ApplicationController \
    and node.children[2].begin_type? == true then
    insert_after node.children[2].loc.expression.end, <<-END

private
  # Only allow a trusted parameter "white list" through.
  def #{@model_class_name.underscore}_params
    params.require(:#{@model_class_name.underscore}).permit(#{@model_fields.collect {|s| ":#{s}"}.join ", "})
  end
END
  end
  super
end

#on_send(node) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/attr_accessible2strong_params/modify_controller_rewriter.rb', line 23

def on_send(node)
  if node.children.count == 3 and node.children[1] == :[] \
    and node.children[0].children[1] == :params and node.children[2].sym_type? \
    and node.children[2].children[0].to_s == @model_class_name.underscore then
    replace node.loc.expression, "#{@model_class_name.underscore}_params"
  end
  super
end