Sha256: fa183a870b78c8245070cccb99969ecd923efbfb24517ccd98a61bb6518b25a1

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module JetsUpgrade::Rewrite
  class Controller < Base
    def initialize(path)
      @path = path
    end

    def filename
      @path # /full/path/to/app/controllers/posts_controller.rb
    end

    def marker
      "def destroy"
    end

    def content
      within_destroy = false
      modified_lines = lines.map do |line|
        if line =~ /\s{2,}def delete$/
          line = line.sub('delete', 'destroy')
          within_destroy = true
        end
        if within_destroy
          if line =~ /\s{2,}redirect_to (\w+)$/
            location_method = $1 # IE: posts_path
            line = respond_to_code(location_method)
          end
        end
        if within_destroy && line =~ /\s{2,}end$/
          within_destroy = false
        end

        if line =~ /before_action :/ && line =~ /delete/
          line = line.sub('delete', 'destroy')
        end
        line
      end
      modified_lines.join('')
    end

    def respond_to_code(location_method)
      code = <<~EOL
        respond_to do |format|
          format.html { redirect_to #{location_method} }
          format.js   { render json: { location: #{location_method} } }
        end
      EOL
      lines = code.split("\n")
      # add 4 spaces to each line
      lines.map! { |line| "    #{line}" }.join("\n") + "\n"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jets-upgrade-0.1.1 lib/jets_upgrade/rewrite/controller.rb