Module: PartialFinder::Formatter
- Defined in:
- lib/partial_finder/formatter.rb
Class Method Summary collapse
-
.controller_signature(path, method) ⇒ Object
Returns a controller signature constructed from a controller’s view path and a manually specified method name.
-
.controller_signature_from_view(path) ⇒ Object
Given a view path, the controller name and method that implicitly renders it are assumed by convention.
-
.fix_path(incomplete_path) ⇒ Object
Takes a view path that can be complete or have missing/extra parts and returns it in the format app/views/any_subfolders/view_file.html.erb or app/controllers/any_subfolders/controller_file.html.erb.
- .is_controller?(path) ⇒ Boolean
- .is_partial?(path) ⇒ Boolean
- .is_view?(path) ⇒ Boolean
-
.methods_that_render(partial_path, controller_path, rails_root = PartialFinder.default_root) ⇒ Object
Searches through a controller’s definition to find which method is rendering a given partial.
-
.path_to_ref(path) ⇒ Object
Takes a file path and turns it into a partial reference.
-
.type_of(path) ⇒ Object
Determines if the given path is a partial, a view, a controller or none of the above.
Class Method Details
.controller_signature(path, method) ⇒ Object
Returns a controller signature constructed from a controller’s view path and a manually specified method name. If the path is not a controller, an empty string is returned.
90 91 92 93 94 95 96 97 98 |
# File 'lib/partial_finder/formatter.rb', line 90 def self.controller_signature(path, method) if is_controller?(path) cname = path.deep_dup cname.remove!('app/').remove!('controllers/').remove!('_controller.rb') "#{cname}##{method}" else "" end end |
.controller_signature_from_view(path) ⇒ Object
Given a view path, the controller name and method that implicitly renders it are assumed by convention. A controller signature is returned. If the path is not a view, an empty string is returned.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/partial_finder/formatter.rb', line 75 def self.controller_signature_from_view(path) if is_view?(path) cname = path.deep_dup cname.remove!('app/').remove!('views/').remove!('.html.erb') cname = cname.split('/') method = cname.pop "#{cname.join('/')}##{method}" else "" end end |
.fix_path(incomplete_path) ⇒ Object
Takes a view path that can be complete or have missing/extra parts and returns it in the format app/views/any_subfolders/view_file.html.erb or app/controllers/any_subfolders/controller_file.html.erb
This is needed mainly when the search directly for grep is altered and parts of the path need to be restored and scrubbed of the leading ./ characters.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/partial_finder/formatter.rb', line 40 def self.fix_path(incomplete_path) path = incomplete_path.remove(/\A\.\//) if path.match(/\Aviews/) || path.match(/\Acontrollers/) "app/#{path}" elsif !path.match /\Aapp/ if is_view?(path) "app/views/#{path}" elsif is_controller?(path) "app/controllers/#{path}" else path end else path end end |
.is_controller?(path) ⇒ Boolean
26 27 28 29 |
# File 'lib/partial_finder/formatter.rb', line 26 def self.is_controller?(path) return false unless path.present? !!path.split('/').last.match(/.+_controller\.rb/) end |
.is_partial?(path) ⇒ Boolean
16 17 18 19 |
# File 'lib/partial_finder/formatter.rb', line 16 def self.is_partial?(path) return false unless path.present? !!path.split('/').last.match(/\A_.+\.erb/) end |
.is_view?(path) ⇒ Boolean
21 22 23 24 |
# File 'lib/partial_finder/formatter.rb', line 21 def self.is_view?(path) return false unless path.present? !!path.split('/').last.match(/\A[^_].+\.erb/) end |
.methods_that_render(partial_path, controller_path, rails_root = PartialFinder.default_root) ⇒ Object
Searches through a controller’s definition to find which method is rendering a given partial. Multiple method names may be returned.
A root is needed to provide flexibility so the controller file can be opened regardless of the current working directory. Normally paths are just used for their conventions, but in the controller’s case here, the path needs to actually point to a file relative to the current working directory.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/partial_finder/formatter.rb', line 107 def self.methods_that_render(partial_path, controller_path, rails_root = PartialFinder.default_root) if is_partial?(partial_path) && is_controller?(controller_path) full_c_path = "#{rails_root}/#{controller_path}" fragments = File.read(full_c_path).split /def (.+?)$/ ref = path_to_ref(partial_path) matches = [] fragments.each.with_index do |fr,i| matches << fragments[i-1] if fr.match ref end matches else "" end end |
.path_to_ref(path) ⇒ Object
Takes a file path and turns it into a partial reference. Example: app/views/orders/_order_sidepanel.html.erb becomes orders/order_sidepanel which is the format used when rendering a partial in a controller or view.
8 9 10 11 12 13 14 |
# File 'lib/partial_finder/formatter.rb', line 8 def self.path_to_ref(path) ref = path.deep_dup ref.remove!('app/').remove!('views/').remove!(/\.html\.erb\z/) ref = ref.split('/') ref[-1] = ref.last.remove(/\A_/) ref.join('/') end |
.type_of(path) ⇒ Object
Determines if the given path is a partial, a view, a controller or none of the above
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/partial_finder/formatter.rb', line 60 def self.type_of(path) if is_partial?(path) :partial elsif is_view?(path) :view elsif is_controller?(path) :controller else :unknown end end |