Gem configuration in rails 3 - ruby-on-rails

I am new to Ruby on Rails and I am trying to use some gems that require to apply some changes in the initial configuration.
In this particular gem it says I have to change the configuration parameters. Where do I find this in ruby 1.9 and rails 3
Thanks

You can put that code anywhere -- if it's setting up and configuring objects that will be used throughout your app and you want it run when the app starts, you can put it in an initializer. config/initializers/APNS.rb

Related

Is it possible to create a ruby gem that when added to a rails project simply appends code to its initializers?

I've got some helper code (monkey patches) I tend to carry around from rails project to rails project via simply copying files or pasting code into the initializers folder.
I think a clean way to deploy different categories of my initalizer code would be to simply add gems to my project Gemfile that would do the equivalent of this for me. I'm not very skilled at creating gems or the deeper side of ruby on rails engineering so I just wanted to know if this were possible before I got started with it.
Yes, this is definitely possible. It should be probably enough to make a simple gem, put your monkey-patches there and require them in the gem entry point (e.g., if the gem is called foobar, then bundler will require lib/foobar.rb by default).
There is a guide on creating gems available in Bundler docs. In case you need to interact with Rails application configuration, you can take a look at Rails Engines.

Working on gem within a Rails app

What's the easiest way to work on a plain-ole Ruby gem, and then quickly turn around and run it in a Rails app?
I like to keep application logic out of Rails. Encourages code reuse across projects, and keeps my Rails app clean. It produces, however, an ugly workflow:
Test my gem. rake test
Build my gem. gem build ...
Upload gem to private repository (currently using Gemfury). fury push ...
Update my gem from within Rails (bundle update ...)
Run my Rails code.
Yuck. I'd like to simply save my file in the library, and then watch it work in the Rails project.
Clojure's Leiningen has a concept called "checkouts", allowing you to work on several libraries within one.
Techniques, anyone?
Use Bundler's path directive.
gem 'my_gem', :path => "~/my_gem"
You'll still have to restart the Rails server every time your code changes to reload it, but you won't have to go through the whole build-and-publish step for each revision. Be sure to update your gem reference to something production-worthy before pushing your code.

Rails + Gems (in general): How do gems work?

I've been using Rails for a while and have always used gems in my gemfile, but I never really understood how the functionality of gems that I install actually become available. Say I use the has_permalinks gem (http://haspermalink.org/). It provides a .generate_permalink! method for my Model. Where does this method get defined? How come just I can use this method all of a sudden just by installing the gem? Is there some sort of include/require/load to initialize the gem's code so that it becomes accessible to the rest of the application? Also, where is this code stored when I install the gem?
I answered your questions separately, and out of order, but I think it actually might make it easier to understand the answers in this order.
Also, where is this code stored when I install the gem?
If you're using Bundler, you can do bundle show has_permalink and it will show you where that gem is installed. Here's an example of me doing it with the pg gem:
✗ bundle show pg
/Users/jasonswett/.rvm/gems/ruby-1.9.2-p320#jason/gems/pg-0.11.0
Where does this method get defined?
If you do the bundle show thing, it returns a path - the method is defined somewhere in there. (You can use grep -r 'def generate_permalink' /gem/path to find exactly where if you want.)
How come just I can use this method all of a sudden just by installing
the gem? Is there some sort of include/require/load to initialize the
gem's code so that it becomes accessible to the rest of the
application?
Look at this part of the doc about the Rails initialization process:
http://guides.rubyonrails.org/initialization.html#config-boot-rb
In a standard Rails application, there’s a Gemfile which declares all
dependencies of the application. config/boot.rb sets
ENV["BUNDLE_GEMFILE"] to the location of this file, then requires
Bundler and calls Bundler.setup which adds the dependencies of the
application (including all the Rails parts) to the load path, making
them available for the application to load.
It looks like, fairly early on in the process, Rails looks at your Gemfile and loads all your gems via Bundler. So there's your include.

How to correctly embed a test Rails application inside the gem that it tests?

I have a small gem that I have created and a separate small Rails application that I use to test the gem.
I have been looking for information about how I could embed the test rails app inside the gem so it's packaged as one and so I can launch it to test the application.
I have currently got it inside the gem tree in a subdirectory of test. I can change to that subdirectory and run the app up ok, or I can run its tests with rake. But is this the correct way to do this ?
(in case it makes any difference, this gem and the small test app are for Rails 2.x)
I'd suggest you take a look at some other gems on github and what they do. Some probably don't need a full rails app, but devise, for instance has a barebones rails app embedded at /test/rails_app
https://github.com/plataformatec/devise/tree/master/test/rails_app

How can I create a rubygem and test it in a rails application at the same time?

I want to learn how to create a rubygem with a generator etc.
When I create a gem, do I create a separate project?
The reason being, since it will have generators and hook into the lifecycle of a Rails 3 application, I want to create a test rails 3 application at the same time to see how things are working etc.
Can someone outline what I should do to do this?
For example, I'm using git, so what I want to do is, when I run a generator for my gem and it doesn't do what I want, I can then easily rollback to the previous version using git.
This will be a simple gem, just trying to get a feel for things and how I can hook into various parts of rails etc.
Use bundler to create the new Gem with the following command:
bundle gem foogem
Then go to the Gemfile of the application you're going to use and add something like the following:
gem 'foogem', :path =>'/path/to/foogem'
In this way you can easily test your gem inside your rails project.

Resources