module Rarocrud class OverrideGenerator < Rails::Generators::NamedBase source_root File.expand_path('../templates', __FILE__) argument :acao, type: :string, default: [], banner: "model action" def copy_initializer_file @model = Module.const_get(file_name.camelize) @action = acao destination = "app/controllers/#{file_name.pluralize}_controller.rb" if File.exist?(destination) puts "Exist #{destination}" else puts "Generating #{destination}" template 'controller.rb', destination, @model end if @action == "new" inject_into_file "config/routes.rb", before: "get '/crud/:model/new' => \"crud#new\"" do <<-RUBY get "/crud/#{file_name}/new" => "#{file_name.pluralize}#new" RUBY end inject_into_file "app/controllers/#{file_name.pluralize}_controller.rb", after: "ApplicationController" do <<-RUBY def new @model = Module.const_get("#{file_name}".camelize) @crud_helper = Module.const_get("#{file_name}_crud".camelize) if params[:render] == "modal" if @model.reflect_on_association(params[:attribute].to_s).present? @model = @model.reflect_on_association(params[:attribute].to_s).class_name.constantize else @model = params[:attribute].to_s.camelcase.constantize end end authorize! :new, @model if respond_to?(:current_usuario) @crud_helper = Module.const_get("#{@model}Crud".camelize) @record = @model.new end RUBY end template 'new.html.erb', "app/views/#{file_name.pluralize}/new.html.erb", file_name append_file "app/views/#{file_name.pluralize}/new.html.erb" do <<-RUBY <% content_for :corpo do %> <%unless params[:render] == 'modal'%> <%= render "/crud/links" %> <%end%>
<%= render "/#{file_name.pluralize}/form" %>
<% end %> <%= render "/crud/crud_template" %> RUBY end template 'partial_new.html.erb', "app/views/#{file_name.pluralize}/_new.html.erb", file_name append_file "app/views/#{file_name.pluralize}/_new.html.erb" do <<-RUBY <%= render "/#{file_name.pluralize}/form" %> RUBY end template 'form.html.erb', "app/views/#{file_name.pluralize}/_form.html.erb", file_name append_file "app/views/#{file_name.pluralize}/_form.html.erb" do <<-RUBY <%= render_crud do %> <%= simple_nested_form_for @record, remote: false, html: {class: "form-horizontal"}, url: "/crud/\#{@model.name.underscore}/\#{@record.new_record? ? 'create' : @record.id.to_s+'/create'}?page=\#{params[:page]}" do |f| %> <%= flash_messages_error(@record.errors) %>
<%= f.submit "Salvar", class: 'btn btn-primary' %> <%unless params[:render] == 'modal'%> <%= link_to "Voltar", "/crud/\#{@model.name.underscore}?page=\#{params[:page]}", class: 'btn btn-default', data: {push: 'partial', target: "#form"} %> <% end %>
<% end %> <% end %> RUBY end puts "Insira o form no arquivo app/views/#{file_name.pluralize}/_form.html.erb" end if @action == "index" inject_into_file "config/routes.rb", before: "get '/crud/:model' => \"crud#index\"" do <<-RUBY get "/crud/#{file_name}" => "#{file_name.pluralize}#index" RUBY end inject_into_file "app/controllers/#{file_name.pluralize}_controller.rb", after: "ApplicationController" do <<-RUBY def index @model = Module.const_get("#{file_name}".camelize) @crud_helper = Module.const_get("#{file_name}_crud".camelize) authorize! :read, @model if respond_to?(:current_usuario) if params[:scope].present? @q = @model.send(params[:scope]).search(params[:q]) else @q = @model.search(params[:q]) end if @q.sorts.empty? if "\#{@crud_helper.order_field}".include?("desc") or "\#{@crud_helper.order_field}".include?("asc") @q.sorts = "\#{@crud_helper.order_field}" else @q.sorts = "\#{@crud_helper.order_field} asc" end end if respond_to?(:current_usuario) @records = @q.result(distinct: true).accessible_by(current_ability, :read).page(params[:page]).per(@crud_helper.per_page) else @records = @q.result(distinct: true).page(params[:page]).per(@crud_helper.per_page) end @titulo = @model.name.pluralize render partial: 'records' if request.respond_to?(:wiselinks_partial?) && request.wiselinks_partial? end RUBY end template 'index.html.erb', "app/views/#{file_name.pluralize}/index.html.erb", file_name append_file "app/views/#{file_name.pluralize}/index.html.erb" do <<-RUBY <% content_for :corpo do %>
<%= render "/#{file_name.pluralize}/records" %>
<% end %> <%= render "/crud/crud_template" %> RUBY end template 'records.html.erb', "app/views/#{file_name.pluralize}/_records.html.erb", file_name append_file "app/views/#{file_name.pluralize}/_records.html.erb" do <<-RUBY <%= render_crud do %>
<%= render "/crud/links" %>
<%= paginate @records, target: "#form", theme: "templus" %>
<% end %> RUBY end puts "Insira a index no arquivo app/views/#{file_name.pluralize}/_records.html.erb" end end end end