How to access Rails 3 engine models in rails console - ruby-on-rails-3

In the mail app's rails console (irb), how to access engine's models.
update: Say "team" is my main app and "team_page" is the engine. "team_page" is required in main app in the gemfile through 'gem => "team_page", :path => "local/path/to/team_page"'.
when I go to team's rails console, I couldn't access team_page's models.

First you must know the module's name. To help with that, you can run a
bundle show team_page
to find its directory and explore over there (probably under lib/team_page.rb) until
you see the following definition:
module TeamPage
# ...
end
Let's say that the module is called TeamPage. Then just prepend double colon to its name like this:
::TeamPage::SomeModel.some_method

Related

How to set sinatra-authentication to use erb instead of haml?

I'm trying to set up the sinatra-authentication gem in a simple sinatra app, and running into an issue where sinatra can't find the correct views. I understand that sinatra-authentication uses haml by default, but I'm using erb in this app.
This in mind, I found in the sinatra-authenticaiton docs that there is a setting which allows you to change the template engine, by adding the following to your app file:
configure do
set :template_engine, :erb # for example
end
I've added this to my app.rb file, and sinatra is still looking for the signup.haml when I try to hit the /signup route in my app.
A couple of notes:
I've included the gem in my Gemfile, and successfuly run a bundle install on my app.
source 'https://rubygems.org'
gem 'sinatra'
gem 'data_mapper'
gem 'pg'
gem 'dm-postgres-adapter'
gem 'sinatra-authentication'
I saw something in the documentation that suggested that I may need to specify the location of my view files, so I added the following to my configuration block.
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
**I think I've required the gem accurately in my app file by adding
require "sinatra-authentication"
use Rack::Session::Cookie, :secret => 'mys3cr3tk3y'
This gist is a current representation of my app.rb file in the root of my sinatra app. https://gist.github.com/rriggin/5378641#file-gistfile1-txt
Here is a screenshot of the error sinatra throws: http://cl.ly/image/0y041t0K3u3O
When I run the app locally, a 'dm-users' table is created in my local db as expected.
Is there another configuration setting that I'm missing in order to get sinatra-authentication to properly look for the erb templates rather than haml files. Any help would be greatly appreciated.
Thanks
The specs don't test that the template_engine setting works, and looking at the way the setting is called, I believe it's not correct, i.e.
send settings.template_engine, get_view_as_string("index.#{settings.template_engine}"), :layout => use_layout?
might better work as:
send app.settings.template_engine, get_view_as_string("index.#{app.settings.template_engine}"), :layout => use_layout?
that's what I reckon. If you fork the project, change the line and add it to your Gemfile and it works then consider writing a quick spec for it and you'll have improved the mainline of that project as well as fixed your problem.

rails +capybara +selenium can not open an external url

In a rails project, i add a feature file and step definition try to use capybara+selenium to load an external url,but it can not work? it has no any browser loading behavior,and from the console, it did not request the external url 'http://baidu.com' but 'http://localhost:3000' why, how can check for me,thanks very much!
**env.rb**
require 'cucumber/rails'
require 'capybara/rails'
require 'capybara/cucumber'
require 'selenium/client'
Capybara.current_driver = :selenium
Capybara.default_selector = :css
**setp_definition:**
When /^i visit baidu$/ do
visit('http://www.baidu.com/')
end
Then /^you should see Baidu$/ do
page.should have_content('Baidu')
end
****the infor on the console:****
[31m expected there to be text "Baidu" in "Browse the documentation Rails
Guides Rails API Ruby core Ruby standard library Welcome aboard You鈥檙e riding
Ruby on Rails! About your application鈥檚 environment Getting started Here鈥檚 h
ow to get rolling: Use rails generate to create your models and controllers To s
ee all available options, run it without parameters. Set up a default route and
remove public/index.html Routes are set up in config/routes.rb. Create your data
base Run rake db:create to create your database. If you're not using SQLite (the
default), edit config/database.yml
with your username and password." (RSpec::Ex
pectations::ExpectationNotMetError)[0m.

Gem with models

I created a Gem with models (actually, extracted it from the main project) to share amongst the projects we have in our platform.
We have dozens of models, so instead of requiring them one by one, I wrote the following code:
Gem.find_files("my_gem/models/*.rb").each { |path| require path }
I access one of the projects that has my_gem in the gem file and running rails c I get the following output:
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined method `has_attached_file' for #<Class:0x007fad4b93ccb8> (NoMethodError)
One of my models is using the gem paperclip, what is weird is:
If I remove the line declared above to load all the models
automatically, rails c runs fine
If I try to include onlye the model that uses paperclip require
"my_gem/models/paperclip_model" I receive the same error
So then I change my gem to not load any model, and when I try to reference any model from rails console, it says the class is not loaded, but then I run Gem.find_files("my_gem/models/*.rb").each { |path| require path } or require "my_gem/models/paperclip_model" 'they work perfectly and I am able to work with the model.
Has any of you seen the same issue?
Seems that changing require for autoload solved the problem
I changed Gem.find_files("my_gem/models/*.rb").each { |path| require path }
for
Gem.find_files("my_gem/models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "my_gem/models/#{filename}"
end
and now it is working.
It sounds like one of the models in your gem depends on Paperclip, but you don't explicitly set it as a dependency. So what's happening is that if your models get loaded before paperclip gets loaded, you'll see the UndefinedMethod error for has_attached_file.
If you use your models in a Rails application which has paperclip as a dependency, and you load those models after the console (or server) has spun up, Paperclip will be present, so you won't see this error.
The solution is to explicitly add paperclip as a dependency in your gemspec, something like:
s.add_dependency('paperclip')
Assuming that this gem will always be used in the context of a Rails application, this should work. If not, you might also need to add the following line to the top of your models that use paperclip:
require "paperclip"

Cucumber: undefined method error for path on visit

I have a cucumber step
When /^I go to the Add Suggestions form$/ do
visit new_manage_suggestions_path
end
and a route
namespace "manage" do
resource :suggestions
end
rake routes outputs
manage_suggestions POST /manage suggestions(.:format) manage/suggestions#create
When I run cucumber I get
undefined method `suggestions_path' for #<#<Class:0x000000064a4768>:0x000000064accd8> (ActionView::Template::Error)
Why is cucumber trying that path?
The new_manage_suggestions_path works fine in my app, I have a link that uses it and that is working fine.
In your routes definition, in order to have your app generate the correct routes, you need to switch from the singular resource to plural resources since you could potentially have multiple suggestions.
namespace "manage" do
resources :suggestions
end
More details can be found in the Rails documentation on singular resources, where you can see that the singular version does not include the namespace in its path names.

How do I get the root directory of a gem in the Rspec tests for my Rails project?

I've got a gem, we'll call it ToastMitten, which I'm including in one of my Rails apps. I'm writing some tests for ToastMitten in which I need to load a file, and I want to specify a path from the root of the gem.
I just tried using Rails.root.to_s, but that gives me something like /Users/me/projects/toastmitten/spec/dummy. I would have expected that path to end at toastmitten/.
What am I doing wrong?
Rails.root.parent.to_s
If it always gives back your dummy Rails app, just move up to the parent.
It looks like you are using a Rails engine (generated with enginex, hence the dummy app in your spec folder). If you need to require a file in your test using an absolute path, you can use the following:
file = File.expand_path(File.join(File.dirname(__FILE__), 'path', 'to', 'file.ext'))
root = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
Can you be more specific on where you are trying to require this file?
Also, The root example may require more ..'s.