Gem with models - ruby-on-rails-3

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"

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.

How do I make gemspec dependencies autoload in a Rails 3 app, using a Gemfile

I have a Rails 3 app that I am turning into a Rails engine / gem.
This engine has some gem dependencies that I have put inside it's .gemspec file.
I have created a new 'parent' Rails 3 app, and I would like to add my engine gem to the Gemfile and have the gem's dependencies automatically 'loaded', but this does not work for me! bundle install installs the gem dependencies fine, but when I start the server, the app crashes because they are not loaded.
For example, my engine's gemspec contains these lines:
s.add_runtime_dependency(%q<rails>, ["= 3.0.7"])
s.add_runtime_dependency(%q<acts_as_commentable>, [">= 3.0.1"])
s.add_runtime_dependency(%q<haml>, [">= 3.1.1"])
.. and the parent Rails 3 application has these lines in its Gemfile:
source 'http://rubygems.org'
gem 'my_engine', :path => "~/src/gems/my_engine"
But I get the following error:
undefined local variable or method `acts_as_commentable'
from /home/user/src/gems/my_engine/app/models/account.rb:66:in `<class:Account>'
But if I add gem 'acts_as_commentable', '>= 3.0.1' to the Gemfile of the parent Rails 3 app, then the gem is loaded and the error disappears.
I am using Rails 3.0.8.
Does anyone have any suggestions? Do I need to change something about the way my engine is loading?
During main Rails app boot, Bundler will only require dependencies directly listed in the Gemfile but not any sub-dependencies. It's your library's/Engine's responsibility to require its dependencies when it itself gets required. You can do so using initializers in your Railtie.
class MyRailtie < Rails::Railtie
initializer "require stuff" do
require "stuff"
end
end
In our Rails Engine we used a small trick to require dependencies automatically. Unfortunately you can't specify whether or not they should load in the .gemspec, which would allow for greater control.
Gem.loaded_specs["our_rails_engine"].dependencies.each do |d|
begin
require d.name
rescue LoadError => le
# Put exceptions here.
raise le if d.name !~ /factory_girl_rails/
end
end
I'm looking at Spree (the superhero of Rails Engines!), and they do this in spree_core-0.60.1/lib/spree_core.rb:
require "rails/all"
require 'state_machine'
require 'paperclip'
require 'stringex'
require 'will_paginate'
require 'nested_set'
require 'acts_as_list'
require 'resource_controller'
require 'active_merchant'
require "meta_search"
require "find_by_param"
So the answer is that within your gem, you have to require all of it's gem dependencies one by one. Well, that's how I will do it for now. But please comment if this ever changes in the future.
Seems it don't work, i create a host project and a sub-project with rails 3 engine.
Added the gem to engine's gemspec
s.add_dependency 'simple_form'
then added the require to engine_name.rb like below
require 'simple_form'
But if delete the line [gem 'simple_form'] in host project's Gemfile, it will show undefined immediatly

How to access Rails 3 engine models in rails console

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

Custom Model - "no such file to load -- odbc"

I'm trying to create a custom model to interface with a database that isn't supported navtively by Rails or through the OpenLink ODBC driver. For some reason, I'm getting the following error:
no such file to load -- odbc
I'm at a lost as to why this is occurring as the ruby-odbc gem is in the GEM_PATH and I have a small script that tests this to ensure that the connection works and it worked perfectly. Does Rails ignore the gem_path? Is there something else I need to configure? I checked the GEM_PATH in Rails by writing it out to the browser and it matches what I saw in a terminal window.
FYI: I'm using rvm to manage ruby and all of my gems. Not sure if this makes any difference, though it really shouldn't from what I can tell.
Does your Gemfile include something like the following
gem 'ruby-odbc'

Rails 3: how to load files in /lib?

I'm new to rails and making some sort of noob mistake: I frequently need to count the number of lines in a file, so I'm trying to monkey patch class File like this:
class File
def self.line_count( filename )
%x{wc -l #{filename}}.split.first.to_i
end
end
I saved this to /lib/file_util.rb. I thought that this was supposed to be auto-required, so that I could just use it, but that doesn't work:
$ rails console
>> File.line_count('Gemfile')
NoMethodError: undefined method `line_count' for File:Class
...
So I try to require it manually, no joy:
>> require '<myproj>/lib/file_util.rb' # same result with require 'file_util.rb'
=>nil
But it works if I require it within IRB:
$ irb
>> require '<myproj>/lib/file_util.rb'
=> true
>> File.line_count('Gemfile')
=> 22
I also tried to to add the require to config/application.rb:
...
Bundler.require(:default, Rails.env) if defined?(Bundler)
require 'file_util.rb'
module <myproj>
...
and I get:
$ rails console
<myproj>/config/application.rb:9:in `require': no such file to load -- file_util.rb (LoadError)
What am I doing wrong?
Ok, I seem to have mostly figured it out. Rails doesn't automatically require everything under /lib. It only auto loads when you try to use a new class name that matches a file name in lib. So if I define line_count in class FileUtil instead of File it automatically finds and loads 'file_util.rb'. But patching File and naming the patch file 'file.rb' doesn't work, since the File class is already defined, so Rails doesn't go looking for a definition.
My other problem was that I was trying to do the require too soon in the startup sequence, before Rails had a chance to enhance require to look in its directories. When I added "require 'file_util'" to config/environments/development.rb it works fine.
But this doesn't explain why I can't manually require the file from within rails console.
Monkeypatching classes can be done more easily by adding a file in config/initializers. All of those files are automatically loaded by Rails at startup.
You can call your initializer file anything you want. Try config/initializers/file.rb.