Making Engine: Lib class not found: unitialized constant (NameError) - ruby-on-rails

I'm build a gem and created:
"/lib/gem_name/resource.rb"
module GemName
class Resource
def initialize
# nothing
end
end
end
and in "/lib/gem_name.rb"
require "gem_name/engine"
module GemName
def new_resource
#resources << GemName::Resource.new
end
end
in "/test/dummy/config/initializers/gem_name.rb"
GemName.new_resource
when I start console, show this message:
unitialized constant GemName::Resource (NameError)
I check gem_name.gemspec and its contents is:
$:.push File.expand_path("../lib", __FILE__)
# Maintain your gem's version:
require "gem_name/version"
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "gem_name"
s.version = GemName::VERSION
s.authors = ["TODO: Your name"]
s.email = ["TODO: Your email"]
s.homepage = "TODO"
s.summary = "TODO: Summary of GemName."
s.description = "TODO: Description of GemName."
s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
s.add_dependency "rails", "~> 3.2.8"
# s.add_dependency "jquery-rails"
s.add_development_dependency "sqlite3"
end
Environment spec:
ruby 1.9.2
rails 3.2.8

Maybe you need to require gem_name/resource directly in "/lib/gem_name.rb" file?

Related

Unable to load classes from my gem to my rails app

I have some common code that I want to share between couple of my rails app.
I've generated my gem like so :
bundle gem document-common
My gemspec looks like so :
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'document_common/version'
Gem::Specification.new do |spec|
spec.name = "document_common"
spec.version = DocumentCommon::VERSION
spec.authors = ['Author']
spec.email = ['test#domain.com']
spec.summary = 'Common Models and Lib'
spec.description = 'Common Models and Lib'
spec.homepage = 'google.com'
spec.license = 'WTFPL'
spec.files = Dir["{app,config,db,lib}/**/*", "LICENSE", "Rakefile", "README.rdoc"]
spec.test_files = Dir["spec/**/*"]
spec.test_files.reject! { |file| file.match(/[.log|.sqlite3]$/) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
end
In my lib folder I have this :
document_common
document_common.rb
Folder document_common and ruby file document_common.rb, and the ruby file has this content :
require 'document_common/version'
module DocumentCommon
# Your code goes here...
end
Then inside document_common there is a version.rb with this content :
module DocumentCommon
VERSION = '0.1.0'
end
And document.rb with this content:
module DocumentCommon
class Document < ActiveRecord::Base
end
end
So I push this gem to my git repo. And then I add this gem info to my Gemfile in the rails 4 app do a bundle install, but when I refer to DocumentCommon::Document I get this error :
NameError: uninitialized constant DocumentCommon::Document
However if I retrieve the info about the version DocumentCommon::VERSION I get no errors and get the actual version. Also when I do DocumentCommon.constants I get [:VERSION]. What am I doing wrong here? What do I need to do to have access in my main rails app to DocumentCommon::Document model?
You can use the autoload, check out the devise gem does exactly that:
https://github.com/plataformatec/devise/blob/master/lib/devise.rb

Invalid gemspec in ...: wrong number of arguments (0 for 1+)

In my first attempt to create a gem, I have run into this error
Invalid gemspec in [rulers.gemspec]: wrong number of arguments (0 for 1+)
when I try to build it (gem build rulers.gemspec).
I have pasted the code of the gemspec file below.
Where have I made errors?
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'rulers/version'
Gem::Specification.new do |spec|
spec.name = "rulers"
spec.version = Rulers::VERSION
spec.authors = ["Noffica"]
spec.email = ["email#domain.com"]
spec.summary = %q{An attempt to create a Rails-like framework...}
spec.description = %q{... written in Ruby.}
spec.homepage = ""
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = ""
else
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
end
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
gem.add_runtime_dependency "rack"
end
The second last line should be
spec.add_runtime_dependency "rack"
The argument is spec, not gem.

Rails 4, custom gem with generator 'Could not find generator'

I'm building a gem simply to go through the processes, and I'm trying to add a generator to it. When I run $ rails g, my generator shows up:
Mygem:
mygem:install
but rails doesn't recognize it
rails g mygem:install
Could not find generator mygem:install.
I have my gem pointed to the latest version in my gemfile
#mygem/lib/rails/generators/mygem_generator.rb
require 'rails/generators'
require 'rails/generators/base'
module Mygem
class InstallGenerator < Rails::Generators::Base
def test
puts 'hi'
end
end
end
.
-mygem
- lib
- generators
- mygem
mygem_generator.rb
- mygem
version.rb
mygem.rb
- pkg
mygem-0.0.1.gem
.gitignore
Gemfile
LICENSE.txt
README.md
Rakefile
mygem.gemspec
.
#mygem.gemspec
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mygem/version'
Gem::Specification.new do |spec|
spec.name = "mygem"
spec.version = Mygem::VERSION
spec.authors = ["me"]
spec.email = ["email#email.com"]
spec.summary = %q{lalaala}
spec.description = %q{lalalalal}
spec.homepage = ""
spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake"
end
We've just made a gem, and have a generator which allows you to call rails generate exception_handler:install, like this:
#lib/generators/my_gem/install_generator.rb
module MyGem
class InstallGenerator < Rails::Generators::Base
def test
puts "hi"
end
end
end
This should help you - it works for us
I faced this issue after publishing my first public gem, it's because the bundler, or let's say the Rails app don't have access to the actual repository with the generator (I believe so).
I kept including gem 'spree_custom_checkout'
no luck to see the gem's generator when typing rails g.
Then I did this:
gem 'spree_custom_checkout', github: '0bserver07/Spree-Custom-Checkout'
and bam the generator shows up!
steps:
I did `gem build spree_custom_checkout.gemspec.
add it to the github repo, and push to the repostiory.
link to the repository.

Gem with shared models using devise and carrierwave

I have two apps that share several models. I created a gem that contains those models. Minus carrierwave functions and devise methods, it works great (commented them out to test). However when I add them back in I get errors:
undefined method mount_uploader
undefined method devise
I was able to get carrierwave partially working by placing this at the top of one of the models,
require "carrierwave"
require "carrierwave/orm/activerecord"
it would allow the file to upload but it wouldn't save it in the uploads folder, just in the temp one, and it would put the tmp hash in the database instead.
here is my gemspec-
journalbooks_core.gemspec
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'journalbooks_core/version'
Gem::Specification.new do |spec|
spec.name = "journalbooks_core"
spec.version = JournalbooksCore::VERSION
spec.authors = [NAME]
spec.email = [EMAIL]
spec.description = "Contains the shared models of journalbooks.com and creative-queue.com"
spec.summary = ""
spec.homepage = ""
spec.license = "MIT"
spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_dependency "carrierwave", "= 0.9.0"
spec.add_dependency "devise"
end
Here is the journalbooks_core.rb that runs initially
require "journalbooks_core/version"
module JournalbooksCore
require "models/virtual_request"
require "models/creative_user"
require "models/user"
require "models/virtual"
require "models/cov_order"
end
and here is the model I have been trying to get to work
class CreativeUser < ActiveRecord::Base
require "uploaders/avatar_uploader"
require "carrierwave"
require "carrierwave/orm/activerecord"
...
mount_uploader :avatar, AvatarUploader
...
end

Distribute a Rails Generator as a Ruby Gem [duplicate]

This question already has answers here:
Rails 3 generators in gem
(2 answers)
Closed 8 years ago.
I am fairly new to Ruby/Rails framework. I have a simple generator in my Rails application at lib/generators. The generator's function is to copy one of the files in the templates/myfile.yml directory into config/myfile.yml.
Now I would like to use this generator in another app. So, I would like to package this as a gem. I have generated gem using bundler and it contains the following file structure:
lib/
mygem/
version.rb
generators/
mygem/
install/
templates/
somefile.yml
install_generator.rb
USAGE
mygem.rb
mygem.gemspec
Gemfile
Gemfile.lock
LICENSE.txt
Rakefile
README.md
Content of install_generator is as follows:
require 'rails/generators'
module Mygem
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
def generate_something
copy_file "something.yml", "#{Rails.root}/config/something.yml"
end
end
end
end
Following is the content of mygem.gemspec:
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mygem/version'
Gem::Specification.new do |spec|
spec.name = "mygem"
spec.version = MyGem::VERSION
spec.authors = ["Swaroop SM"]
spec.email = ["myemail#gmail.com"]
spec.description = %q{Something}
spec.summary = %q{Something}
spec.homepage = ""
spec.license = "MIT"
spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
end
I reckon there is something missing in my lib/mygem.rb
require "mygem/version"
module Mygem
# Your code goes here...
end
I have no problem in building and pushing it to rubygems.org.
Inorder to use this in one of my rails app's, I added this to the Gemfile and run bundle install. The problem is now when I run rails g mygem:install it throws an error that says:
Could not find generator mygem:install.
What am I doing wrong?
I solved the issue. I had forgotten to add rails as a dependency in my gemspec file. i.e., I had to include the following in mygem.gemspec:
spec.add_development_dependency "rails", "~> RAILS_VERSION"

Resources