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.