updating this static page controller code to a newer rails version - ruby-on-rails

I've been trying to use the code here:
http://snafu.diarrhea.ch/blog/article/4-serving-static-content-with-rails
but I'm getting errors like:
undefined method `template_exists?' for #<StaticController:0xb74cbe4c>
How can I update this method to Rails 2.5? Probably there are other deprecated things too.

The method call template_exists? is deprecated as of Rails 2.2.1 (see: Rails APIdock)
A little bit of digging revealed the solution, by the original author, in the following article article. Basically requires adding the method into the StaticController, derived from ApplicationController and thus gives the method.

Related

Rails method global params kind_in error when upgraded

I've recently done an rails and ruby upgrade, we don't have strong params in the app (I know it's legacy).
So the way it's done in the app we have the following
def all_params_permitted(this_params = nil)
this_params = params if this_params == nil
this_params.permit!
this_params.each do |i, v|
if v.kind_in?([Hash, ActionController::Parameters])
all_params_permitted(v)
end
end
end
Which loops through all params and just accepts everything, all_params_permitted is called throughout the app I would love to add strong params but that's a no-go for now.
The issue in the above method is kind_in? the upgrade I did for this app was rails 5.0.3 to rails 6.1+ and went from ruby 2.2.6 to ruby 3.0.1 so I'm not sure why kind_in? has stopped working. This is an old app (built-in rails 2) so not sure if this has been deprecated.
Any help here would be great.
Edit
I have tried kind_of? but no dice.
the upgrade I did for this app was rails 5.0.3 to rails 6.1+ and went from ruby 2.2.6 to ruby 3.0.1
This is asking for trouble. It is strongly advised to try upgrading one minor version at a time (e.g. rails 5.0 --> 5.1 --> 5.2 --> 6.0 --> 6.1), otherwise you're very likely to break things with little information on why it's stopped working/how to fix it.
Likewise for ruby versions... At an absolute minimum I'd postpone the final upgrade to ruby v3 until your application works fine under ruby 2.7.
I'm not sure why kind_in? has stopped working
Nor am I, because that's a custom method. You haven't show us how it's defined, and nor have you shown us the error message, so it's impossible for me to say with confidence what's gone wrong.
My guess is that it's implemented something like this:
class Object
def kind_in?(classes)
classes.any? { |c| self.kind_of?(c) }
end
end
i.e. it's a little wrapper around the built-in kind_of? method.
And with that said, I still have no idea why this would have "stopped working" due to a ruby and/or rails upgrade.
Not sure about kind_in?, also didn't find any reference to that method, also as you have not posted the error so not sure about your issue. is_a?, kind_of?, instance_of? are few methods that check the object class but they check only a single class. Looking at your code one option for your condition could be:
if [Hash, ActionController::Parameters].include?(v.class)
which will check if it belongs to one of these classes.

undefined method `find_all_by_X'

Currently maintaining some old Ruby server and got the following error in Log:
NoMethodError (undefined method `find_all_by_X_ID' for #<Class:0x00000005555555>):
app/controllers/some_controller.rb:10:in `buggy_function'
When viewed the faulty line in code of the buggy function is looks like this:
Hash[S.find_all_by_X_ID(TaskRun.select(:x_id).uniq.where(y_id: #y.Y_ID).map(&:x_id)).map { |s| [s.S_IDENTIFIER, s.X_ID] }]
To be frank, I'm new to Ruby, and wondering how implementing this find_all_by_X query would be the best, and why it appears as it should be automatic (as it has to do with the model component).
We're working on Ruby version 2.
It seems that find_all_by was deprecated in Rails 4 ...
Internally Rails implemented methods like find_all_by_x_id using method_missing (the method is actually defined dynamically through metaprogramming) ... but you don't have to worry about that for your use-case.
In terms of your code, if we extract the x_ids list into a variable:
x_ids = TaskRun.select(:x_id).uniq.where(y_id: #y.Y_ID).map(&:x_id)
Then you have this line that you need to rewrite:
S.find_all_by_X_ID(x_ids)
You can rewrite this as:
S.where(x_id: x_ids)
See https://stackoverflow.com/a/23921890/2981429

Is there an updated version of 'where_clauses' method for Arel in Rails?

So I updated Rails from 4.0.1 to 4.1.16 and I'm getting errors along the lines of:
Failure/Error: self.published.arel.where_clauses.join(' AND ')
NoMethodError:
undefined method `where_clauses' for #<Arel::SelectManager:0x007fd773b1dd50>
I looked through the documentation for Arel (using version 5.0.1.20140414130214) and I can't find the same method. Has it been replaced and if so, with what?
Has it been replaced and if so, with what?
It was deprecated and then removed with no replacement. Seems that you need to review the logic of the places, which used where_clauses and reimplement those parts of the application using the current existing methods of arel

Install generator won't see a method I defined

I'm trying to use my Engine in another app to test the install generator and it seems to be failing. I haven't used my install generator for a long time, so I'm not sure when it broke (or if it ever truly smoothly worked). My project is based off radar/forem, so I tried to borrow a lot of their code (including the generator).
Edit: My installer works for the engines test/dummy but not in other apps. Why?
GH issue: https://github.com/NJayDevelopment/mongoid_forums/issues/16
Here is the log:
$ rails g mongoid_forums:install
What is your user class called? [User]
What is the current_user helper called in your app? [current_user]
Defining mongoid_forums_user method inside ApplicationController...
insert app/controllers/application_controller.rb
Adding mongoid_forums initializer (config/initializers/mongoid_forums.rb)...
create config/initializers/mongoid_forums.rb
(erb):5:in `template': undefined method `per_page' for MongoidForums:Module (NoMethodError)
The route is successfully added, however the initializer/mongoid_forums.rb is a blank file. The method is defined exactly how radar/forem does it, what could be the error?
Here is the relevant code:
Per page method definition: https://github.com/NJayDevelopment/mongoid_forums/blob/master/lib/mongoid_forums.rb#L33
Mattr accessor:
https://github.com/NJayDevelopment/mongoid_forums/blob/master/lib/mongoid_forums.rb#L9
Initializer template:
https://github.com/NJayDevelopment/mongoid_forums/blob/master/lib/generators/mongoid_forums/install/templates/initializer.rb
Install generator at error point:
https://github.com/NJayDevelopment/mongoid_forums/blob/master/lib/generators/mongoid_forums/install_generator.rb#L47
Turns out when you try requiring mongoid_forums in pry, you'll see that an error involving decorators occurs. The issue is fixed here in my pull request to decorators: parndt/decorators#13
It's because of the way files are required and how load! is called over there.
Waiting on PR status, that is the same version that radar/forem uses as well.

How do I stub a view in rspec-2

I'm in the process of upgrading an app to Rails 3/Rspec 2. I see that
stubbing a view helper method has changed in Rspec 2. It looks like
instead of doing template.stub!, we're now supposed to do view.stub!,
but I can't seem to get this to work on beta 10. I get an "undefined
local variable or method `view' for # < RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x106785fd0>"
error.
I see that in this commit David removed the view
method, but I can't figure out what it was replaced with. Something
in ActionView::TestCase::Behavior?
I'm on rails 3.0.0.beta3.
Any idea what I'm missing?
This turned out to be a bug in rspec-rails after moving more of the functionality back to ActionView::TestCase::Behavior. David re-exposed _view as view, so view.stub! is still the way to go. It was just temporarily broken.

Resources