Stupid question, and forgive me for asking, but someone is asking me, and I am not a super expert with Rails yet.
Suppose I have some Rails gem I write. Now suppose a customer has some other framework, like Django or CakePHP, and I want to provide the functionality offered by my gem (eg. CRUD for automotive data) to them as a module in their framework. Could I somehow make it so they could interface my gem with Django or CakePHP?
Obviously I could do something with some API magic--and I'll probably end up going that route. But I just want to know whether there is a way to directly interface with Gems from a non-Rails application.
Not really. Gem's are by their very nature Ruby. They of course don't need to be for a Rails application they can be used with any ruby program you write, but really are the preferred method of packaging ruby libraries and programs.
Alternatively if the Ruby Gem in question is executable and your Ruby Gem's path is in your shell's path variable you can executed the gems binary like any command line tool and thus use it with anything.
Like like how Rails gives you the rails command.
Related
I want to write a gem that adds some functionalities to MyController in a Rails application. Basically I know how to write a gem, I have already wrote a couple of gems, used them in my Ruby applications. They work fine. But I have never written gems that deals with Rails classes or objects, in particular Controller classes.
I understand that Rails applications are Ruby applications, but it is at the same time a framework with a lot of out-of-box modules, classes, objects, and underlying structures and conventions.
So, is there any rules/standards/convention that one should follow when writing a gem to use it in Rails application? Maybe best practices?
I'm not a RoR expert, trying to write my first gem which includes javascript, erb, etc. Is this possible, it seems like every tutorial and example code I can find only includes ruby code. If it is possible, what does the directory structure of the gem look like with javascript and erb files? Thanks.
What you want is an engine.
Engines can be considered miniature applications that provide
functionality to their host applications. A Rails application is
actually just a "supercharged" engine, with the Rails::Application
class inheriting a lot of its behavior from Rails::Engine.
Therefore, engines and applications can be thought of almost the same
thing, just with subtle differences, as you'll see throughout this
guide. Engines and applications also share a common structure.
What are the options to integrate OrientDB with Ruby on Rails 4 ? I have found many gems for JRuby but there is no active gem for MRI Ruby.
I need document database and graphdatabase, so OrientDB should be interesting for my project.
This is a native Ruby client, though i don't think you can drop it directly into a rails app and work on it like mongoid, ActiveRecord, etc.
https://rubygems.org/gems/orient_db_client
Its 100% ruby so it should be compatible with MRI.
We are using https://github.com/veny/orientdb4r in production, which doesn't require JRuby, and it's been pretty good so far. We only use the REST client, which is obviously going to be slower than any of the other ways of interacting with OrientDB. The gem offers a binary client as well, but I haven't used it, and I think it's still in development.
I'm feeling fairly seasoned in Ruby on Rails by now, and have attempted to build my own Rails plugins. Going through that process, however, I realized that I really have not found very many good resources that clearly spell out what the conventions are for creating Rails gems/plugins, and how to efficiently accomplish some of the things that I wanted to do with my plugin.
It seems to me that the documentation for buildings Rails gems is not very good, but maybe I'm not looking in the right places. In an attempt to gain insight into how other gems are built, I've read through some of the source code of the Devise plugin for user authentication. I have found virtually nothing describing a procedure similar to how Devise injects its own methods into an existing model, even though this seems like it may be a very useful thing for a lot of good gems to do.
My question is this: Where should I go to learn how to build good Rails gems? Are there spelled out conventions for how to do certain things?
The currently popular method to do what you are trying to achieve is to use Engines. Engines basically let you mount one application inside another, allowing you to do anything from add a method or two, to adding a complete blog. The official guides have a very nice step-by-step guide to getting started, and there are many good unofficial guides, as well. An engine basically consists of a little bit of initialization code, the application code, and a dummy application for testing and development. It might look intimidating, at first, but it's much easier than it sounds, at first. Good luck
Here is the most modern approach to gem crafting with Bundler:
bundle gem your_gem
cd your_gem
edit your_gem.gemspec and add description, summary and optional website.
Add required gems such as rspec to the Gemfile.
rspec --init
touch spec/your_gem_spec.rb
Write good tests.
Add your code to lib/your_gem.rb.
When you're finished its time to build and push to rubygems.org:
gem build your_gem.gemspec
gem push your_gem-0.0.1.gem
And thats it. Next time you make a change be sure to change the version number in version.rb.
see below link that will help you how to build a good rails gem
Making a Gem
Some ruby gems like jekyll, toto and webby offer out of the box blog-type integration into your ruby app. Another way of developing a rich web blog-type application is to build and model the application yourself using pure ruby and rails practices. (e.g creating an Article and User model). The first offers out of the box features the 2nd option offers more customization and control.
In people's experience on Stack Overflow, which would be the best route and what would people consider when making the decision to use a gem out of the box versus going alone?
All of the gems you mentioned take static, markdown/textile/etc files and turn them into HTML websites. They take different approaches to it, with jekyll spitting out the finished website for hosting, toto doing the converting and routing on request, and webby doing the same as jekyll mostly.
If you're using Rails, it's important to note that none of these will integrate into your application well. They're built to more-or-less operate on their own.
Generally speaking, if a gem has the functionality you need, use it. They are not equivalent to plugins you find for Wordpress and Drupal where they are typically low-quality, buggy, poorly documented, etc. More often than not, gems simply add a couple modules that you can integrate into your application how you like.
On the other hand, a basic blog is pretty quick and simple in Rails, especially considering you've got a handy walkthrough guide straight from the Rails documentation on how to do it.
If you're new to Rails and want tight integration with your app, it's probably best to bake your own blog features.
This will take some time to do, but its worth it to learn how things really work.
If you're more seasoned, just look at the gem's API and documentation and decide if it does what you want it to do and if you're comfortable with how to integrate it. If so, it'll save you time.
One other consideration: who will be using the blog? Is it for internal use, and programmers will be the ones updating it? If that's the case, then you can make it very easy by not worrying about a lot of aesthetic polish in the back-end. Conversely, if you're making an app that includes a blogging component for the general public you might want it to feel more polished. In this case a gem might save you a lot of time.
It depends on your application.