I've pushed gem into github & rubygems, but constants of that gem are not being loaded into rails app. Tried this by loading from multiple sources and results are like this:
rubygems = not loaded
github = not loaded
local path = loaded
Interesting things is that when I load with rubygems/github I can load constants with require 'module/gem_name'
The gemspecs looks fine to me:
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'module/gem_name/version'
Gem::Specification.new do |spec|
spec.name = 'gem_name'
spec.version = Module::GemName::VERSION
spec.authors = ['Mike QWERTY']
spec.email = ['mike.qwerty#gamil.com']
spec.summary = 'some_desc'
spec.homepage = 'https://github.com/mike/gem-name'
spec.license = 'MIT'
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
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 do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.4.0'
spec.add_runtime_dependency 'graphql', '>= 1.6.0'
spec.add_development_dependency 'bundler', '~> 1.14'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
end
And structure is:
lib/module/gem_name/stuff/others.rb
lib/module/gem_name/extras.rb
lib/module/gem_name.rb
Also, I've been working on this gem basing on other gem that has the same module name. But it loads correctly into app, if this matters.
Any ideas on this?
The gem is not autoloading because the main file (gem_name.rb) is not at the root of the lib directory. It is recommended that your Gem adhere to the following structure:
% tree
.
├── gem_name.gemspec
└── lib
├── gem_name
│ └── some_file.rb
│ └── other_file.rb
└── gem_name.rb
Yours has an extra module nesting. In your Rails application.rb you should see Bundler.require(*Rails.groups). What this method does is loop through every Gem in your Gemfile and call require gem_name. This works for Gem's that follow the convention mentioned above. Your gem does not - so the auto require being done by Bundler doesn't find the correct file to use.
See the Ruby Gems Guide for creating your own Gem for a bit more information on proper code organization.
If you don't want to change the structure of your application, either add
require "module/gem_name" in an initializer, or within the Gemfile:
gem "gem_name", require: "module/gem_name"
Related
rails -v
Activating the command above results in:
bin/rails: 7: bin/rails: Syntax error: "(" unexpected
My rails in engine seems to not be running correctly, and is not responding.
It tells about syntax error when I try to call rails -v, or rails db:create or something like that.
rails s returns the same error
Here is the rest of the .gemspec file I used:
# frozen_string_literal: true
$:.push File.expand_path('lib', __dir__)
# Maintain your gem's version:
require 'vulnerabilities/version'
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = 'vulnerabilities'
s.version = Vulnerabilities::VERSION
s.authors = ['Evgeny Karpenko']
s.email = ['e.karpenko#isoit.ru']
s.homepage = 'https://new.docshell.ru'
s.summary = 'Vulnerabilities Plugin for Docshell4'
s.description = 'VulnerabilitiesPlugin for Docshell4'
s.license = 'proprietary'
s.files = Dir['{app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md']
s.add_dependency 'rails', '~> 5.1.6', '>= 5.1.6.1'
s.add_dependency 'active_model_serializers', '~> 0.10.7'
s.add_dependency 'kaminari', '~> 1.1', '>= 1.1.1'
s.add_dependency 'pg', '~> 1.1', '>= 1.1.2'
s.add_dependency 'swagger-blocks', '~> 2.0.2'
s.add_dependency 'rest-client', '~> 2.0', '>= 2.0.2'
s.add_development_dependency 'annotate'
s.add_development_dependency 'database_cleaner'
s.add_development_dependency 'factory_bot_rails'
s.add_development_dependency 'faker'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rspec-instrumentation-matcher'
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'shoulda-matchers'
end
here is the bin/rails file, which can not run File.expand_path
# frozen_string_literal: true
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.
ENGINE_ROOT = File.expand_path('..', __dir__)
ENGINE_PATH = File.expand_path('../lib/vulnerabilities/engine', __dir__)
APP_PATH = File.expand_path('../spec/dummy/config/application', __dir__)
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
require 'rails/all'
require 'rails/engine/commands'
The reason was in this line:
# frozen_string_literal: true
after removing it - rails starts to work properly
this line appeared due to RuboCop intervention
I used Swagger CodeGen to generate a Ruby client for an API. Here is what the resulting .gemspec file looks like:
# -*- encoding: utf-8 -*-
#
=begin
#convertapi
#Convert API lets you effortlessly convert file formats and types.
OpenAPI spec version: v1
Generated by: https://github.com/swagger-api/swagger-codegen.git
Swagger Codegen version: unset
=end
$:.push File.expand_path("../lib", __FILE__)
require "cloudmersiveConvertApiClient/version"
Gem::Specification.new do |s|
s.name = "cloudmersiveConvertApiClient"
s.version = CloudmersiveConvertApiClient::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Cloudmersive"]
s.email = [""]
s.homepage = "https://www.cloudmersive.com/convert-api"
s.summary = "Convert API lets you effortlessly convert file formats and types."
s.description = "Convert files and content between file formats. Convert data between file formats. Instantly."
s.license = "Apache 2.0"
s.required_ruby_version = ">= 1.9"
s.add_runtime_dependency 'typhoeus', '~> 1.0', '>= 1.0.1'
s.add_runtime_dependency 'json', '~> 2.1', '>= 2.1.0'
s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
s.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.1'
s.add_development_dependency 'webmock', '~> 1.24', '>= 1.24.3'
s.add_development_dependency 'autotest', '~> 4.4', '>= 4.4.6'
s.add_development_dependency 'autotest-rails-pure', '~> 4.1', '>= 4.1.2'
s.add_development_dependency 'autotest-growl', '~> 0.2', '>= 0.2.16'
s.add_development_dependency 'autotest-fsevent', '~> 0.2', '>= 0.2.12'
s.files = `find *`.split("\n").uniq.sort.select{|f| !f.empty? }
s.test_files = `find spec/*`.split("\n")
s.executables = []
s.require_paths = ["lib"]
end
Here is what the directory structure looks like:
However, when I build it, it produces a very small .gem file. If I then install that gem and I go look in the folder where it was installed, the folder is completed empty - nothing is inside.
Any ideas why this would happen? New to Ruby so any guidance you can provide is greatly appreciated.
On Windows operating system the following two lines would not obviously work:
s.files = `find *`.split("\n").uniq.sort.select{|f| !f.empty? }
s.test_files = `find spec/*`.split("\n")
They are used to produce arrays of files (sources and tests) out of your current directory structure. Both call a system to execute find command which does unlikely happen to exist in Windows. To overcome the issue (since your gem is relatively small) just list all the files manually, or use Dir#[] or use any unix to build the gem. Something like that should work (not tested):
s.files = Dir['./**/*'] # .select { |f| ... } # condition to include file
Just as an add-on the accepted answer, the typical approach is having your file list synced with the committed git files. This is how Bundler would generate the relevant part of the spec file:
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
This is obviously convenient as it can easily be controlled via .gitignore, etc. If not using git or if you don't have it installed for some strange reason, disregard this.
After I upgraded to Yosemite, Compass stopped compiling in my Rails 4 project. I am using Foundation with it (I am not sure if that makes a difference but thought I should mention it). Compass was compiling fine before the upgrade. My fils structure is as follows:
Config.rb (located in root level of project)
require 'zurb-foundation'
require 'sass-globbing'
# Change this to :production when ready to deploy the CSS to the live server.
environment = :development
#environment = :production
# Location of the theme's resources.
css_dir = "css/compiled"
sass_dir = "sass"
extensions_dir = "sass-extensions"
images_dir = "images"
javascripts_dir = "js"
# Require any additional compass plugins here.
#require 'ninesixty'
#require 'susy'
##
## You probably don't need to edit anything below this.
##
# You can select your preferred output style here (can be overridden via the command line):
# output_style = :expanded or :nested or :compact or :compressed
output_style = (environment == :development) ? :expanded : :compressed
# To enable relative paths to assets via compass helper functions. Since Drupal
# themes can be installed in multiple locations, we don't need to worry about
# the absolute path to the theme from server root.
relative_assets = true
# Pass options to sass.
# - For development, we turn on the FireSass-compatible debug_info.
# - For production, we force the CSS to be regenerated even though the source
# scss may not have changed, since we want the CSS to be compressed and have
# the debug info removed.
sass_options = (environment == :development) ? {:debug_info => false} : {:always_update => true}
I also tried:
# Location of the theme's resources.
css_dir = "app/assets/stylesheets/*"
sass_dir = "app/assets/stylesheets/*"
extensions_dir = "app/assets/stylesheets/*"
images_dir = "app/assets/stylesheets/*"
javascripts_dir = "app/assets/stylesheets/*"
and this:
# Location of the theme's resources.
http_path = "/"
css_dir = "."
sass_dir = "."
images_dir = "img"
javascripts_dir = "js"
output_style = :compressed
relative_assets=true
line_comments = false
compass.rb (located in my config folder)
# Require any additional compass plugins here.
project_type = :rails
Gemfile
source 'https://rubygems.org'
gem 'rails'
# Use postgresql as the database for Active Record
gem 'pg'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails'
# Use SCSS for stylesheets
gem 'sass', '3.4.5'
gem 'sass-rails'
gem 'sprockets', '~>2.11.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier'
# Add Compass to the project
gem 'compass'
gem 'compass-rails', github: 'Compass/compass-rails'
gem 'zurb-foundation'
# Add access to foundation classes
gem 'foundation-rails'
group :development do
gem 'better_errors'
gem 'binding_of_caller', :platforms=>[:mri_21]
gem 'quiet_assets'
gem 'rails_layout'
# gem 'sass-rails-source-maps'
end
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'jquery-ui-sass-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
gem 'font-awesome-sass'
gem 'simple_form'
gem 'carrierwave'
gem 'fog'
gem 'figaro'
gem 'mini_magick'
gem 'jquery-turbolinks'
gem 'rails_12factor', group: :production
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
gem 'high_voltage'
# Use Capistrano for deployment
gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'devise'
gem 'searchkick'
My file structure leading to the .scss files is yelper/app/assets/stylesheets
When I try to compile, it tells me:
No application integration exists for rails.
When I try to compile with project_type = :rails from compass.rb,commented out, compass watch does work (without tracking any files) but it tells me:
Compass can't find any Sass files to compile.
Is your compass configuration correct?.
If you're trying to start a new project, you have left off the directory argument.
So, my confusion is because it was working just fine before the Yosemite upgrade and now it just won't work and I have no clue why. Any help would be appreciated very much!
I have a Gemfile as part of rails app, which I am converting to a gem.
gem "jruby-openssl", "0.7.4"
gem "nokogiri"
gem 'xml-simple', :require => 'xmlsimple'
gem "httpclient"
How do I write, the .gemspec file, for the gem I create from this?
The gemspec I wrote looks like this.
Gem::Specification.new do |s|
s.name = "my_gem"
s.platform = Gem::Platform::RUBY
s.authors = [""]
s.email = [""]
s.homepage = ""
s.summary = %q{MyGem}
s.description = %q{MyGem}
s.files = Dir["{app,lib,config,public,db}/**/*"] + ["Rakefile", "Gemfile"]
s.require_paths = ["lib"]
s.add_dependency 'nokogiri', '>= 1.5.0'
s.add_dependency('xml-simple')
s.add_dependency 'httpclient'
end
However, the xmlsimple object is not called, because, the add dependency cannot take the same parameters as a regular gem call.
How do I write the gemspec to call xmlsimple like its done in the Gemfile ?
You can do:
s.add_dependency 'xml-simple'
Like you have already. However when requiring the library inside your application (perhaps inside lib/yourgem.rb) you do:
require 'xmlsimple'
Instead of:
require 'xml-simple'
I found a gem that is using xml-simple which may be useful for your reference. https://github.com/stjohncj/RESTRack
I have done the following steps:
Added to gemfile:
gem "recaptcha"
Added to config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.public_key = 'MyKeyHere'
config.private_key = 'MyKeyHere'
end
Added to view:
= raw recaptcha_tags
Ran:
bundle install
...then restarted server. The result?
undefined local variable or method `recaptcha_tags' for #<#<Class:0x1053baaa0>:0x1053b69c8>
The gem's README says to, in Rails 2 (yes, I know you're in 3—just a minute :D), include the following line to require the gem:
config.gem "recaptcha", :lib => "recaptcha/rails"
The important bit to note here is the instruction that Rails should essentially end up running require 'recaptcha/rails'. Your current Gemfile doesn't have that specified, so it may very well only be loading in the gem's core classes, not the Rails-specific tie-ins.
Try:
gem 'recaptcha', :require => 'recaptcha/rails'
Add the following gem into your gemfile.
gem 'recaptcha', :require => 'recaptcha/rails'