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

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.

Related

How to convert existing rails application to gem

I have a rails application that is kind of similar to the active_admin gem. I want to make this existing application into a gem.
I know that you can generate a gem with:
bundler gem gem_name_here
This command creates an empty gem. Which is fine I suppose, I just don't know where to put all my code.
You're ostensibly supposed to put all of your code in the /lib directory. However, my application has an assets pipeline and an app directory with all my models, controllers, and views. I don't know where to place all of these directories in the gem. Any thoughts or ideas?
If you need me to share any code, I'll gladly share it.
You're describing a rails engine. Engine is a miniature rails application that can be used inside other rails app. For more details see official rails guide

Structure of Ruby code which is not intended to be a gem

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.

Rails 4 - Adding JS assets via gem

I am using a gem that somebody else wrote to serve the fabric javascript library. The gem is using an old version of the library (1.3) and I'd like to be using 1.4 (the latest version). I haven't found any other gems using this version. My question is, is this the best way to load assets, or is there a more preferred method? And, if so, how would I go about building this gem with the latest version of this library?
In my opinion, it is good to do so in most cases.
In your situation, depending on how much time you have, you may want to do one of the following:
1. Contribute to the gem
If the gem is open source, you may fork it, update to the newest version, and do a pull request.
By this way you also give contribution to the rails whole and the others who are facing the same problem as well.
Downside is this takes time. You have to wait for the author to accept the pull request and wait for the next version of the gem. But you can point your Gemfile to use your forked version until the new version is out ;)
2. Write your own gem
Writing a gem for rails providing assets is actually not difficult. You may follow other existing gem's structure and should be easy to understand.
A good example is https://github.com/rails/jquery-rails
Downside is you have to maintain the gem. Otherwise when fabric 1.5 is out, another one would ask the same question as yours again.
3. Put the assets in your vendor directory
Rails project by default do have a vendor directory. It's ok to put external assets here as well.
The above are my preferred way to manage external assets.

less-rails-bootstrap ... where are the less files

I don't know if this is a ridiculous question however I am creating a website with RefineryCMS, Ruby, and Rails. I chose to speed up some things by using Twitter's Bootstrap via the less-rails-bootstrap gem. I do however need to customize some colors and such though I can't seem to locate any of the less files. I looked in the logs and see references to things such as 'twitter/bootstrap.css' though I'm not able to locate this or where the less files live.
Where are these things? Else, how to do you override the defaults?
When you complete with gem install and bundle install command , you should :
rails g bootstrap:install
This will insert some files in your app/assets dir , including bootstrap_and_overrides.css.less in app/assets/stylesheets.
You can learn more in this Railscast.
UPDATE: (Jan 25 2014): Valuable information form the comment of #Niels Abildgaard:
According to GitHub repository of less-rails-bootstrap gem, Rails generator command shoud be :
rails generate less_rails_bootstrap:custom_bootstrap

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.

Resources