Cleaning up my controllers, can I have Merb style action defs today? - ruby-on-rails

In Rail 2.3.2 can I have merb style action definitions:
Eg: instead of
def show
#user = User.find(params[:id])
end
Can I have:
def show(id)
#user = User.find(id)
end
What kind of crazy monkey patching do I need to do to get this working, note I only need this working for MRI so ParseTree is an option.
Note: there is a Rails 3 port of this functionality now.

You could change all params in to instance var's (eg. #id) in a before_filter...

EDIT: I was wrong: Method#parameters has been added to Ruby 1.9.2.
Original:
No, this is not possible in Rails.
There is almost zero chance of this making it into Rails 3.0. merb-action-args used ParseTree which doesn't and will not work on Ruby 1.9, therefore making it unlikely that it will be included in Rails.

I am running Rails 2.3.3 and User.find(id) works just fine.

Related

Rails5 how to refactor a find_or_create_by

I got some old code I'm trying to port over to Rails 5 (reasons)
And I ran into this....
def vol_event_for_date(date)
VolunteerEvent.find_or_create_by_description_and_date("Roster ##{self.id}", date)
end
def vol_event_for_weekday(wday)
VolunteerDefaultEvent.find_or_create_by_description_and_weekday_id("Roster ##{self.id}", wday)
end
I know from a post I've seen on S.O. that the find_by thingie is an old outdated Rails helper of some kind so my question is...how can I refactor this for a Rails 5.0.7 app?
The change is actually very simple as explained in the Rails 4 Active Record Deprecations
find_or_create_by_... can be rewritten using find_or_create_by(...).
VolunteerEvent.find_or_create_by_description_and_date("Roster ##{self.id}", date)
just changes to
VolunteerEvent.find_or_create_by(description: "Roster ##{self.id}", date: date)

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.

Upgrading ruby to 2.3.4 with rails 3.0.5

I am trying to upgrade my rails 3.0.5 application with ruby 2.3.4. Originally it was ruby 1.9.3. I was able to fix most things by updating the gems. However, i m stuck on this one problem where when creating new active record objects, the time does not convert properly.
For example
Product.new(:bought_on => Date.today) will save the object with bought_on to be the date, not datetime.
I was able to narrow down the problem to the file
activerecord-3.0.20/lib/active_record/attribute_methods/time_zone_conversion.rb
For some reason its not calling these two functions, define_method_attribute and define_method_attribute=.
Any ideas?
I found the issue, the define_method_attribute under time_zone_conversion.rb is a protected method, and in ruby 2, the respond_to function always returns false for protected methods. Had to monkey patch to remove the protected attribute.

Rails 4.2: Getting "Unpermitted parameters" on user creation after adding `protected_attributes` gem

I am upgrading from Rails 3.2 to 4.2 and wanted to follow Ryan Bates' advice of getting things working as quickly as possible before doing any major refactoring.
To that end, I installed the protected_attributes gem because I was under the impression that with this gem installed I wouldn't need to implement the strong params approach in my controllers immediately and could continue using attr_accessible in the models until I have time to refactor.
I'm not getting any errors about attr_accessible itself, but when I try to create a user in development I get Unpermitted parameters: first_name, last_name, phone despite having all of those as arguments in the User model's attr_accessible method.
Can someone point out what I'm doing wrong here?
That's not the correct approach. Instead of porting a legacy, deprecated feature from 3.2 to 4.2, what you really want to do instead is the opposite: install strong_parameters gem in Rails 3.2 and make sure to replace the attr_accessible before the upgrade.
Rails 4.x is not really designed to use protected attributes anymore, therefore you will encounter a lot of issues trying to reintroduce it.
To use strong params you will have to update your controller's code (which is what I recommend to do, since it won't cost too much work).
In general the implementation of using strong_parameters is as follows:
def create
#model = Model.create(model_params)
if #model.persisted?
# logic
else
#logic
end
end
private
def model_params
params.require(:model).permit(:model_attrbite1, :model_attribute2)
end

How do I apply a ruby memoization pattern to code this code

I am upgrading an app from Rails 3.2.11 to 3.2.17 and I'm getting the following error message:
DEPRECATION WARNING: ActiveSupport::Memoizable is deprecated and will be removed in future releases, simply use Ruby memoization pattern instead.
I know what memoization is, and the offending code appears to be the following:
def api
#client.vm_by_name(name) if cluster
end
memoize :api
I'm not quite sure how to memoize this using a ruby memoization pattern. The previous techs have memoized the api method. Anyone got any ideas?
Use this:
def api
#api ||= #client.vm_by_name(name) if cluster
end
Note on thread safety.

Resources