Module: PeterConsuegraRecipes
- Defined in:
- lib/peterconsuegra_recipes.rb
Class Method Summary collapse
- .add_concern_to_controller(code, controller_file, controller_class) ⇒ Object
- .add_route(base_route, action_name, method) ⇒ Object
- .append_before_last_appearance_of(string, code, file) ⇒ Object
- .get_scaffold_vars(model) ⇒ Object
- .move_template(src_folder, dest_folder, file) ⇒ Object
- .move_templates(src_folder, dest_folder, files) ⇒ Object
Class Method Details
.add_concern_to_controller(code, controller_file, controller_class) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/peterconsuegra_recipes.rb', line 29 def self.add_concern_to_controller(code,controller_file,controller_class) rails_app_folder = Dir.pwd file="#{rails_app_folder}/app/controllers/#{controller_file}" #puts "class #{controller_class} < ApplicationController" #code="include FilesByPete\n" TTY::File.inject_into_file file, code, after: "class #{controller_class} < ApplicationController\n" puts "Adding concern to controller #{file}: #{code}".blue end |
.add_route(base_route, action_name, method) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/peterconsuegra_recipes.rb', line 21 def self.add_route(base_route,action_name,method) rails_app_folder = Dir.pwd route_file="#{rails_app_folder}/config/routes.rb" route_code = "#{method} '/#{base_route}_#{action_name}', to: '#{base_route}##{action_name}'" PeterConsuegraRecipes::append_before_last_appearance_of("end",route_code,route_file) puts "Adding route: #{route_code}".blue end |
.append_before_last_appearance_of(string, code, file) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/peterconsuegra_recipes.rb', line 38 def self.append_before_last_appearance_of(string,code,file) File.open(file, 'r+') do |file| lines = file.each_line.to_a length = lines.length() lines.reverse.each_with_index do |val, index| if val.include?(string) aux = lines[length-(index+1)] lines[length-(index+1)] = "#{code} \n" lines.append(aux) file.rewind file.write(lines.join) break end end end end |
.get_scaffold_vars(model) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/peterconsuegra_recipes.rb', line 56 def self.get_scaffold_vars(model) hash = Hash.new hash["model"] = model hash["model_class"] = model.titleize.gsub(' ','') hash["table_name"] = eval(hash["model_class"]).table_name hash["controller_file"] = hash["table_name"]+"_controller.rb" hash["controller_class"] = hash["table_name"].titleize.gsub(' ','')+"Controller" hash["base_route"] = hash["table_name"] #puts hash return hash end |
.move_template(src_folder, dest_folder, file) ⇒ Object
15 16 17 18 19 |
# File 'lib/peterconsuegra_recipes.rb', line 15 def self.move_template(src_folder,dest_folder,file) FileUtils.mkdir_p dest_folder FileUtils.cp src_folder+file,dest_folder+file puts "File copied to: #{dest_folder+file}".green end |
.move_templates(src_folder, dest_folder, files) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/peterconsuegra_recipes.rb', line 7 def self.move_templates(src_folder,dest_folder,files) FileUtils.mkdir_p dest_folder files.each do |file_name| FileUtils.cp(src_folder+file_name,dest_folder+file_name) puts "file copied to: #{dest_folder+file_name}".green end end |