Module: Filewriter
- Defined in:
- lib/active_recorder/filewriter.rb
Overview
A Filereader module which create a controller and view for ActiveRecord tables
Class Method Summary collapse
-
.create_tables_dir(path) ⇒ Object
Creates a directory with the given path.
-
.pluralize(name) ⇒ Object
Appends ‘s’ to a records name.
-
.to_all(name) ⇒ Object
Appends ‘.all’ to a records name.
-
.to_instance_variable(name) ⇒ Object
Prepends ‘@’ and appends ‘s’ to a lower case variable name.
-
.write_controller(path, records) ⇒ Object
Creates controller in the directory given.
-
.write_routes(path) ⇒ Object
Writes the tables routes to the routes.rb file.
-
.write_view(path, records) ⇒ Object
Creates a tables view in the directory given.
Class Method Details
.create_tables_dir(path) ⇒ Object
Creates a directory with the given path
116 117 118 119 |
# 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) ⇒ Object
Appends ‘s’ to a records name
109 110 111 112 113 |
# 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) ⇒ Object
Appends ‘.all’ to a records name
104 105 106 |
# File 'lib/active_recorder/filewriter.rb', line 104 def self.to_all(name) "#{name}.all" end |
.to_instance_variable(name) ⇒ Object
Prepends ‘@’ and appends ‘s’ to a lower case variable name
97 98 99 100 101 |
# 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) ⇒ Object
Creates controller in the directory given
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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) ⇒ Object
Writes the tables routes to the routes.rb file
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# 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) ⇒ Object
Creates a tables view in the directory given
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 |