Sha256: 6f1134e7c46597eb70202898eaaed2bd713b8088629ed6f5a2373db22dccc42c

Contents?: true

Size: 1.38 KB

Versions: 20

Compression:

Stored size: 1.38 KB

Contents

module Footnotes
  module Notes
    class ControllerNote < AbstractNote
      def initialize(controller)
        @controller = controller
      end

      def row
        :edit
      end

      def link
        Footnotes::Filter.prefix(controller_filename, controller_line_number + 1, 3)
      end

      def valid?
        prefix? && controller_filename && File.exists?(controller_filename)
      end

      protected
        def controller_path
          @controller_path = @controller.class.name.underscore
        end

        def controller_filename
          @controller_filename ||= Gem.find_files(controller_path).first # tnx https://github.com/MasterLambaster
        end

        def controller_text
          @controller_text ||= IO.read(controller_filename)
        end

        def action_index
          (controller_text =~ /def\s+#{@controller.action_name}[\s\(]/)
        end

        def controller_line_number
          lines_from_index(controller_text, action_index) || 0
        end

        def lines_from_index(string, index)
          return nil if string.blank? || index.blank?

          lines = string.respond_to?(:to_a) ? string.to_a : string.lines.to_a
          running_length = 0
          lines.each_with_index do |line, i|
            running_length += line.length
            if running_length > index
              return i
            end
          end
        end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
rails-footnotes-5.0.0 lib/rails-footnotes/notes/controller_note.rb
rails6-footnotes-5.0.2 lib/rails-footnotes/notes/controller_note.rb
rails6-footnotes-5.0.1 lib/rails-footnotes/notes/controller_note.rb
rails6-footnotes-5.0.0 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.1.8 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.1.7 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.1.6 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.1.5 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.1.4 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.1.3 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.1.2 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.1.1 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.1.0 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.0.2 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.0.1 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-4.0.0 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-3.7.9 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-3.7.8 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-3.7.7 lib/rails-footnotes/notes/controller_note.rb
rails-footnotes-3.7.6 lib/rails-footnotes/notes/controller_note.rb