Rails is it possible? - ruby-on-rails

Is it possible exec ruby code that is in a instance variable in a controller?
Example:
def something
#code = "redirect_to 'https://www.google.com/'"
exec(#code) // And then it would redirect.
end

DON'T DO THIS. Rails IS Ruby. In Ruby, you can execute any command inside a string using the eval method. And when you start really getting into it, there is class_eval.
#myvar = "puts 'SHOULD NOT HAVE DONE THIS!!'"
eval(#myvar) # SHOULD NOT HAVE DONE THIS!!
Keep safe.

Related

How to run script before every Rails console invocation?

I'm pretty tired of writing this line every time I want to open the Rails console:
irb(main):001:0> ActsAsTenant.current_tenant = User.find(1).account
Is there any way to run command/script before every "rails c"/"irb" invocation?
Thanks in advance!
Put the code you want to execute into .irbrc file in the root folder of your project:
echo 'ActsAsTenant.current_tenant = User.find(1).account' >> .irbrc
bundle exec rails c # ⇐ the code in .irbrc got executed
Sidenote: Use Pry instead of silly IRB. Try it and you’ll never roll back.
I wrote an extended answer to this in another question but the short answer is that if you are using Rails 3 or above you can use the console method on YourApp::Application to make it happen:
module YourApp
class Application < Rails::Application
...
console do
ActsAsTenant.current_tenant = User.find(1).account
end
end
end
You could put your setup code in a rb file, for example:
foo.rb:
def irb_setup
ActsAsTenant.current_tenant = User.find(1).account
end
launch irb like this:
irb -r ./foo.rb
and call the method (which will autocomplete pressing tab)
2.3.0 :001 > init_irb
In fact maybe you could put the code directly, without any method, and it would be executed when it is loaded. But I'm not sure if that would work or mess with the load order.

Debug 'env' in Rails initializer

I want to check the value of env[:clearance] to see why something is broken.
How can I do this. I tried puts:
config.user.current = Proc.new { env[:clearance].current_user }
puts "ENV[:CLEARANCE]: #{env[:clearance]}"
but that gets: undefined local variable or methodenv' for main:Object (NameError)`
How can I get some visibility into the env hash in this situation? BTW this is loosely related to the Clearance gem.
It's not clear what problem you are trying to solve or what config.user.current is (it's not Clearance). One problem however is that you are trying to use the method env in the proc where it will not be defined. The proc has its own scope.
If you just want to see what ENV is after the clearance middleware has run the simplest thing to do would be to put a binding.pry or debugger statement inside a controller and access env that way.
The clearance environment is set by middleware in response to a request. It will not be set in an initializer.

How do I output a variable in a rspec test?

Is there a quick way to output the value of variable in a rspec test? Something like this for example, in a controller to output a variable, I do:
raise variable.to_yaml
Is there something similar I can do in a rspec test to see the contents of a variable?
If you want the output to go into the log file (i.e. logs/test.log), you can use the rails logger.
Rails.logger.debug variable.inspect
Rails.logger.debug variable.to_yaml
If you want to see the output in the console, you can use the pretty printer 'pp'.
require 'pp'
it 'does something'
thing = Factory(:something)
pp thing
end
Or you can use good 'ol puts
puts thing.to_yaml
At this moment (Rails 4) you can log it:
p variable
Use this:
$stderr.puts variable.to_yaml
You can use the gem pry to do this
add gem "pry" in your gemfile
bundle install
Now you can any test any variable by putting "binding.pry" just after that variable. Run bundle exec rspec filepath and you will get something like rails c, then write directly your variable.
I hope it makes sense

"puts" method in rails 3

I've been doing some development with rails 3 and I was wondering why calling the "puts" method doesn't output to standard out.
How do I get this back?
Instead of puts, use logger.info:
logger.info #collection.inspect
You can also use the standard ruby way by calling STDOUT << 'your output'. Method put is not a rails speciality, it comes with ruby. If you use rails, you can also rely on the logger object.

How do I use a custom log for my rake tasks in Ruby on Rails?

I have a rake task that calls functions like this:
namespace :blah do
task :hello_world => :environment do
logger.info("Hello World")
helloworld2
end
end
def helloworld2
logger.info("Hello Again, World")
end
I want the log output to a custom log, and I really don't want to have to pass a log reference every time I make a function call. I found this somewhere (can't find it again):
def logger
##logger ||= Logger.new("#{RAILS_HOME}/log/blah.log")
end
But this does not work for me and I am not sure what it even does because I grabbed the code a long time ago and haven't used it until now. I can't search for ## on google (tried +"##" rails) to see what it does. Any help on this issue would be great. I am hoping for a quick solution and not having to install a gem or plugin (unless there is a really really good reason to.
Thanks!
rake disables logging in production mode. make sure you're running in development mode if you want it to log
What do you mean by "does not work for me"? I just tried this same code and it worked - created a new log file and put some text in it.
##logger is a class variable, it's a language issue, not Rails' one. I believe there's no need in further explanations :)
You've probably mistaken typing "function helloworld2" :)
Advanced Rails Recipes Recipe 84 from #topfunky shows how to define a custom logger. He has some code in the environment config file (production would look like this): RAILS_ROOT/config/environments/production.rb:
config.logger = RAILS_DEFAULT_LOGGER = Logger.new(config.log_path)
I'd test that out instead of redefining the class variable as you have. He might have something on http://nubyonrails.com to check as well.

Resources