class Rarocrud::OverrideGenerator

Public Instance Methods

copy_initializer_file() click to toggle source
# File lib/generators/rarocrud/override/override_generator.rb, line 6
    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%>
  <div id="form">
    <%= render "/#{file_name.pluralize}/form" %>
  </div>
<% 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) %>
                <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-2">
                                <%= 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 %>
                        </div>
                </div>
        <% end %>
<% end %>
          RUBY
        end
        puts "Insira o form no arquivo app/views/#{file_name.pluralize}/_form.html.erb"
      end
      
      #Override SHOW
      if @action == "show"
          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
      end
      
      #Override INDEX
      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 %>
  <div id="form" >
    <%= render "/#{file_name.pluralize}/records" %>
  </div>
<% 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 %>
  <div>
    <%= render "/crud/links" %>
  </div>
  <div class="dataTables_wrapper form-inline">
    <%= paginate @records, target: "#form", theme: "templus" %>
  </div>
<% end %>

          RUBY
        end
        puts "Insira a index no arquivo app/views/#{file_name.pluralize}/_records.html.erb"
      end
    end