Padrino Tutorial: Can't Modify Frozen String (Runtime Error) - ruby

I am following the Padrino tutorial from here:
https://www.padrinorb.com/guides/blog-tutorial
I am copy and pasting the commands but I quickly ran into an error I don't understand:
$ padrino g controller posts get:index get:show
create app/controllers/posts.rb
create app/views/posts
apply tests/shoulda
/Users/waprin/.rvm/gems/ruby-2.1.0/gems/padrino-gen-0.12.4/lib/padrino-gen/generators/controller.rb:66:in `prepend': can't modify frozen String (RuntimeError)
from /Users/waprin/.rvm/gems/ruby-2.1.0/gems/padrino-gen-0.12.4/lib/padrino-gen/generators/controller.rb:66:in `create_controller'

This might be a bit late, but in case anyone else runs across this error (and because I just worked through the same tutorial) I'll post anyway...
It looks like there's an issue when generating controllers if a test component is specified. In this case you're using shoulda, but the same happens when using rspec and maybe others. It's been reported as a bug: https://github.com/padrino/padrino-framework/issues/1850 and has been fixed, but isn't yet part of a stable release.
One option to fix this would be to change your Gemfile to work with the latest from their github repo. To do this delete your GemFile.lock file, and comment out the line under 'Padrino Stable Gem' in your GemFile:
gem 'padrino', '0.12.4'
then uncomment the line under 'Or Padrino Edge':
gem 'padrino', :github => 'padrino/padrino-framework'
then re-run bundle install.
Of course, you'll no longer be running the stable release, and that may come with other trade-offs.
As a side-note, I believe that the guide on that page is fairly out of date. I also needed to replace:
get :index do
#posts = Post.all(:order => 'created_at desc')
render 'posts/index'
end
with:
get :index, :provides => [:html, :rss, :atom] do
#posts = Post.order('created_at desc')
render 'posts/index'
end
in the Post controller as the active record interface has changed since the time that the guide was written.

I was able to sole this problem by simply going to padrino gem path.
For me it was:
/Users/ahmadhassan/.rvm/gems/ruby-2.2.0/gems/padrino-gen-0.12.4/lib/padrino-gen/generators
open controller.rb and change line number 61:
path = #controller
to
path = #controller.dup

Related

Updating from FactoryGirl to factoryBot results in NoMethodError

I tried to convert from FactoryGirl to FactoryBot. This should not be a big issue but i do not get it to work. The code:
Added to Gem File
gem 'factory_bot'
Added to spec_helper
FactoryBot.definition_file_paths = %w(spec/factories)
FactoryBot.find_definitions
config.include FactoryBot::Syntax::Methods
Factory
FactoryBot.define do
factory :user do
first_name 'John'
last_name 'Doe'
birthdate { 21.years.ago }
admin false
end
end
When i try to run a rspec test i get following error:
NoMethodError: undefined method 'first_name' in 'user' factory!
Method_missing at C:/jruby-9.1.17.0/lib/ruby/gems/shared/gems/factory_bot-5.0.2/lib/factory_bot/definition_proxy.rb:97
block in (root) at <path to factory>
It seems to me the gem is correctly loaded into the project, the factoryBot code is executed. But for some reason it does not recognize the structure of the factory.
Note:
- I did a bundle install/update
Like people said in the comments static attributes like first_name 'John' have been deprecated on v4 (check this guide) and then removed on v5, the alternative is to make them like dynamic attributes: first_name { 'John' }.
They even included a Rubocop to help you fix all of your factories:
rubocop \
--require rubocop-rspec \
--only FactoryBot/AttributeDefinedStatically \
--auto-correct
My recommendation is to migrate slowly but surely, go from FactoryGirl to FactoryBot using a similar version, run your specs, check for all deprecation warnings, run the custom Rubocop to auto-correct your factories, then only migrate major versions after reading the changelog.
I agree the message could be a bit better, usually the main goal of deprecation is to simplify code and reduce branching logic, so once the deprecation warning was there for enough time and it is time to remove it, any detection of the old usage would be extra code that had to be removed, common result in open source projects.
Glad you found your way out.

Trying to change Ruby Cucumber Transforms Code to Cucumber 3.0.0 Replacement

I recently found this old project for setting up ruby, cucumber, and capybara on this page: jonathanchrisp/capybara-cucumber-ruby-kickstarter
I was able to fix some runtime errors by adding the following line in support/env.rb
require 'capybara/dsl'
However, whenever I run "cucumber" to run the code, it always fails with
undefined method `open_google_maps' for "google maps":String (NoMethodError)
I think the problem is that the "Transforms" code in features/step_definitions/transforms.rb was deprecated in cucumber 3.0.0. The code I am looking at is over 3 years old so I am not sure if the original author will still maintain it. I did reach out to him. In the meantime, I was wondering if there is a Rub/Cucumber expert who can tell me what is the proper way to change the old transform code from:
Transform(/^google maps$/) do |impersonator|
google_maps
end
Transform(/^the user$/) do |impersonator|
#current_user
end
Transform(/^a anonymous user$/) do |impersonator|
a_anonymous_user
end
to something that works in cucumber 3.0.0 and later.
Transforms were removed from Cucumber 3 and you need to swap over to using ParameterTypes. How to do that is documented in the 'Upgrading to Cucumber 3' blog post at https://cucumber.io/blog/2017/09/21/upgrading-to-cucumber-3 .
For your first transform above it would be something like
ParameterType(
name: 'GoogleMaps'
regexp: /google maps/,
transformer: -> (_) { google_maps }
end

How to use ohai gem

In the OpsCode Wiki there is there following documentation:
require 'ohai'
# ...
# Profit! ;-)
How can I print the JSON data provided by the 'ohai' command but using IRB? I tried to see the code in application.rb but I get empty data.
require 'ohai/application'
ohai = Ohai::System.new
ohai.json_pretty_print
=> "{\n\n}"
I am not trying to do this within Chef (or Shef), I just want to use the ohai gem itself, in my own app.
Poking about in the Ohai::Application class (what you get when you run ohai), #run_application instantiates Ohai::System and, unless it was configured by a file, it calls all_plugins to populate it with data.
Presumably, Ohai::System#all_plugins delegates the data collection to the lib/ohai/plugins directory.
$ irb -rohai
> system = Ohai::System.new
=> #<Ohai::System:0x00000100988950 #data={}, #seen_plugins={}, #providers={}, #plugin_path="", #hints={}>
> system.all_plugins
=> true
> puts system.to_json
{"languages":{"ruby":{"platform":"x86_64-darwin10.8.0","version":"1.9.2", ...
> system.to_json.size
=> 42395
I too searched for a simple and good library to do that...
I came across
Usagewatch
and after 5 minutes had my monitoring.rb done..
Install : gem install usagewatch
Install on mac : gem install usagewatch_ext
Refer to this article for more info

wrong number of arguments (3 for 2) after upgrading from Rails 3.0.14 to Rails 3.1.4

Everything was working fine in Rails 3.0.14, but after changing
gem 'rails', '3.0.14' to gem 'rails', '3.1.4' and running bundle update rails I now get the following error:
Started GET "/" for 127.0.0.1 at 2012-03-16 11:11:44 -0400
Processing by PagesController#index as HTML
Completed 500 Internal Server Error in 54ms
ArgumentError (wrong number of arguments (3 for 2)):
app/controllers/application_controller.rb:37:in `customize_by_subdomain'```
The most popular answer seemed to be that sqlite3 needed to be updated, but I did bundle update sqlite3 and I still have the same problem.
Here is the full trace: https://gist.github.com/2050530
The method that it is complaining about looks like this:
35 def customize_by_subdomain
36 subdomain = (request.subdomain.present? && request.subdomain != 'www' && request.subdomain) || 'launch'
37 #current_org = Organization.find_by_subdomain(subdomain) || Organization.find_by_subdomain('launch')
38 end
I have looked at the multitude of similar questions and I not found anything that solves my problem. The closest was question to mine was: wrong number of arguments (3 for 1) after upgrading rails from 3.1.1 to 3.1.3 but I am using authlogic and the version I am using didn't change after upgrading rails.
The only other interesting thing is my entire test suite passes, except for one request/integration spec which goes through the process of creating a new user. It seems strange that my request specs work fine when I can't even access a page in development.
Any ideas on what I can do to get to the bottom of this?
It looks like your New Relic plugin may need to be updated to a new version. In your stacktrace, the first line is from the New Relic code in your plugins folder. From their site, it looks like they released new Rails 3.1-specific code:
http://blog.newrelic.com/2011/07/29/for-the-active-record-new-relic-support-for-rails-3-1-is-here/
In the blog post, they talk about changes to the way ActiveRecord does logging, and your exception was triggered on the log_with_instrumentation method.
It looks like now you should install it as a gem rather than a plugin:
https://github.com/newrelic/rpm
Hope this helps.

How can fixtures be replaced with factories using rails3-generators?

I'm trying to replace fixture generation with factories using rails3-generators:
https://github.com/indirect/rails3-generators#readme
The gem is included in my Gemfile and has been installed:
# Gemfile
gem 'rails3-generators', :group => :development
I added the following to application.rb:
# application.rb
config.generators do |g|
g.stylesheets false
g.fixture_replacement :factory_girl
end
Yet 'rails g model Insect' is still generating fixtures ('insects.yml'). Is this working for others using Rails 3.0.4 and rails3-generators 0.17.4?
'rails g' shows the new generators available (such as Authlogic and Koala), but 'rails g model' still lists fixtures and doesn't refer to factories.
What else should I add to get this to work? Thanks.
Edit: I ran the gem's test suite, which includes a test for this, and it passes. No clue why it doesn't work with my app.
Edit2: I tried again with a test project and get the same result: fixtures instead of factories. If anybody could confirm whether this works for them with Rails 3.0.4 and rails3-generators 0.17.4, that would be helpful too because it would imply that I'm doing something wrong with my projects.
Edit3: It works if I run 'rails g model Insect -r factory_girl'. I thought the generator configuration in application.rb was supposed to take care of that, so this seems to be the source of the problem.
Searching around I found the following, which may help:
Try specifying a directory option for factory_girl's factories:
config.generators do |g|
g.stylesheets false
g.fixture_replacement :factory_girl, :dir => "spec/factories" # or test/factories, as the case may be
end
If you're using Test::Unit, try the following:
config.generators do |g|
g.stylesheets false
g.test_framework :test_unit, :fixture_replacement => :factory_girl
end
In both cases you will still need the rails3-generators gem, although there is a push to get that functionality into factory_girl_rails.
This Rails bug indicates that, at some point, the g.fixture_replacement code may not have worked right. Perhaps a test in 3.0.5 is in order. :)
A short update 9 years later:
instead of "factory_girl_rails" (which is deprecated now) use "factory_bot_rails".
Now, the factory gets created automagically:
$ rails g model tester name:string
Running via Spring preloader in process 31467
invoke active_record
create db/migrate/20200327152901_create_testers.rb
create app/models/tester.rb
invoke rspec
create spec/models/tester_spec.rb
invoke factory_bot
create spec/factories/testers.rb
I use rails 5.2.4, but this should also work with rails 6.

Resources