Class: AttrAccessible2StrongParams::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/attr_accessible2strong_params/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#model_fieldsObject (readonly)

Returns the value of attribute model_fields.



4
5
6
# File 'lib/attr_accessible2strong_params/converter.rb', line 4

def model_fields
  @model_fields
end

Instance Method Details

#read_attr_accessible(filename) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/attr_accessible2strong_params/converter.rb', line 6

def read_attr_accessible(filename)
  @model_file_name = filename
  root_node, comments, buffer = parse_file_with_comments(filename)
  aa_nodes = root_node.each_node(:send).select {|n| n.children[1] == :attr_accessible}
  aa_fields = []
  aa_nodes.each do |m|
    @model_class_name = m.parent.parent.children[0].children[1].to_s
    aa_fields <<= m.each_node(:sym).collect {|n| n.children[0]}
  end
  @model_fields = aa_fields.flatten
end

#remove_attr_accessible_from_model(filename, no_rename = false) ⇒ Object



18
19
20
21
22
23
# File 'lib/attr_accessible2strong_params/converter.rb', line 18

def remove_attr_accessible_from_model(filename, no_rename = false)
  root_node, comments, buffer = parse_file_with_comments(filename)
  no_aa_src_buffer = Parser::Source::Buffer.new('(string)')
  rewritten_src = RemoveAttrAccessibleRewriter.new.rewrite(buffer,root_node)
  write_file_with_comments(filename, rewritten_src, no_rename)
end

#write_controller_with_strong_params(filename = nil, no_rename = false) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/attr_accessible2strong_params/converter.rb', line 25

def write_controller_with_strong_params(filename = nil, no_rename = false)
  filename = "#{File.dirname @model_file_name}/../controllers/#{@model_class_name.pluralize.underscore}_controller.rb" if filename.nil?
  return unless File.exist? filename
  root_node, comments, buffer = parse_file_with_comments(filename)
  source = ModifyControllerRewriter.new(@model_class_name, @model_fields).rewrite(buffer,root_node)
  write_file_with_comments(filename, source, no_rename)
end