Adding Geocder to Spree Extension - ruby-on-rails

I want to add geocoder gem to a spree extension and I have included in the the gemspec as
s.add_development_dependency 'geocoder', '~> 1.1'
and have added this line to lib/extension_name.rb file:
require 'geocoder'
When I do rake -T, I do not see any geocoder related tasks in my extensions and when I include this extension in a spree website, it throws
cannot load such file -- geocoder (LoadError)
error. Where am I going wrong?
In web, all I can see is to include the gem in gemspec and require it. What am I missing?

You're misunderstanding the gemspec dependencies a bit.
add_development_dependency is used for adding dependencies that aid in coding of the gem. Examples would be things like TestUnit, Pry, or RSpec. These dependencies won't be available in applications that use your gem.
add_dependency is used for dependencies which are important for the operation of your gem. For example, geocoder in your case.
If you make that change, you should be good to go.

Related

what is the best way to use sass in a sinatra application?

I'm writing a small Sinatra project, using rack and a Gemfile for hosting on Heroku. In order to use sass, I wanted to add gem 'sass', '~> 3.7', '>= 3.7.4' to my Gemfile and then add this to config.ru
require 'sass/plugin/rack'
use Sass::Plugin::Rack
I turns out that the sass gem is deprecated, as per this information: https://github.com/sass/ruby-sass, saying we should use libsass . That would mean that I need to add gem 'sassc', '~> 2.4' to the Gemfile ... I haven't figured out yet how and where add this line so my Sinatra app knows is should use libsass - any help would be much appreciated:
SassC::Engine.new(sass, style: :compressed).render
Now, it turns out that libsass is also deprecated, and the Sass blog recommends using dart sass as per this post https://sass-lang.com/blog/libsass-is-deprecated. I didn't find any information (yet) on how to implement dart sass in a ruby project, or Sinatra specifically.
So now I'm puzzled. What to use and how?

Bundle Update downgrades several gems in a gem

Disclaimer before reading: I have resolved this issue but I am asking because I still don't know the reasons behind it.
I'm working on an old gem that pulls assets into the asset pipeline. I'm not positive how the gem was originally created I imagine it was with rails plugin new static_assets.
Since it is a gem the Gemfile is not overly complex (I haven't made any changes to it):
source "http://rubygems.org"
gemspec
# jquery-rails is used by the dummy application
gem "jquery-rails"
But the Gemfile.lock has dozens of gems and dependencies showing up. For the most part, the gems seem up to date.
When I run a bundle update several of the gems go back to much older versions; like Rails 5 to Rails 3.
I believe I solved this by updating the Gemfile to
gem 'jquery-rails', '~> 4.3', '>= 4.3.3'
but I want to know why this was happening.
I'm not overly familiar with how Gemfile.lock gets created and updated but I was under the impression that it was based on the Gemfile, pulling all gems and their dependencies from the Gemfile. If all the gems in Gemfile.lock are dependant on jquery-rails why was it automatically downgrading them so unilaterally and by so much?

Rails 4. Gem creating. Include rubyzip gem

I'm creating my own gem(mountable plugin), and I need to include an older version of rubyzip gem. The reason for using the old version is that I have some code templates written in Rails 3. In the gemfile I include the old version as follows:
gem 'rubyzip', "~> 0.9.9"
It installs successfully. In the lib file where I wrote my gem-module I've added:
require 'devise' # work nice.. haven't any error.
require 'zip/zip' # give me error that it is not found
If I try require zip doesn't work either. If I don't require the gem in my lib file, the error disappeared but in the gem's controller I don't have access to Zip constant anymore.

What causes this dependency error in Rails 3?

I now get the following:
`dependencies.rb:239:in `require': no such file to load -- require_relative (LoadError`)
But I don't have enough information to figure out what is causing it or how to debug it.
What can I do?
It probably comes from linecache gem version 0.45 which was released yesterday. Rolling back to 0.43 will get you around this for now. I'm not sure if they intentionally broke support with Ruby 1.8.7 or not.
This is a dependency of ruby-debug-base.
Add something similar to the following in your Gemfile.
group :development, :test, :cucumber do
gem "linecache", "0.43"
gem "ruby-debug-base", "0.10.4.0"
gem "ruby-debug", "0.10.4"
end
Alternatively, add
gem 'require_relative'
to your Gemfile. It looks like linecache 0.45 needs it, but 0.43 doesn't, which is why downgrading linecache works.

Rails 3: define plugin gem dependency

I wrote a plugin that requires a gem as a dependency.
Where do I have to define this dependency?
I have tried to create a Gemfile in vendor/plugins/my_plugin/, but bundle install doesn‛t find this file.
Ok. I have solved.
1) Create a Gemfile in vendor/plugins/my_plugin like:
# Gemfile
source "http://rubygems.org"
gemspec
2) Create a gemspec file. In the folder vendor/plugins run this command:
bundle gem my_plugin
(Note this command ask you for overwrite some files. Check the files before answer: Y)
3) Open gemspec file in vendor/plugins/my_plugin/ and add before the keyword end:
s.add_dependency('will_paginate', '~> 3.0.pre2')
(In this example I have used will_paginate how required dipendency of my_plugin)
4) Now go in your rails app and edit Gemfile, add:
gem 'my_plugin', :path=>'vendor/plugins/my_plugin'
The path specified supposed that your plugin is already in vendor/plugins folder of your rails app.
Of course when deploy rails app you don't need anymore to specify :path argument.
5) Now in rails app root do:
bundle install
And dependency of my_plugin (will_paginate in this case) is installed.
Thank to Sinetris for initial input.
Create a Gemfile in your vendor/plugins/my_plugin/ like:
# Gemfile
source "http://rubygems.org"
gemspec
gem "your-dependency-gem-name"
note the gemspec directive.
Take a look at Using Bundler with Rubygem gemspecs for more information.
Sebtm's own answer is quite good, but it still didn't work as Tiago and orangechicken described. I had to add
require 'your-dependency-gem-name'
on top of lib/my_plugin.rb right before the engine of my_plugin is loaded.
See http://guides.rubyonrails.org/engines.html#other-gem-dependencies
Gemfile in the application folder.
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3
Reference for myself. If you're making a plugin that should work with Rails as a RailTie, use rails plugin my_plugin to create the gem instead of bundle gem my_plugin. It saves you (me) a lot of trouble.
Edit: When do you need the gem to work as a RailTie? Whenever you want to add rails generator, rake tasks or add your code to a Rails app (Called Rails Engine).
/Edit
$ rails plugin new my_plugin
then you add dependencies
Gem::Specification.new do |s|
#...
s.add_dependency "rails"
s.add_runtime_dependency "heroku"
s.add_development_dependency "sqlite3"
end
to include it in your rails app, use path: as described by #Sebtm
or release it to rubygems.
$ gem build my_plugin.gemspec
$ gem push my_plugin-0.7.0.gem #replace version number
#in Gemfile, as always
gem 'my_plugin'
or tag it on github. (use their release UI) and depend on it using
gem 'my_plugin', github: 'accountname/my_plugin', tag: 'v0.7.0'

Resources