I am trying to edit rails scaffold generators where I want to add custom action to my controllers and add them into routes. I can edit model generators by adding this code in my lib directory:
#/lib/template/active_record/model.rb
<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>
def self.import1(file)
spreadsheet = Roo::Spreadsheet.open(file.path)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
puts row.to_hash
product = find_by(id: row["id"]) || new
product.attributes = row.to_hash
product.save!
end
end
<% attributes.select(&:reference?).each do |attribute| -%>
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %>
<% end -%>
<% attributes.select(&:token?).each do |attribute| -%>
has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
<% end -%>
<% if attributes.any?(&:password_digest?) -%>
has_secure_password
<% end -%>
end
<% end -%>
When I am generating rails g scaffold my_controller name:string .. I can see it is invoking resource_route. And it adds a line to my routes.
I want a solution where I can add two actions like test and import in my controller whenever I generate scaffold and add them into routes. I can edit my templates by adding this:
#/lib/templates/scaffold/index.html.erb
What do I need to write or copy in my lib folder which can generate a controller with two actions like:
def import
# Module1.import(params[:file])
ProductionProductivity7.import1(params[:file])
redirect_to tests_path, notice: "Products imported."
end
And in my routes I want this to be added like:
resources :production_productivity9s do
collection { post :import }
collection { get :dropdown }
collection { get :test }
end
Instead of resources :production_productivity9s. I want a solution to edit controller and routes. I am searching on net and I don't see any solution.
I was trying to edit the resource route and add some action to my controller I added this code for editing my routes:
#rails/generators/rails/resource_route
module Rails
module Generators
class ResourceRouteGenerator < NamedBase # :nodoc:
# Properly nests namespaces passed into a generator
#
# $ rails generate resource admin/users/products
#
# should give you
#
# namespace :admin do
# namespace :users do
# resources :products
# end
# end
def add_resource_route
return if options[:actions].present?
depth = 0
lines = []
# Create 'namespace' ladder
# namespace :foo do
# namespace :bar do
regular_class_path.each do |ns|
lines << indent("namespace :#{ns} do\n", depth * 2)
depth += 1
end
# inserts the primary resource
# Create route
# resources 'products'
# lines << indent(%{resources :#{file_name.pluralize}\n }, depth * 2)
lines << indent(%{resources :#{file_name.pluralize} do
collection { post :import }
collection { get :dropdown }
collection { get :test }
end \n}, depth * 2)
# Create `end` ladder
# end
# end
until depth.zero?
depth -= 1
lines << indent("end\n", depth * 2)
end
route lines.join
end
end
end
end
source code for resource route : Resource Route
I was able to edit my controller I used this code to edit:
#/lib/templates/rails/scaffold_controller/controller.rb
def import
<%= class_name %>.import1(params[:file])
redirect_to tests_path, notice: "Products imported."
end
Source code for scaffold controller edit is: Scaffold_Controller