what to set in Capybara.app for a Middleman project? - capybara

In the env.rb to use Capybara you should setup Capybara.app = something
Middleman is based on sinatra so I was thinking to use Sinatra::Application but gives an error
Anyone know what should be put to set up Capybara in the proper way?

Although I've recently stated this answer in the (recently closed) GitHub issue that #bhollis gave, I should fill in the answer here as well in keeping with the spirit of StackOverflow.
In a spec_helper.rb file added to a spec folder in the root of your Middleman project, the assignment I've used is Capybara.app = Middleman::Application.server.inst - however, I configure it a little bit too like so:
Capybara.app = Middleman::Application.server.inst do
set :root, File.expand_path(File.join(File.dirname(__FILE__), '..'))
set :environment, :development
set :show_exceptions, false
end
A full example of this using RSpec can be found here.

The answer will eventually be at https://github.com/middleman/middleman/issues/895

Related

Ruby on Rails: configure web_console in initializer

I am using the web_console gem and I would like to add some IPs to the whitelist. For reasons that would probably go to far to explain, can't simply add something to the config/application.rb or config/environments/development.rb. However I can create an initializer config/initializers/.
I simple tried this in config/initializers/99-webconsole.rb, but while the file is loaded (--> debug message is shown), the web console does not seem to pick up my settings.
Rails.application.configure do
config.web_console.whitelisted_ips = '10.10.0.0/16'
p "Debug: this is loaded."
end
I assume it's related to some kind of race condition? Providing the same line in config/environments/development.rb works, but as said, I sadly can not change that file.
Based on this code https://github.com/rails/web-console/blob/e3dcf4c588af526eafcf1ce9413e62d846599538/lib/web_console/railtie.rb#L59
maybe there is a code in your initializer that configuring config.web_console.permissions, so your whitelisted_ips config is ignored
whitelisted_ips is also deprecated
and have you checked that you are using v4.2.0, the permissions was buggy and fixed by this commit https://github.com/rails/web-console/commit/6336c89385b58e88b2661ea3dc42fe28651d6296

Rspec: Capybara server map a directory

I have a set of files in a folder under spec/support/fixtures directory. I need those files to be accessible through an uri such as "http://127.0.0.0:#{Capybara.current_session.server.port}/test_fixtures"
After many trail and errors I ended up the following solution: In rails_helper.rb I added the following code:
Capybara.app = Rack::Builder.new do
map '/' do
run Rails.application
end
map '/test_fixtures' do
run Rack::File.new('spec/support/fixtures')
end
end.to_app
It works well, but the solution to re-create Capybara app does not seem elegant to me. I'm looking for a better solution... or is it ok like this ?
Thanks
For your stated goal you have implemented things correctly and that is exactly what the Capybara.app setting is meant for. The only thing I would recommend doing is moving it into a separate file and requiring that file in your rails_helper.

How to configure a GEM in a Ruby on Rails app

Sorry, I'm back with another noob/dumb question. However, I have been working on this for a while now and can't seem to find a definitive answer/solution.
I found this answer from SObut it's really specific to the gem in question... I also found another answer or 2 but it really wasn't clear enough.
So I'm trying to use this gem for my app: https://github.com/shideneyu/kraken_client#configuration and there's a whole section about configuration that I would like to use:
KrakenClient.configure do |config|
config.api_key = ENV['KRAKEN_API_KEY']
config.api_secret = ENV['KRAKEN_API_SECRET']
config.base_uri = 'https://api.kraken.com'
config.api_version = 0
config.limiter = true
config.tier = 2
end
Note that they do not precise any 'require' in this code so I'm guessing that I won't be needing it (unlike the 'phaxio' gem from the answer I found on SO)?
I've tried to create a kraken_client.rb file in /config/initializers/ but when prompting the rails console it won't use it.
My objective is to first test the gem through the console to be able to build the rails app on that afterwards.
I could use the other option cited in the gem repo:
KrakenClient.load({base_uri: 'https://api.kraken.com', tier: 3}).config.tier
But I'm really not sure how to use that either (sorry... I'm really, really new). So my question is how can I configure this gem and generally how are gems configured once installed in a rails project?
Thanks in advance for your help!
in config/initializers/load_lib.rb(create if does not exist) write: require 'kraken_client' and save
after it come to rails c and call it

Access APP_CONFIG['var'] inside routes? or supply routes with variables?

I need to do in rails 4 supply some ip address to set a constraint on certain routes.
Is there a way to get this data from a config file without harcoding it into the routes file?
Im using a yaml file and initializer for app variables like:
APP_CONFIG = YAML.load_file("#{Rails.root}/config/application.yml")[Rails.env]
so normally I could do:
constraints(:ip => %w[APP_CONFIG['app_url']]) do
.. my routes..
end
This fails in the routes.rb is there a way to fix this?
The routes.rb file is a ruby file which is instantiated once, and loaded into memory.
You can just add the ruby code inside it and it will be executed once:
Rails.application.routes.draw do
app_config = YAML.load_file("#{Rails.root}/config/application.yml")[Rails.env]
constraints(:ip => %w[app_config['app_url']]) do
.. my routes..
end
end
This will instantiate the routes.rb file with the variable loaded from the yml and available throughout your rails routes app. You don't even need to use a env variable. Local variable seems a better idea.
You can also put logic inside and make it environment dependant:
if Rails.env.production?
app_config = YAML.load_file("#{Rails.root}/config/application.yml")[Rails.env]
constraints(:ip => %w[app_config['app_url']]) do
.. my routes..
end
else
.. my routes ...
end
Taking a look at the initialization process of rails (http://guides.rubyonrails.org/initialization.html). You'll see that routing is actually loaded quite early (and earlier than application.rb or other initializers). It has therefore not yet loaded this file.
A way round this would be to place this into your boot.rb:
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
#Now load app config:
require 'yaml'
APP_CONFIG = YAML.load_file(File.expand_path('../../config/application.yml', __FILE__))
I believe you are running into a load order issue. You could probably hack around this, but...
I would highly recommend using Figaro to solve this problem. It is a gem specifically designed for rails configuration and will work nicely with 12 factor app deployments (like Heroku): https://github.com/laserlemon/figaro
I am using Figaro in the app I am currently working on and was able to confirm access to the env variables within my routes file. I believe this gem will solve your current issue and other config issues you don't even know you have yet!

Rails: Per-environment initializers?

I'd like the code in one of my initializers (in config/initializers/) to be run only for the :development environment, but not :test or :production. What's the best way to do that?
Pasting it into config/environments/test.rb seems unclean, and I don't quite like wrapping the entire initializer file in an if Rails.env == 'development' do ... end statement. Is there some canonical way to do this?
(Background: To speed up test load times, I'm trying to move the Barista gem into the :development group of my Gemfile, but config/initializers/barista_config.rb calls Barista.configure, so now it chokes on that in test (and production) mode.)
I'm pretty sure your only two options are putting the configuration code in config/environments/development.rb or wrapping your initializer code with your if block. You can tighten up your second option by doing if Rails.env.development?, though.
I don't know if this is a good idea, but it's a different idea.
You could create a config/initializers/development directory (or config/environments/development/initializers), put your barista_config.rb in that directory, and then include anything in that directory from config/environments/development.rb.
I don't know if that's a good idea or not but it's at least a third option...just in case you're still thinking about this problem three and a half years after asking the question.
Barista has a config setting for this:
Barista.configure do |c|
c.env = :development
end
This will only recompile coffescript into js in dev mode and should speed up your tests.
Make sure you run:
rake barista:brew
before checking your code in.
https://github.com/Sutto/barista

Resources