How to generate documentation with Yard for Rails views? - ruby-on-rails

I have just started using Yard to generate my docs and whilst it works great for my models and controllers, I can't seem to get it to generate anything for the views
I've tried a variety of different ways to get it to work. Currently I've set up a rake task to generate the docs that looks something like this
YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb', 'app/**/*.rb', 'app/views/**/*.erb']
t.options = []
end
The last path there, I believe, should get it to include my views but when I look at the docs, there's no mention of any of these files at all. Any ideas why?
Cheers

Template files are not expected to contain Ruby methods or business logic. What kind of documentation would you like to generate?

Related

Rails: find routes/methods which no longer match any controller

I have a massive monolithic Rails repo with a couple thousand routes.
Over time, developers have either deleted a route which maps to an existing controller or have deleted a controller which still has a route mapped to it
Is there a smart way for me to systematically find these anomalies within my rails app to clean it up?
NOTE: Please assume that these mismatches do not present a user-facing issue but is merely for maintenance purposes to trim the number of bad routes
The traceroute gem looks very nice, but just in case it doesn't work as you expect, you could do this in rails console.
Get an array of all your controllers with their actions:
ApplicationController.descendants.map {|c| [c, c.action_methods]}
Get an array of all routes:
Rails.application.routes.routes.map &:defaults
Then you could iterate over the two arrays, selecting actions that appear in one array but not in the other one.
Before starting the console set config.eager_load = true in config/environments.development.rb. Without this you won't see all of your controllers.
There is no such command that will find you:
But here is a thing you can do:
Run command:
rails routes > routes.rb
It will write all your route in an text file and they you can match the manually if you have the time.
But there is no harm if you have extra routes present in Rails application its common in most of the Rails applications.
I found this gem which aims to solve my question somewhat
https://github.com/amatsuda/traceroute

Query which controller will be used for a URL

Question
I'm looking for something in the rails console or similar where I can call get_controller_for_path('/some/path/') and it will return the corresponding controller.
Background
I have a rails project with a lot of routes. I'm investigating a routing problem and want to confirm that a given URL will match a specific route.
I can use bundle exec rake routes to view the list of routes, but that still requires my human eyes to parse the hundreds (thousands?) of routes and figure out what's going on.
Here you go: Rails parse url to hash(Routes)
That says it's Rails.application.routes.recognize_path "/accounts/1"
You can also use assert_routing in your tests. If you don't have tests, now is a very good time to add some. And you can read the source of assert_routing, and assert_recognizes, to see what they do.

Dynamic Sitemap Errors

I'm trying to get dynamic_sitemaps gem to work with my site, but the readme is very technical and a bit over my head at the moment.
I'm running into errors when trying to generate the sitemap for this bit of code.
# You can have multiple sitemaps like the above – just make sure their names are different.
# Automatically link to all pages using the routes specified
# using "resources :pages" in config/routes.rb. This will also
# automatically set <lastmod> to the date and time in page.updated_at:
#
sitemap_for :offers
It's returning the below error
ArgumentError: The collection given to sitemap_for must respond to
find_each. This is for performance. Use Model.scoped to get an ActiveRecord relation that responds to find_each.
I'm looking to have the sitemap contain all my offer posts etc.
Any help is greatly appreciated!
If your model's name is Offer, try
sitemap_for Offer.all
(note: #scoped is deprecated, so #all seems to be the better option going forward)

How to translate a route scope in Rails?

I'm trying to translate the routes in a Rails application. I tried with i18n_routes, but I had some problems with translating actions that didn't depend on a resource. In the end, I solved it without using i18n_routes, as explained here.
The issue now is that I don't find the way to translate scopes. This is my routes.rb:
scope "sport" do
translated_named_route 'it_is_healthy', 'sport#it_is_healthy'
translated_named_route 'it_is_social', 'sport#it_is_social'
end
How to translate sport? I tried with i18n_routes again, but I don't see how to translate the scope. Thanks!
If you are open to using a gem for this, I would recommend using
https://github.com/francesc/rails-translate-routes
The readme is fairly comprehensive and it definitely covers namespaces and scopes in the routes as well.

Ruby on Rails model inside namespace can't be found in controller

I'm new to rails and can't figure out this issue...
I have a controller
Admin::Blog::EntriesController
defined in app/controllers/admin/blog/entries_controller.rb
And I have a model called
Blog::Entry
defined in app/model/blog/entry.rb
When I try to access my model from the controller, I get a "uninitialized constant Admin::Blog::EntriesController::Blog" from this line:
#blog_entries = Blog::Entry.find(:all)
Clearly it is not finding the namespace correctly which is odd because according to what I have read, I have placed my model in the correct folder with the correct syntax.
Any ideas on how I can fix this?
Thanks
Try:
#blog_entries = ::Blog::Entry.find(:all)
It's currently looking for the wrong class. Using :: before Blog will force it to look from the top level.
It is now 2011 and we are in Rails 3.1 territory, but this issue still arises. I just ran into it with a namespaced controller referencing a non-namespaced model, but only when there were no rows for that model in the database!
Prefixing the model name with :: fixes the problem.
You can achieve a custom table name by using
set_table_name('foo')
at the top of your model.
As for multiple namespaces, you might be able to get away with using
polymorphic_path(#the_object)
to generate your urls as it does more basic inference (in my experience at least, maybe form_for uses it under the hood).
Yeah, from looking at the code form_for uses polymorphic_path under the hood.

Resources