Structure of Ruby code which is not intended to be a gem - ruby-on-rails

I'm working on a specific project where I have to write a SDK (kind of gem) on top of openid_connect gem. Currently my code consists of single file (but I'm also going to write some tests for it) and sample app which shows the use of SDK. Problem is that I cannot ship SDK as a rubygem as it has to be pushed to the same git repository as sample app. Client is going to use only SDK. SDK is a single class with methods in it.
My current approach is to write SDK in RubyOnRails /lib folder (but SDK has to work with other ruby frameworks as well) and let client download the SDK located inside sample app's lib folder.
1)Should my code go inside lib or vendor directory? Or there is some better approach?
2)How this code can be included in ruby app? Installed as a gem or included by require...
3)Can the tests go in the same folder as SDK (not in rails /test folder)?

1) It sounds like this code is not at all dependent on Rails. As such, do not create a Rails project for it. That would be adding ceremony and dependency that are unnecessary.
2) There is nothing wrong with having sample files in a gem's git repo. You can put them in a directory named samples or examples, for example.
3) To create a conventional gem directory structure, use bundle:
bundle gem my_gem_name
4) Your tests should go in a directory named test if they are Minitest, or spec if they are RSpec.

Related

How to access any of the classes/methods from someone else's Rails app?

I have a Rails project that needs to use someone else's Git repository in order to handle a bit of the backend work. I brought the guy's repository into my own and made it a submodule (not sure if this was the right move), so I have a folder called 'submodule' within my project directory (i.e it's on the same level as my app, bin, config folders).
To require all the file functionality in the submodule folder, I put the following in my application_controller.rb (using the require_all gem):
require_all 'submodule'
Here's my confusion: the submodule folder is a Rails app in and of itself, so it has an app folder, config, etc. I want to be able to use, in my application, any of the mailers/controllers/helpers that the submodule folder uses. I know I required them correctly, but how do I properly reference these classes/methods in my application? What's the syntax for that?
For example, the submodule has a deliveries controller which includes a DeliveryProcessor class, and the DeliveryProcessor class has a method called deliver. How do I call the DeliveryProcessor class's deliver method in my application's controller?

How to add JavaScript framework in rails app without adding gems?

I just want to know how to include frameworks like angularjs, polymer js, bootstrap.... Into rails app without adding corresponding gems. Reason is the gems that are available are changing and some of them are given up. So instead of gem I want to download the library files given by the vendor directly. How to add them into rails app?
You can download the framework source files/folders, add them to your vendor folder and require those. Thats essentially what those gems do. If you're looking for a more advanced approach you can look into setting up a package manager like gulp and use the direct npm packages

how to use stocktwits library?

I am building a stocktwits app in Ruby. I want to retrieve data through Stocktwits.com API. I found this library on github: https://github.com/jesseyoungmann/omniauth-stocktwits. But i am a newbie to Ruby and Rails. I am confused on how to use it. Could i use it without adding it to a Rails project? I mean i do not need to create a Rails project in order to use it. If i need to add it to Rails project, how should i add it?
Thanks in advance.
You do need to have a Rails app in order to use it. You can add it to your project by adding
gem 'omniauth-stocktwits'
to the Gemfile in the root of your project, and running bundle install.
Then follow the instructions in the README, which involves creating a file in the initialisers directory of your Rails app.

Why is there Rails.rb files all over the place?

Was digging around my Rails applications and noticed that there are rails.rb files all over the place. In my ruby gems directories like:
...gems\devise-2.0.4\lib\devise\rails.rb
...gems\cucumber-rails-1.3.0\lib\cucumber\rails.rb
...gems\railties-3.2.3\lib\rails.rb
I am assuming that there are executed whenever you issue some command like "rails xxx". So all these extra rails.rb files combine with the original rails.rb file to essentially make one big rails.rb file. Essentially, when we type in "rails xxx" it goes thru all them all?
Just looking for some confirmation PLUS a little more knowledge about this. Thanks.
The best way to understand what these rails.rb files are doing, is to read the source code.
ralties
devise
cucumber-rails
As you can see, in any library the file assumes a different scope. The common behaviour is that the file rails.rb normally contains the code required to initialize the library when loaded from a Rails project.
BTW, this has nothing to do with the script/rails command and there is no "big rails.rb" file.
The files are not generated but are simply source files of these libraries you are using.
In this case they are probably rails-related classes that either extend Rails in some way or modify it or make the library interact with Rails.
Rails is a very common framework in Ruby land so most if not all libraries will have some sort of integration with Rails.
By no means are all of those loaded when you run rails XXX but rather when your application loads these libraries their rails.rb files may be executed to provide some sort of integration with Rails.

Updating to Rails 3.2.2: How to properly move my plugin from the '/vendor' to '/lib' directory?

I am upgrading Ruby on Rails from 3.1 to 3.2.2 and I would like to know what I should make and at what I should be care in order to properly move my vendor plugin (note: it is not a gem and at this time I am not planning to make that a gem) from the directory /vendor to /lib as well as wrote in the official documentation:
Rails 3.2 deprecates vendor/plugins and Rails 4.0 will remove them completely. You can start replacing these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, lib/my_plugin/* and add an appropriate initializer in config/initializers/my_plugin.rb.
I refer mostly to the "an appropriate initializer in config/initializers/my_plugin.rb": What code should I put in that file?
More: Do you have some advice or alert on making the above process?
The initializer should contain the appropriate requires and other startup related tasks that are necessary for your plugin to work correctly. It's difficult to help you without real code examples from your app but this link should help you get started.
http://code.coneybeare.net/how-to-convert-simple-rails-23-style-plugins
The example in the link requires the plugin (now in the lib directory) and adds a module to ActiveRecord::Base.

Resources