module Filewriter
A Filereader
module which create a controller and view for ActiveRecord tables
Public Class Methods
create_tables_dir(path)
click to toggle source
Creates a directory with the given path
# File lib/active_recorder/filewriter.rb, line 116 def self.create_tables_dir(path) dir = File.join(path, 'app/views/tables') Dir.mkdir dir end
pluralize(name)
click to toggle source
Appends ‘s’ to a records name
# File lib/active_recorder/filewriter.rb, line 109 def self.pluralize(name) pluralized = "#{name}s" pluralized = "#{name}es" if name.end_with?('s') pluralized end
to_all(name)
click to toggle source
Appends ‘.all’ to a records name
# File lib/active_recorder/filewriter.rb, line 104 def self.to_all(name) "#{name}.all" end
to_instance_variable(name)
click to toggle source
Prepends ‘@’ and appends ‘s’ to a lower case variable name
# File lib/active_recorder/filewriter.rb, line 97 def self.to_instance_variable(name) instance_name = "@#{name.downcase}s" instance_name = "@#{name.downcase}es" if name.end_with?('s') instance_name end
write_controller(path, records)
click to toggle source
Creates controller in the directory given
# File lib/active_recorder/filewriter.rb, line 5 def self.write_controller(path, records) # Get the absolute path to write the controller dir = File.join(path, 'app/controllers/tables_controller.rb') open(dir, 'w') do |f| # Prints class declaration f.puts 'class TablesController < ApplicationController' # Prints comment f.puts ' # GET /tables' # Prints route function f.puts ' def index' # Prints out entities instance variable and function call records.each do |record| f.puts " #{Filewriter.to_instance_variable(record.name)} = #{Filewriter.to_all(record.name)}" end f.puts ' end' f.puts 'end' end end
write_routes(path)
click to toggle source
Writes the tables routes to the routes.rb file
# File lib/active_recorder/filewriter.rb, line 74 def self.write_routes(path) # Get input and output directory of files input = "#{File.join(path, 'config')}/routes.rb" output = "#{File.join(path, 'config')}/tmp.rb" # Open the file to read from open(input, 'r') do |input_file| open(output, 'w') do |output_file| # Read each line of input input_file.each_line do |line| if line.start_with? 'end' output_file.puts(" get 'tables' => 'tables#index'") output_file.puts('end') else output_file.write(line) end end end end # Overwrite input with output FileUtils.mv(output, input) end
write_view(path, records)
click to toggle source
Creates a tables view in the directory given
# File lib/active_recorder/filewriter.rb, line 25 def self.write_view(path, records) # Get the absolute path to write the view dir = File.join(path, 'app/views/tables/index.html.erb') open(dir, 'w') do |f| # Print headers f.puts '<!DOCTYPE HTML>' f.puts '<html lang="en">' f.puts ' <head>' f.puts ' <title>ActiveRecord Tables</title>' f.puts ' <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">' f.puts ' </head>' f.puts ' <body>' f.puts ' <h1 align="center">ActiveRecord Tables</h1>' # Print record tables records.each do |record| f.puts ' <div class="container col-md-offset-2 col-md-8">' f.puts " <h3 align='center' style='margin-top:30px'>#{Filewriter.pluralize(record.name)} Table</h3>" f.puts ' <table class="table table-bordered">' f.puts ' <thead>' f.puts ' <tr class="info">' f.puts ' <th>ID</th>' record.columns.each do |col, _val| f.puts " <th>#{col.capitalize}</th>" end f.puts ' </tr>' f.puts ' </thead>' f.puts ' <tbody>' f.puts " <% #{Filewriter.to_instance_variable(record.name)}.each do |#{record.name.downcase}| %>" f.puts ' <tr>' f.puts " <td><%= #{record.name.downcase}.id %></td>" record.columns.each do |col, val| if val == 'references' f.puts " <td><%= #{record.name.downcase}.#{col}.id %></td>" else f.puts " <td><%= #{record.name.downcase}.#{col} %></td>" end end f.puts ' </tr>' f.puts ' <% end %>' f.puts ' </tbody>' f.puts ' </table>' f.puts ' </div>' end f.puts ' </body>' f.puts '</html>' end end