Skip to content

Commit

Permalink
Make the remove_attr_accessible_from_model as single public method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Guo committed Oct 3, 2014
1 parent 27bf79d commit 0bf4f77
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ tmp
.ruby-version

*.sublime-workspace

*.rewritten
23 changes: 13 additions & 10 deletions lib/attr_accessible2strong_params/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ def read_attr_accessible(filename)
@model_class_name = m.parent.parent.children[0].children[1]
aa_fields <<= m.each_node(:sym).collect {|n| n.children[0]}
end
write_file_with_comments(filename, remove_attr_accessible_from_model(buffer, root_node), comments)
@model_fields = aa_fields.flatten
end

def write_controller_with_strong_params(filename)
def remove_attr_accessible_from_model(filename, no_rename = false)
root_node, comments, buffer = parse_file_with_comments(filename)
write_file_with_comments(filename, root_node, comments)
no_aa_src_buffer = Parser::Source::Buffer.new('(string)')
no_aa_src_buffer.source = RemoveAttrAccessibleRewriter.new.rewrite(buffer,root_node)
no_aa_node = Parser::CurrentRuby.new(Astrolabe::Builder.new).parse(no_aa_src_buffer)
write_file_with_comments(filename, no_aa_node, comments, no_rename)
end

def write_controller_with_strong_params(filename, no_rename = false)
root_node, comments, buffer = parse_file_with_comments(filename)
write_file_with_comments(filename, root_node, comments, no_rename)
end

private
Expand All @@ -29,19 +36,15 @@ def parse_file_with_comments(filename)
return root_node, comments, buffer
end

def write_file_with_comments(filename, root_node, comments)
def write_file_with_comments(filename, root_node, comments, no_rename)
rewritten = Unparser.unparse(root_node, comments)
temp_path = "#{filename}.rewritten"
File.open(temp_path, 'w') do |temp_file|
temp_file.write(rewritten)
temp_file.puts unless rewritten.end_with?(?\n)
end
File.rename(temp_path, filename)
end

def remove_attr_accessible_from_model(buffer, root_node)
no_aa_src_buffer = Parser::Source::Buffer.new('(string)')
no_aa_src_buffer.source = RemoveAttrAccessibleRewriter.new.rewrite(buffer,root_node)
Parser::CurrentRuby.new(Astrolabe::Builder.new).parse(no_aa_src_buffer)
return 0 if no_rename
File.rename(temp_path, filename)
end
end
7 changes: 6 additions & 1 deletion test/test_attr_accessible2strong_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ def test_read_model_attr_accessible_sym_list
, c.read_attr_accessible('test/example_model.rb')
end

def test_remove_attr_accessible_from_model
c = AttrAccessible2StrongParams::Converter.new
assert_equal 0, c.remove_attr_accessible_from_model('test/example_model.rb', true)
end

def test_write_controller_src
c = AttrAccessible2StrongParams::Converter.new
c.read_attr_accessible('test/example_model.rb')
assert_equal 0, c.write_controller_with_strong_params('test/example_controller.rb')
assert_equal 0, c.write_controller_with_strong_params('test/example_controller.rb', true)
end
end

0 comments on commit 0bf4f77

Please sign in to comment.